ferrous-systems / rust-exercises

Exercises for learning Rust, by Ferrous Systems
Creative Commons Attribution Share Alike 4.0 International
183 stars 17 forks source link

Note: use of static mut is a warning in 1.77 #84

Closed jonathanpallant closed 6 months ago

jonathanpallant commented 6 months ago

Need to make static mut go away, or disable deny(warnings)

tallamjr commented 6 months ago

Ref:

15:03:08 ✘ ~/github/tallamjr/forks/rust-exercises/nrf52-code/usb-app (main) :: rustc --version
rustc 1.77.0-beta.5 (f2043422f 2024-02-17)
15:02:55 ✔ ~/github/tallamjr/forks/rust-exercises/nrf52-code/usb-app (main) :: cargo run --bin hello -- --allow-erase-all
   Compiling dk v0.0.0 (/Users/tallamjr/github/tallamjr/forks/rust-exercises/nrf52-code/boards/dk)
error: mutable reference of mutable static is discouraged
   --> /Users/tallamjr/github/tallamjr/forks/rust-exercises/nrf52-code/boards/dk/src/lib.rs:277:36
    |
277 |         ep0in: unsafe { Ep0In::new(&mut EP0IN_BUF) },
    |                                    ^^^^^^^^^^^^^^ 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: the lint level is defined here
   --> /Users/tallamjr/github/tallamjr/forks/rust-exercises/nrf52-code/boards/dk/src/lib.rs:6:9
    |
6   | #![deny(warnings)]
    |         ^^^^^^^^
    = note: `#[deny(static_mut_ref)]` implied by `#[deny(warnings)]`
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
    |
277 |         ep0in: unsafe { Ep0In::new(addr_of_mut!(EP0IN_BUF)) },
    |                                    ~~~~~~~~~~~~~~~~~~~~~~~

error: could not compile `dk` (lib) due to 1 previous error

Reverting to v1.76.0 solved the issue

15:03:41 ✔ ~/github/tallamjr/forks/rust-exercises/nrf52-code/usb-app (main) :: rustup default stable
info: using existing install for 'stable-x86_64-apple-darwin'
info: default toolchain set to 'stable-x86_64-apple-darwin'

  stable-x86_64-apple-darwin unchanged - rustc 1.76.0 (07dca489a 2024-02-04)
ryan-summers commented 6 months ago

I just hit this while updating usb-device in https://github.com/rust-embedded-community/usb-device/pull/144 - the quick fix in my case was to simply wrap the static mut buffer in an UnsafeCell. Hope that helps!


Edit: We talked about this further, and the specific commit linked wouldn't be the right approach. Instead, use a static UnsafeCell and then unsafe {&mut *UnsafeCell::get() }. All of these approaches are equally unsafe and require that the reference only exist once.


Edit 2: After further discussion, you cannot have a static UnsafeCell because it does not ever implement Sync. As such, the only approach currently is to use a static mut UnsafeCell and wait for SyncUnsafeCell to stabilize