-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Disallow *references* to static mut
[Edition Idea]
#114447
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I think that this would be much better if methods on a type could take |
We discussed this in the lang team meeting today, and the consensus was in favour of going in this direction. (With https://doc.rust-lang.org/std/cell/struct.SyncUnsafeCell.html and https://doc.rust-lang.org/std/ptr/macro.addr_of_mut.html we have options that we didn't back in 2018.) Steps needed, I think:
|
@rustbot claim |
@rustbot labels -I-lang-nominated I'm going to unnominate this, since there's nothing more to discuss until there is a PR to review. Please nominate such a PR as soon as it is posted. (This is correctly labeled so we can track it with other ideas for the next edition without the nomination.) |
@obeis Any updates? Have you managed to make progress here? |
@scottmcm almost done. I will submit my pull request within the next two days at the latest. I apologize for the delay. |
While implementing the lint in #117556, the question came up whether various cases of implicitly taking a reference should be linted against: static mut STATIC = &[0, 1, 2];
let _ = STATIC.len();
let _ = STATIC[0];
let _ = format!("{}", STATIC); The three listed cases are (fairly) harmless. However, @scottmcm (and t-lang in general), what is your opinion on that? |
Personally I lean towards deprecating these cases, too, if the idea is that we want to mostly deprecate |
I think this should be linting about anything that's created enough of a reference to trigger the reference rules -- if it can trigger UB when someone else holds a |
Are |
This discussion is only about So |
…twco Disallow reference to `static mut` and adding `static_mut_ref` lint Closes rust-lang#114447 r? `@scottmcm`
…twco Disallow reference to `static mut` and adding `static_mut_ref` lint Closes rust-lang#114447 r? `@scottmcm`
…twco Disallow reference to `static mut` and adding `static_mut_ref` lint Closes rust-lang#114447 r? ``@scottmcm``
Disallow reference to `static mut` and adding `static_mut_ref` lint Closes rust-lang#114447 r? `@scottmcm`
Okay, I thought you were thinking of getting rid of mutable statics entirely. The 1.78 compiler warning which brought me to this page only mentioned |
That's exactly why we are only deprecating references to My personal opinion is that switching from |
Well, the simplest transition is to https://doc.rust-lang.org/std/cell/struct.SyncUnsafeCell.html, but unfortunately that's not yet stable :( You can always also make your own types with appropriate
The differences are in whether you get a reference directly. |
This is clearer now, thanks. I can see the justification for some kind of However, I couldn't use I don't think my new code is any better or worse without |
I feel this way too, and am glad that you do, too! Still, if there's some busywork to discourage misuse, I can live with that, just please let me have (the equivalent of) my: let p = ptr::addr_of_mut!(STATIC_MUT); There's programs I can't express without. For instance, we have a low-overhead profiler in our company that is just two rdtscs, and a couple of adds, loads and stores per profiling zone. The profiler is very careful to not create references, and it can only be ran in isolated single-threaded scenarios when we are tuning algorithms. |
With at least rustc 1.79.0 (129f3b996 2024-06-10) (Fedora 1.79.0-3.fc40) We were getting warnings when building the rust examples like warning: creating a mutable reference to mutable static is discouraged --> src/lib.rs:75:40 | 75 | let ctx: *mut luw_ctx_t = unsafe { &mut CTX }; | ^^^^^^^^ mutable reference to mutable static | = note: for more information, see issue #114447 <rust-lang/rust#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: `#[warn(static_mut_refs)]` on by default help: use `addr_of_mut!` instead to create a raw pointer | 75 | let ctx: *mut luw_ctx_t = unsafe { addr_of_mut!(CTX) }; | ~~~~~~~~~~~~~~~~~ So do like it says and use the addr_of_mut!() macro. Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
As a reminder, the Rust issue tracker is for filing bugs or issues with the language, it is not for use as a helpdesk. This includes creating code of dubious quality via passing a random number generator through a filter (or vector of weights) and asking us to not lint on it: When lints trigger correctly, they are supposed to trigger on code of dubious quality, to discourage reusing it. |
fix for issue #114447 <rust-lang/rust#114447>
``` warning: creating a mutable reference to mutable static is discouraged --> src/main.rs:40:29 | 40 | unsafe { ALLOCATOR.init(&mut HEAP as *const u8 as usize, core::mem::size_of_val(&HEAP)) }; | ^^^^^^^^^ mutable reference to mutable static | = note: for more information, see issue #114447 <rust-lang/rust#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: `#[warn(static_mut_refs)]` on by default help: use `addr_of_mut!` instead to create a raw pointer | 40 | unsafe { ALLOCATOR.init(addr_of_mut!(HEAP) as *const u8 as usize, core::mem::size_of_val(&HEAP)) }; | ~~~~~~~~~~~~~ + ```
``` warning: creating a mutable reference to mutable static is discouraged --> src/main.rs:40:29 | 40 | unsafe { ALLOCATOR.init(&mut HEAP as *const u8 as usize, core::mem::size_of_val(&HEAP)) }; | ^^^^^^^^^ mutable reference to mutable static | = note: for more information, see issue #114447 <rust-lang/rust#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: `#[warn(static_mut_refs)]` on by default help: use `addr_of_mut!` instead to create a raw pointer | 40 | unsafe { ALLOCATOR.init(addr_of_mut!(HEAP) as *const u8 as usize, core::mem::size_of_val(&HEAP)) }; | ~~~~~~~~~~~~~ + ```
Summary: Fixes the following warning by following the advice given: ``` warning: creating a mutable reference to mutable static is discouraged --> fbcode/eden/scm/lib/cpython-ext/src/bytesobject.rs:37:13 | 37 | &mut PyBytes_Type as *mut PyTypeObject, | ^^^^^^^^^^^^^^^^^ mutable reference to mutable static | = note: for more information, see issue #114447 <rust-lang/rust#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: `#[warn(static_mut_refs)]` on by default help: use `addr_of_mut!` instead to create a raw pointer | 37 | addr_of_mut!(PyBytes_Type) as *mut PyTypeObject, | ~~~~~~~~~~~~~ + ``` Reviewed By: quark-zju Differential Revision: D65218834 fbshipit-source-id: 8298cdf8089b6d8516e0cb59a811b30465eb1f8c
See rust-lang/rust#114447 for more information about the issue.
This fixes this warning: warning: shared reference of mutable static is discouraged --> stm32l0xx-hal/src/signature.rs:45:40 | 45 | core::str::from_utf8_unchecked(&DEVICE_ID_STR) | ^^^^^^^^^^^^^^ shared reference of mutable static | = note: for more information, see issue #114447 <rust-lang/rust#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: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer Co-authored-by: Javier Cardona <javier@cardonabits.com>
Now that we have
ptr::addr_of_mut!
, the 2024 edition would be a great time to do @RalfJung's idea from #53639 (comment):Disallow
&MY_STATIC_MUT
and&mut MY_STATIC_MUT
entirely.ptr::addr_of_mut!(MY_STATIC_MUT)
could even be safe -- unlike taking a reference which isunsafe
and often insta-UB -- and helps encourage avoiding races in the accesses, which is more practical than trying to avoid the UB from concurrent existence of references.(Maybe people will end up just doing
&mut *ptr::addr_of_mut!(MY_STATIC_MUT)
without thinking through all the safety requirements -- in particular, making sure that the reference stops being used before a second reference is created -- but we can't force them to drink.)The text was updated successfully, but these errors were encountered: