Check the base type of pattern types for validity first

This commit is contained in:
Oli Scherer 2025-02-02 17:54:35 +00:00
parent 2f2b32b84e
commit ded0836c18
2 changed files with 5 additions and 6 deletions

View file

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

View file

@ -67,7 +67,7 @@ error[E0080]: it is undefined behavior to use this value
--> $DIR/validity.rs:34:1 --> $DIR/validity.rs:34:1
| |
LL | const CHAR_OOB: pattern_type!(char is 'A'..'Z') = unsafe { std::mem::transmute(u32::MAX) }; LL | const CHAR_OOB: pattern_type!(char is 'A'..'Z') = unsafe { std::mem::transmute(u32::MAX) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 4294967295, but expected something in the range 65..=89 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0xffffffff, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
| |
= 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. = 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.
= note: the raw bytes of the constant (size: 4, align: 4) { = note: the raw bytes of the constant (size: 4, align: 4) {