1
Fork 0

Test validity of pattern types

This commit is contained in:
Oli Scherer 2025-01-27 16:36:45 +00:00
parent e1f09207fb
commit 2f2b32b84e
3 changed files with 128 additions and 0 deletions

View file

@ -4,6 +4,7 @@
//! That's useful because it means other passes (e.g. promotion) can rely on `const`s
//! to be const-safe.
use std::assert_matches::assert_matches;
use std::borrow::Cow;
use std::fmt::Write;
use std::hash::Hash;
@ -1240,6 +1241,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) => {
// When you extend this match, make sure to also add tests to
// tests/ui/type/pattern_types/validity.rs((
match **pat {
// Range patterns are precisely reflected into `valid_range` and thus
// handled fully by `visit_scalar` (called below).
ty::PatternKind::Range { .. } => {
assert_matches!(val.layout.backend_repr, BackendRepr::Scalar(_));
},
}
}
_ => {
// default handler
try_validation!(

View file

@ -0,0 +1,37 @@
//! Check that pattern types have their validity checked
#![feature(pattern_types)]
#![feature(pattern_type_macro)]
use std::pat::pattern_type;
const BAD: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(0) };
//~^ ERROR: it is undefined behavior to use this value
const BAD_UNINIT: pattern_type!(u32 is 1..) =
//~^ ERROR: evaluation of constant value failed
unsafe { std::mem::transmute(std::mem::MaybeUninit::<u32>::uninit()) };
const BAD_PTR: pattern_type!(usize is 1..) = unsafe { std::mem::transmute(&42) };
//~^ ERROR: evaluation of constant value failed
const BAD_AGGREGATE: (pattern_type!(u32 is 1..), u32) = (unsafe { std::mem::transmute(0) }, 0);
//~^ ERROR: it is undefined behavior to use this value
struct Foo(Bar);
struct Bar(pattern_type!(u32 is 1..));
const BAD_FOO: Foo = Foo(Bar(unsafe { std::mem::transmute(0) }));
//~^ ERROR: it is undefined behavior to use this value
const CHAR_UNINIT: pattern_type!(char is 'A'..'Z') =
//~^ ERROR: evaluation of constant value failed
unsafe { std::mem::transmute(std::mem::MaybeUninit::<u32>::uninit()) };
const CHAR_OOB_PAT: pattern_type!(char is 'A'..'Z') = unsafe { std::mem::transmute('a') };
//~^ ERROR: it is undefined behavior to use this value
const CHAR_OOB: pattern_type!(char is 'A'..'Z') = unsafe { std::mem::transmute(u32::MAX) };
//~^ ERROR: it is undefined behavior to use this value
fn main() {}

View file

@ -0,0 +1,79 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/validity.rs:8:1
|
LL | const BAD: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0, but expected something greater or equal to 1
|
= 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) {
00 00 00 00 │ ....
}
error[E0080]: evaluation of constant value failed
--> $DIR/validity.rs:11:1
|
LL | const BAD_UNINIT: pattern_type!(u32 is 1..) =
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
error[E0080]: evaluation of constant value failed
--> $DIR/validity.rs:15:1
|
LL | const BAD_PTR: pattern_type!(usize is 1..) = unsafe { std::mem::transmute(&42) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into integer
|
= help: this code performed an operation that depends on the underlying bytes representing a pointer
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
error[E0080]: it is undefined behavior to use this value
--> $DIR/validity.rs:18:1
|
LL | const BAD_AGGREGATE: (pattern_type!(u32 is 1..), u32) = (unsafe { std::mem::transmute(0) }, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered 0, but expected something greater or equal to 1
|
= 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: 8, align: 4) {
00 00 00 00 00 00 00 00 │ ........
}
error[E0080]: it is undefined behavior to use this value
--> $DIR/validity.rs:24:1
|
LL | const BAD_FOO: Foo = Foo(Bar(unsafe { std::mem::transmute(0) }));
| ^^^^^^^^^^^^^^^^^^ constructing invalid value at .0.0: encountered 0, but expected something greater or equal to 1
|
= 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) {
00 00 00 00 │ ....
}
error[E0080]: evaluation of constant value failed
--> $DIR/validity.rs:27:1
|
LL | const CHAR_UNINIT: pattern_type!(char is 'A'..'Z') =
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
error[E0080]: it is undefined behavior to use this value
--> $DIR/validity.rs:31:1
|
LL | const CHAR_OOB_PAT: pattern_type!(char is 'A'..'Z') = unsafe { std::mem::transmute('a') };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 97, but expected something in the range 65..=89
|
= 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) {
61 00 00 00 │ a...
}
error[E0080]: it is undefined behavior to use this value
--> $DIR/validity.rs:34:1
|
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
|
= 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) {
ff ff ff ff │ ....
}
error: aborting due to 8 previous errors
For more information about this error, try `rustc --explain E0080`.