-
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. |
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- |
…reject such constants as patterns
6f196f8
to
4af530e
Compare
@oli-obk based on @ehuss's questions in rust-lang/reference#1859, I made the "borrow of non-transient" error more uniform (use a shared error for raw and ref borrows, and use the same wording for mutable and interior mutable borrows), and made them explicitly talk about lifetime extension, since we have to wait for that reference PR anyway. If you prefer, I can also split this into a separate PR. |
This comment has been minimized.
This comment has been minimized.
42b121f
to
1e43cc2
Compare
Some changes occurred to constck cc @fee1-dead |
This comment has been minimized.
This comment has been minimized.
@@ -1,4 +1,4 @@ | |||
error[E0764]: mutable references are not allowed in the final value of constants | |||
error[E0764]: mutable borrows of lifetime-extended temporaries are not allowed in constants |
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.
This is still somewhat abbreviated... lifetime extension "within" constants is fine, it's just lifetime extension at the top-level scope where we restrict borrows. I couldn't think of a good way to concisely state this.
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.
we don't need to be concise if we add some more info at the label or in a note. That's probably worth it for this case that will otherwise send most ppl to having to ask for help in a forum
1e43cc2
to
3d035aa
Compare
The job Click to see the possible cause of the failure (guessed by this bot)
|
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.