Repository navigation
const-check: clarify when and how we can rely on non-UB assumptions - #164088
Merged
Merged
Conversation
Collaborator
|
Some changes occurred to constck cc @fee1-dead Some changes occurred to the CTFE machinery |
tmiasko
approved these changes
Oct 10, 2026
| /// Normally, we would determine what qualifications apply to each type and error when an illegal | ||
| /// operation is performed on such a type. However, this was found to be too imprecise, especially | ||
| /// in the presence of `enum`s. If only a single variant of an enum has a certain qualification, we | ||
| /// needn't reject code unless it actually constructs and operates on the qualified variant. |
Contributor
There was a problem hiding this comment.
This would be an additional example of non-UB assumption. This kind of reasoning presupposes that producing an invalid values don't surprisingly change an active variant.
Contributor
There was a problem hiding this comment.
This also demonstrates that it relies on subobject provenance, since write could have been to any other field.
oli-obk
approved these changes
Oct 12, 2026
Contributor
rust-bors Bot
pushed a commit
that referenced
this pull request
Oct 12, 2026
Rollup of 5 pull requests Successful merges: - #163981 (New solver opts for `diesel`) - #164088 (const-check: clarify when and how we can rely on non-UB assumptions) - #164105 (Remove unnecessary indirection around `hir::ConstArg`) - #164181 (Tweak output on unused/unaccessible local macro) - #164186 (Update actions/upload-artifact action to v7.0.2)
rust-bors Bot
pushed a commit
that referenced
this pull request
Oct 12, 2026
Rollup of 5 pull requests Successful merges: - #163981 (New solver opts for `diesel`) - #164088 (const-check: clarify when and how we can rely on non-UB assumptions) - #164105 (Remove unnecessary indirection around `hir::ConstArg`) - #164181 (Tweak output on unused/unaccessible local macro) - #164186 (Update actions/upload-artifact action to v7.0.2)
rust-bors Bot
pushed a commit
that referenced
this pull request
Oct 12, 2026
Rollup merge of #164088 - RalfJung:const-check-ub, r=oli-obk const-check: clarify when and how we can rely on non-UB assumptions Turns out const-checking relies on `&x` being immutable. This is the "most obvious" property we get from the aliasing model, and we emit LLVM attributes and have MIR-opts that rely on it. I just didn't realize that taking away this UB would actually be a breaking change; there is code that runs on stable that we'd have to stop accepting if we took away this UB: ```rust struct NotConstDestruct; impl Drop for NotConstDestruct { fn drop(&mut self) { eprintln!("this is not const"); } } const _: () = { let x = None::<NotConstDestruct>; let ptr = std::ptr::from_ref(&x); unsafe { ptr.cast_mut().write(Some(NotConstDestruct)) }; }; ``` This means that I accidentally fixed a hole in const-checking when I added `&` immutability checking to const-eval. r? @oli-obk Cc @rust-lang/opsem @tmiasko
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Turns out const-checking relies on
&xbeing immutable. This is the "most obvious" property we get from the aliasing model, and we emit LLVM attributes and have MIR-opts that rely on it. I just didn't realize that taking away this UB would actually be a breaking change; there is code that runs on stable that we'd have to stop accepting if we took away this UB:This means that I accidentally fixed a hole in const-checking when I added
&immutability checking to const-eval.r? @oli-obk
Cc @rust-lang/opsem @tmiasko