Skip to content

Commit ded0836

Browse files
committed
Check the base type of pattern types for validity first
1 parent 2f2b32b commit ded0836

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//! That's useful because it means other passes (e.g. promotion) can rely on `const`s
55
//! to be const-safe.
66
7-
use std::assert_matches::assert_matches;
87
use std::borrow::Cow;
98
use std::fmt::Write;
109
use std::hash::Hash;
@@ -1241,15 +1240,15 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValueVisitor<'tcx, M> for ValidityVisitor<'rt,
12411240
self.visit_field(val, 0, &self.ecx.project_index(val, 0)?)?;
12421241
}
12431242
}
1244-
ty::Pat(_base, pat) => {
1243+
ty::Pat(base, pat) => {
1244+
// First check that the base type is valid
1245+
self.visit_value(&val.transmute(self.ecx.layout_of(*base)?, self.ecx)?)?;
12451246
// When you extend this match, make sure to also add tests to
12461247
// tests/ui/type/pattern_types/validity.rs((
12471248
match **pat {
12481249
// Range patterns are precisely reflected into `valid_range` and thus
12491250
// handled fully by `visit_scalar` (called below).
1250-
ty::PatternKind::Range { .. } => {
1251-
assert_matches!(val.layout.backend_repr, BackendRepr::Scalar(_));
1252-
},
1251+
ty::PatternKind::Range { .. } => {},
12531252
}
12541253
}
12551254
_ => {

tests/ui/type/pattern_types/validity.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ error[E0080]: it is undefined behavior to use this value
6767
--> $DIR/validity.rs:34:1
6868
|
6969
LL | const CHAR_OOB: pattern_type!(char is 'A'..'Z') = unsafe { std::mem::transmute(u32::MAX) };
70-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 4294967295, but expected something in the range 65..=89
70+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0xffffffff, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
7171
|
7272
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
7373
= note: the raw bytes of the constant (size: 4, align: 4) {

0 commit comments

Comments
 (0)