You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Rustc doesn't error when trying to write to a const array #55721
"of course" not, you can't change constants, as they are (by definition) constant. If we changed that, we'd have a lot of problems (think about what something like 1 = 42; means)
What's going on here is totally not intuitive. Your code essentially expands to
I just spent more than an hour figuring out what was wrong with my code because I accidentally defined one of my values as const while trying to mutate it and Rust didn't even give a warning when it would throw an error if it was a static or a let binding. That just makes no sense whatsoever.
Reacted by gnzlbg, kamulos, Martin Pärtel and Brandon Bennett
// This outer `LocalKey<T>` type is what's going to be stored in statics,
// but actual data inside will sometimes be tagged with #[thread_local].
// It's not valid for a true static to reference a #[thread_local] static,
// so we get around that by exposing an accessor through a layer of function
// indirection (this thunk).
//
// Note that the thunk is itself unsafe because the returned lifetime of the
// slot where data lives, `'static`, is not actually valid. The lifetime
// here is actually slightly shorter than the currently running thread!
//
// Although this is an extra layer of indirection, it should in theory be
// trivially devirtualizable by LLVM because the value of `inner` never
// changes and the constant should be readonly within a crate. This mainly
// only runs into problems when TLS statics are exported across crates.
inner:unsafefn() -> Option<&'staticT>,
}
If this kind of code is very uncommon (which I suspect it is), we could probably get away with whitelisting LocalKey in any lint that we write. However, if there are any crates defining this kind of const, then downstream crates will get a false-positive warning of every single use of the const (even if the fact that it's a const is an implementation detail, like with thread_local!).
Activity
petrochenkov commentedon Nov 6, 2018
cc rust-lang/rust-clippy#829 #40543
oli-obk commentedon Nov 6, 2018
"of course" not, you can't change constants, as they are (by definition) constant. If we changed that, we'd have a lot of problems (think about what something like
1 = 42;means)What's going on here is totally not intuitive. Your code essentially expands to
which also doesn't produce any diagnostics.
Maybe we could have a lint which tells you about "unused" variables like this, even if these variables are temporaries.
MSxDOS commentedon Jul 7, 2019
This is ridiculous that such code even compiles:
I just spent more than an hour figuring out what was wrong with my code because I accidentally defined one of my values as
constwhile trying to mutate it and Rust didn't even give a warning when it would throw an error if it was astaticor aletbinding. That just makes no sense whatsoever.Aaron1011 commentedon Jun 12, 2020
It turns out that there is a legitimate usage of this pattern with
thread_local:When you use the
thread_local!macro, a new const gets defined:rust/src/libstd/thread/local.rs
Lines 183 to 186 in 7c78a5f
A
LocalKeystores a function pointer, so it's fine for it to get copied at each call site:rust/src/libstd/thread/local.rs
Lines 78 to 94 in 7c78a5f
If this kind of code is very uncommon (which I suspect it is), we could probably get away with whitelisting
LocalKeyin any lint that we write. However, if there are any crates defining this kind ofconst, then downstream crates will get a false-positive warning of every single use of theconst(even if the fact that it's aconstis an implementation detail, like withthread_local!).Aaron1011 commentedon Jun 12, 2020
Another challenge is interior mutability: this code is probably fine:
but this code isn't:
Auto merge of rust-lang#75573 - Aaron1011:feature/const-mutation-lint…
Auto merge of rust-lang#75573 - Aaron1011:feature/const-mutation-lint…