-
Notifications
You must be signed in to change notification settings - Fork 13.4k
const-eval: allow constants to refer to mutable/external memory, but reject such constants as patterns #140942
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
base: master
Are you sure you want to change the base?
Conversation
@rust-lang/lang nominating for FCP following prior discussion in #140653. |
This comment has been minimized.
This comment has been minimized.
f619969
to
9767f96
Compare
This comment has been minimized.
This comment has been minimized.
9767f96
to
160cee0
Compare
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
160cee0
to
e316943
Compare
e316943
to
6722d4d
Compare
This comment has been minimized.
This comment has been minimized.
r? @oli-obk (or someone way more familiar with const-eval) |
57ea31d
to
6e9a7f4
Compare
As discussed in #140653 (comment), this sounds right to me, and I propose that we do it. @rfcbot fcp merge |
Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
24338f5
to
6f196f8
Compare
@oli-obk FCP should finish fairly soon here, so this is ready for review. :) @traviscross I guess this will also need a reference PR? Not sure when I'll get around to that. |
cc @ehuss |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My preference would have been 3 commits
- error reporting pulled out of query
- stylistic refactoring of match with large arm
- actual changes
but since I already reviewed everything, let's land it as is
Thanks! I agree smaller commits are nicer in retrospect, but I discovered these things half-way through implementation so it'd have been a lot of work to disentangle them... |
Maybe you are referring to this? That was added as part of const_refs_to_statics (rust-lang/reference#1610) |
Ah, yes, that's it. That should be just removed, and then instead we need something here about consts referencing mutable/extern statics. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Reference PR: rust-lang/reference#1859 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
@bors r=oli-obk |
@bors r- |
static mut S: i32 = 3; | ||
const C2: &'static mut i32 = unsafe { &mut S }; | ||
//~^ ERROR: reference to mutable memory | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this removed? It doesn't seem like its behavior was changed by this PR (it still generates the same E0080 error). Is it covered in some other test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh, I don't remember...
The error message does change, so it is unclear whether this even still tests the right thing. 🤷 But I can add the test back with the new error, sure.
…reject such constants as patterns
6f196f8
to
4af530e
Compare
This fixes #140653 by accepting code such as this:
This can be written entirely in safe code, so there can't really be anything wrong with it.
We also accept the much more questionable following code, since it looks very similar to the interpreter:
Using this without causing UB is at least very hard (the details are unclear since it is related to how the aliasing model deals with the staging of const-eval vs runtime code).
If a constant like
C2
is used as a pattern, we emit an error:(If you somehow manage to build a pattern with constant
C
, you'd get the same error, but that should be impossible: we don't have a type that can be used in patterns and that has interior mutability.)The same treatment is afforded for shared references to
extern static
, for the same reason: the const evaluation is entirely fine with it, we just can't build a pattern for it -- and when using interior mutability, this can be totally sound.We do still not accept anything where there is an
&mut
in the final value of the const, as that should always require unsafe code and it's hard to imagine a sound use-case that would require this.