-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Fix inline const pattern unsafety checking in THIR #116482
Merged
bors
merged 2 commits into
rust-lang:master
from
matthewjasper:thir-unsafeck-inline-constants
Oct 25, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
I don't think it's that intuitive to hide the range pattern behind this nested
PatKind::InlineConstant
construction. Having an explicit range pattern makes this clearer imo. To me this seems to add complexity, but I don't know off the top of my head how these range patterns with inline constants are currently handled in later stages. Does this make things easier in later stages in a way that justifies introducing the complexity here?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 makes things simpler for the consuming side: unsafety checking only cares that there are some inline constants around somewhere, pattern checking only cares that there's a range pattern whose extremities can be evaluated to bits. Carrying the unevaluated constant around felt invisible and brittle, my gut said danger.
But I am biased. I have an open PR (#116692) that reworks
PatRange
a lot, and I'm hoping I can eventually evaluate the consts eagerly and store bits inPatRange
.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.
I don't understand this. Looking at
const_to_pat
, we always error if we find an unevaluatable constant. When we lower inline consts we currently always go throughconst_to_pat
in the non-error case. Why would we carry unevaluated constants around?Can't we introduce a new enum, that is used in
Patkind::Constant
and includes variants forInlineConst(mir::Const, LocalDefId)
andmir::Const
instead of modelling inline constants at the pattern level? The visitor in thir unsafety checking could still detect inline constants, pattern checking should be able to work with this afaict and we could continue to encode pattern ranges with inline constants asPatKind::Range
directly, which seems less hacky than the nested PatKind::InlineConst imo.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.
As I said above
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.
Unsafety checking needs to look at the body of the inline constant, so if we want to use
Const
to carry it around, we must not evaluate it. Pretty sure an evaluated const doesn't know about the body it came from. This is what was done in ranges before I asked for this specialInlineConstant
behavior.We can't if we have an inline constant that evaluates to something that
const_to_pat
will deconstruct, e.g.:Here the constant needs to be deconstructed into a
Pat
that representsSome(0)
so that we can do proper exhaustiveness/unreachable checking with it. The resultingPat
might not have anyPatKind::Constant
left at the end. Hence why we attach the inline constant in the middle of the pattern.