ferrous-systems / rust-exercises

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

Removed use of static mut. #85

Closed jonathanpallant closed 3 months ago

jonathanpallant commented 3 months ago

Remove static mut values, our references to which were going to cause a warning in the next Rust release.

Uses the grounded crate, and a custom wrapper to make an !Sync type Sync.

Closes: #84

jonathanpallant commented 3 months ago

Now works on beta:

$ cargo +beta build
    Finished dev [optimized + debuginfo] target(s) in 0.04s
$ rustc +beta --version
rustc 1.77.0-beta.7 (339fb6965 2024-03-06)

It used to fail:

$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
$ cargo +beta build
   Compiling dk v0.0.0 (/Users/jonathan/Documents/ferrous-systems/rust-exercises/nrf52-code/boards/dk)
error: creating a mutable reference to mutable static is discouraged
   --> /Users/jonathan/Documents/ferrous-systems/rust-exercises/nrf52-code/boards/dk/src/lib.rs:277:36
    |
277 |         ep0in: unsafe { Ep0In::new(&mut EP0IN_BUF) },
    |                                    ^^^^^^^^^^^^^^ mutable reference to mutable static
    |
    = note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
    = note: this will be a hard error in the 2024 edition
    = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
note: the lint level is defined here
   --> /Users/jonathan/Documents/ferrous-systems/rust-exercises/nrf52-code/boards/dk/src/lib.rs:6:9
    |
6   | #![deny(warnings)]
    |         ^^^^^^^^
    = note: `#[deny(static_mut_refs)]` implied by `#[deny(warnings)]`
help: 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
➜  usb-app git:(main)
miguelraz commented 3 months ago

I got 5 working builds with cargo +beta build. 🎉

miguelraz commented 3 months ago

Writing this for posterity: I spent a chunk of my morning debugging this build because I kept getting a

error[E0463]: can't find crate for `core`
  |
  = note: the `thumbv7em-none-eabihf` target may not be installed
  = help: consider downloading the target with `rustup target add thumbv7em-none-eabihf`

error[E0463]: can't find crate for `compiler_builtins`

series of errors when building the hal-app BUT I do have a thumb7em-none-eabihf target installed, just not on beta!

The fix was to switch to beta, then add the target, and everything built.

Pretty sure I should file an issue that some diagnostic or another should come up and suggest doing that - it was not obvious targets are dependent on rustup tool by default from the error message.

jonathanpallant commented 3 months ago

I'll ask on the project Zulip about that - I suspect the issue is that rustc doesn't know how it was invoked, and so it can't tell you what rustup arguments to use.