-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Confusing error when a const contains a shared ref to interior mutable data #140653
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
Comments
This comment has been minimized.
This comment has been minimized.
We may just want to support this? The static item clearly ensures there is no duplication or deduplication, so using the const to refer to the mutable static should be fine ™ |
One problem with this is patterns: we must not read from mutable state when turning a const pattern into a valtree. So if we allow constants that reference mutable state (rather than just having a raw ptr to it), we end up having consts that look like normal consts but that cause errors when they are used as patterns. Pattern construction happens during MIR building so these would be pre-monomorphization errors, so maybe that's fine. But avoiding those is why I kept all those strict checks in place. |
So based on the above I'm pretty sure now that having this error was deliberate, and the final-value validity check is the only place that can really do this (since we want to also forbid So the question is between either making the error message more accurate (this isn't UB, it's something we are deliberately forbidding on consts), or just allowing it and accepting that there are some consts that one cannot use as patterns. @rust-lang/lang any opinion on that? The problematic examples look something like: static mut S: i32 = 0;
const C: &'static i32 = unsafe { &S };
unsafe { S = 1; }
// What exactly does this compare with?
match foo {
C => ...
_ => ...
} This example is obviously blatantly unsound, but if we had a type that has interior mutability while also being
|
Is my following understanding of this issue correct?
So: assuming that constants referencing mutable memory are totally fine as long as they are not used in patterns, should we:
|
Not just questionable, also fundamentally incompatible with the way we compile constants in patterns, which is by turning them into a valtree and turning that into a normal pattern: if you match on an But other than that, yes, that sounds exactly correct. :) |
We talked about this in a short-staffed lang call. Both @joshtriplett and I were in favor of allowing these constants and then disallowing them in patterns. Josh presented an interesting use case about how one might reasonably want to use constants like this as a better replacement for global variables when translating code from C. We'd be interested in seeing a PR to do this nominated for lang so we can propose FCP on it. |
PR is up: #140942 |
I tried this code:
I expected to see this happen: Either it fails to compile due to a type error or it compiles successfully.
Instead, this happened: "it is undefined behavior to use this value"
Here's a self-contained reproducer:
yields
I lost context on "constants referencing statics" so I am not sure right now whether this is just a poorly worded error message, or whether there's some static check that should have caught this earlier but for some reason did not.
Cc @rust-lang/wg-const-eval
Meta
Latest nightly
The text was updated successfully, but these errors were encountered: