embassy-rs / embassy

Modern embedded framework, using Rust and async.
https://embassy.dev
Apache License 2.0
5.34k stars 738 forks source link

mutable reference of mutable static is discouraged #2599

Open ForsakenHarmony opened 7 months ago

ForsakenHarmony commented 7 months ago

https://github.com/embassy-rs/embassy/blob/377e58e408f830f79171a470ba602b7d8bc525e4/examples/rp/src/bin/multicore.rs#L33

warning: mutable reference of mutable static is discouraged
  --> src/...
   |
82 |     spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || {
   |                                   ^^^^^^^^^^^^^^^^ mutable reference of mutable static
   |
   = note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
   = note: reference of mutable static is a hard error from 2024 edition
   = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
   = note: `#[warn(static_mut_ref)]` on by default
help: mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
   |
82 |     spawn_core1(p.CORE1, unsafe { addr_of_mut!(CORE1_STACK) }, move || {
   |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~

https://github.com/rust-lang/rust/issues/114447

peterkrull commented 7 months ago

A fix sees to be just using StaticCell

static STACK: StaticCell<Stack<4096>> = StaticCell::new();

and then

spawn_core1(p.CORE1, STACK.init(Stack::new()), move || {
    let executor1 = EXECUTOR.init(Executor::new());
    executor1.run(|spawner| unwrap!(spawner.spawn(core1_task())));
});