-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Produce a warning when using const
with interior mutability
#40543
Comments
Note that compare_and_swap(&self, current: bool, new: bool, order: Ordering) -> bool |
The underlying issue is the same - |
New warnings generally require an RFC, but I'll let @rust-lang/compiler make the call |
I'm not sure -- precisely what conditions would trigger the warning? I agree there is a subtle footgun at play here, if you don't understand the rules. |
I think "calling a method exploiting interior mutability on |
Seems like it would have to "calling any method on |
Yep. Perhaps this won't add false positives in practice? I wonder if we need to warn about creating constants with interior mutability. This is the root of the problem. Currently we need such constants for |
I'm not sure that's quite right:
Seems to me like this behaviour occurs whenever an internally mutable rvalue is borrowed. However, it's only surprising when that rvalue comes from a constant, because constants don't look like rvalues, and because they can be used multiple times, whereas most rvalues can only be used once. Putting that together, it looks like the compiler should issue a warning whenever a constant containing an UnsafeCell is borrowed. |
Another autotrait would be required for this, no? All atomics have this problem, but maybe not all usages of Also, there are usages for |
Just got hit by this unexpected behavior as well and thought I was going crazy. Reading the explanations made everything make sense, but some warning would be ideal. |
@burdges has a point. Items like the following should be illegal, not merely illegal to try mutating. const X: Cell<u32> = Cell::new(0); |
@dhardy Why so? That's not a |
But are there any legitimate uses for this? Possibly as part of a compound type I guess, but that would be unusual. A lint may be better then. Okay, the problematic bit is that one can call |
@dhardy Yeah. IMO, this is in the same category as this: struct Foo<T>(T);
const X: Foo<i32> = Foo(0);
fn main() {
X.0 += 1;
} Clippy should be linting both this and your example, saying that you're mutating a (field of a) temporary and you probably wanted to do something else. |
Why rely on Clippy here? As I understand it, Clippy is primarily about style and succinctness, not correctness. If someone is assigning to a temporary field that is very likely incorrect code. |
Hi, I'm a beginner learning Rust and I ran into this issue as well. I see this hasn't had much movement in the last year or so, I would love to contribute towards this I'm just not sure where I need to start. Thanks in advance. Here is the snippet I was using: const ARRAY: [i32; 3] = [0; 3];
ARRAY[1] = 2;
for x in &ARRAY {
print!("{} ", x);
}
// output: 0 0 0 % Interestingly enough if I use
|
OnceCell / std::lazy is also to this: matklad/once_cell#145, #82842 |
Pinging this thread to see if there's any progress or thought and how this might be properly implemented.
use core::cell::Cell;
pub const TEST_CELL: Cell<u32> = Cell::new(10);
pub fn test(x: &Cell<u32>) -> u32 {
x.set(x.get() + 1);
x.get()
}
pub fn main() {
// 11 is printed twice
println!("got: {}", test(&TEST_CELL));
println!("got: {}", test(&TEST_CELL));
} Given the only usage of
|
Clippy has two lints for this situation: |
Now that |
Yeah, an opt-in or opt-out would be nice. The clippy lint is pretty useful, but sometimes it's also unhelpful, for example when storing the result of http::header::HeaderName::from_static in a |
This too has hit me now. |
I accidentally ran into this when using a |
I've previously hit and reported a false positive (rust-lang/rust-clippy#7665) with the clippy lint clippy::declare_interior_mutable_const. In short, when
However, this code triggers the clippy lint, and even gives the incorrect suggestion to change the I did just learn that since 1.63 we've had this cleaner alternative:
Either way, the relevant question I was wondering about in that issue:
Are there cases where an interior mutable |
Originally reported in https://users.rust-lang.org/t/broken-atomics-puzzle/9533
Consider this code
Playground
It compiles and runs cleanly, but produces unexpected results because
const
is used instead ofstatic
.It would be nice to somehow give a warning for
.compare_and_swap
call, but I am not sure it is possible.The text was updated successfully, but these errors were encountered: