-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Test validity of pattern types #136145
base: master
Are you sure you want to change the base?
Test validity of pattern types #136145
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
Surely patterns can express more than what fits into So some tests that probably still fail are:
|
Those patterns are not supported yet and will just error out in ast lowering, never reaching the type system |
What exactly is the full set of patterns supported currently? And how can we increase the probability that when that set gets expanded, the validity check is updated accordingly? |
The validity check automatically handles everything, because it looks at the layout of pattern types. The problem will be once we can't represent something in the layout anymore. I could add an exhaustive match with a comment on the range pattern saying it's fully handled by the layout logic |
d9865d7
to
2ed0e74
Compare
Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri Some changes occurred to the CTFE machinery cc @rust-lang/wg-const-eval |
2ed0e74
to
cb79d1b
Compare
done |
Ah... I need to check nesting pattern types within each other. And for that we need to actually recurse into the base type. |
HIR ty lowering was modified cc @fmease |
This comment has been minimized.
This comment has been minimized.
de6c522
to
6697731
Compare
Ok, I think I rejected everything that is relevant for validation and added tests for the rest |
d743801
to
bd1a54c
Compare
// tests/ui/type/pattern_types/validity.rs | ||
match **pat { | ||
// Range patterns are handled fully by looking at the layout | ||
ty::PatternKind::Range { .. } => {}, |
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.
Seems worth asserting that layout.backend_repr
is indeed Scalar
... skipping the recursion here is a bit dubious, we don't do this for anything else.
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.
In particular, I think this means that e.g. if a u32
range pattern type is uninitialized, or a char
range pattern type is not a valid char
, we skip the nice error that try_visit_primitive
would generate, and instead show a more generic error in visit_scalar
.
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.
Hmm tried recursing, but it doesn't actually do anything better
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.
Can you add char
cases to pattern_types/validity.rs
, one case where the character is valid but outside the range and one case where it's not even a valid char
?
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.
That did it, yea
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.
So that test does show a different error with the extra call to visit_value
? Great :)
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.
Yes
@rustbot author |
72dea42
to
640d948
Compare
This comment has been minimized.
This comment has been minimized.
640d948
to
b5d3029
Compare
This comment has been minimized.
This comment has been minimized.
b5d3029
to
c17eee6
Compare
@rustbot ready |
Please avoid rebasing when there is no conflict; now I can't see what the diff is to my last review. |
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 seems like it should be known-bug
? We surely don't want to see these ICE reports, right?^^
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.
it is already, just with no issue open, so known-bug: unknown
There were diffs that confused me, so I thought rebasing would solve them, but it turned out it was diffs in the LOC of the panic location. So now I filtered out those. |
c17eee6
to
7e4ccc2
Compare
@rustbot ready |
@@ -1240,6 +1240,17 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValueVisitor<'tcx, M> for ValidityVisitor<'rt, | |||
self.visit_field(val, 0, &self.ecx.project_index(val, 0)?)?; | |||
} | |||
} | |||
ty::Pat(base, pat) => { | |||
// First check that the base type is valid | |||
self.visit_value(&val.transmute(self.ecx.layout_of(*base)?, self.ecx)?)?; |
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.
Is there a field projection for this? Or is transmute the way this is meant to be done?
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.
There is no field projection. Using a field like representation is what we want to get rid of with pattern types to avoid all the pains we have with the rustc_scalar_layout attributes. We could add something to the validation path to show that we're validating the base type, but there's no projection for it, just like niche optimized enums have no projection for the discriminant really
r=me with CI green, assuming that there is indeed no field projection. |
@bors r=RalfJung |
r? @RalfJung
pulled out of #136006 so we don't have to rely on libcore types excercising this code path
There's nothing to fix.
rustc_layout_scalar_valid_range_start
structs just failed their validation on their value instead of their fields' value, causing a diff where moving to pattern types adds an additional.0
field access to the validation error