Skip to content

fix(coretex-m-rt-macros): Allow #[unsafe(link_section = )] for Rust 2024 edition #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

yutannihilation
Copy link
Contributor

Rust 2024 edition requires marking link_section attribute as unsafe. Otherwise, the compilation fails with the following error.

https://doc.rust-lang.org/nightly/edition-guide/rust-2024/unsafe-attributes.html

error: unsafe attribute used without unsafe
  --> src/bin/tmp.rs:21:3
   |
21 | #[link_section = ".bi_entries"]
   |   ^^^^^^^^^^^^ usage of unsafe attribute
   |
help: wrap the attribute in `unsafe(...)`
   |
21 | #[unsafe(link_section = ".bi_entries")]
   |   +++++++                            +

error: could not compile `rp2350-blink` (bin "tmp") due to 1 previous error

However, if I use #[unsafe(link_section = ".bi_entries")], cortex-m-crate complains that it's not allowed (cf. embassy-rs/embassy#4170).

   Compiling embassy-nrf v0.3.1 (/ci/code/embassy-nrf)
error: this attribute is not allowed on an interrupt handler controlled by cortex-m-rt
  --> /ci/code/embassy-rp/src/multicore.rs:93:1
   |
93 | #[unsafe(link_section = ".data.ram_func")]
   | ^

error: this attribute is not allowed on an interrupt handler controlled by cortex-m-rt
   --> /ci/code/embassy-rp/src/multicore.rs:118:1
    |
118 | #[unsafe(link_section = ".data.ram_func")]
    | ^

   Compiling embassy-stm32 v0.2.0 (/ci/code/embassy-stm32)

This pull request tweaks the check logic to allow this notation so that users can use Rust 2024 edition.

@BartMassey
Copy link
Member

Looks good to me.

@therealprof
Copy link
Contributor

How does this PR figure out whether edition 2024 is in play or not? Or is that up to the user to figure out? It would be nice if there was a way to provide guidance about that change between editions.

@yutannihilation
Copy link
Contributor Author

#[unsafe(link_section = )] is a valid syntax both in edition 2021 (Rust >=1.85) and in edition 2024. The difference is that it's mandatory in edition 2024. The user will immediately see the guidance in the error message by the Rust compiler if they tries to compile without unsafe(). So, I believe there's nothing that has to be done on cortex-m-rt-macros' side. Any concerns...?

@therealprof
Copy link
Contributor

#[unsafe(link_section = )] is a valid syntax both in edition 2021 (Rust >=1.85) and in edition 2024. The difference is that it's mandatory in edition 2024. The user will immediately see the guidance in the error message by the Rust compiler if they tries to compile without unsafe(). So, I believe there's nothing that has to be done on cortex-m-rt-macros' side. Any concerns...?

Sounds excellent to me then. No concerns. 😄

@therealprof therealprof added this pull request to the merge queue May 7, 2025
Merged via the queue into rust-embedded:master with commit c04e4db May 7, 2025
11 checks passed
@yutannihilation
Copy link
Contributor Author

Thanks for reviewing!

@yutannihilation yutannihilation deleted the fix/allow-unsafe-wrap-rust2024 branch May 7, 2025 05:44
@Sympatron
Copy link

Sympatron commented May 7, 2025

is a valid syntax both in edition 2021 (Rust >=1.85)

The docs currently state an MSRV of 1.61:

//! This crate is guaranteed to compile on stable Rust 1.61 and up. It *might*
//! compile with older versions but that may change in any new patch release.

This crate is guaranteed to compile on stable Rust 1.61.0 and up. It might compile with older versions but that may change in any new patch release.

This crate is guaranteed to compile on stable Rust 1.61.0 and up. It *might*
compile with older versions but that may change in any new patch release.

//! The MSRV of this release is Rust 1.61.0.

@therealprof
Copy link
Contributor

The docs currently state an MSRV of 1.61:

This doesn't affect MSRV. People can still use the old "safe" link_section attribute. This only changes that you can use the unsafe link_section attribute at all. Starting with Rust 1.85 you can use either, with edition 2024 it's a MUST use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants