rust-embedded / cortex-m

Low level access to Cortex-M processors
Apache License 2.0
832 stars 152 forks source link

Allow static_mut_refs lint in macro expansions #561

Closed jannic closed 1 month ago

jannic commented 1 month ago

On nightly beta, the static_mut_refs lint defaults to warn. With some of our examples using #[deny(warnings)], this leads to the following error:

error: creating a mutable reference to mutable static is discouraged
  --> cortex-m-rt/examples/entry-static.rs:15:16
   |
15 |     static mut COUNT: u32 = 0;
   |                ^^^^^ mutable reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

With edition 2024, the lint will probably default to deny. (https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html)

To avoid that, set #[allow(static_mut_refs)] locally in the macro expansion.

This only silences the warning and doesn't answer the underlying question if we want to do that transform at all. See eg. https://github.com/rust-embedded/cortex-m/issues/411 for discussion.

adamgreig commented 1 month ago

Thanks. I think it makes sense to allow the lint for now, since it's otherwise hard for the user to do anything about.