Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upProduce a warning when using `const` with interior mutability #40543
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Note that compare_and_swap(&self, current: bool, new: bool, order: Ordering) -> bool |
This comment has been minimized.
This comment has been minimized.
|
The underlying issue is the same - |
This comment has been minimized.
This comment has been minimized.
|
New warnings generally require an RFC, but I'll let @rust-lang/compiler make the call |
steveklabnik
added
A-lint
T-compiler
labels
Mar 15, 2017
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
I think "calling a method exploiting interior mutability on |
This comment has been minimized.
This comment has been minimized.
|
Seems like it would have to "calling any method on |
This comment has been minimized.
This comment has been minimized.
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 |
Mark-Simulacrum
added
C-bug
C-feature-request
and removed
C-bug
labels
Jul 27, 2017
This comment has been minimized.
This comment has been minimized.
|
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. |
petrochenkov
referenced this issue
Jan 19, 2018
Closed
Disallow taking non zero-sized constants by mutable reference #885
This comment has been minimized.
This comment has been minimized.
burdges
commented
Jan 19, 2018
|
Another autotrait would be required for this, no? All atomics have this problem, but maybe not all usages of Also, there are usages for |
This comment has been minimized.
This comment has been minimized.
pedrohjordao
commented
Aug 11, 2018
|
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. |
This comment has been minimized.
This comment has been minimized.
|
@burdges has a point. Items like the following should be illegal, not merely illegal to try mutating. const X: Cell<u32> = Cell::new(0); |
This comment has been minimized.
This comment has been minimized.
|
@dhardy Why so? That's not a |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
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. |
matklad commentedMar 15, 2017
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
constis used instead ofstatic.It would be nice to somehow give a warning for
.compare_and_swapcall, but I am not sure it is possible.