Skip to content

Commit 7c0ed54

Browse files
committed
Test validity of pattern types
1 parent e1f0920 commit 7c0ed54

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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;
78
use std::borrow::Cow;
89
use std::fmt::Write;
910
use std::hash::Hash;
@@ -1240,6 +1241,17 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValueVisitor<'tcx, M> for ValidityVisitor<'rt,
12401241
self.visit_field(val, 0, &self.ecx.project_index(val, 0)?)?;
12411242
}
12421243
}
1244+
ty::Pat(_base, pat) => {
1245+
// When you extend this match, make sure to also add tests to
1246+
// tests/ui/type/pattern_types/validity.rs((
1247+
match **pat {
1248+
// Range patterns are precisely reflected into `valid_range` and thus
1249+
// handled fully by `visit_scalar` (called below).
1250+
ty::PatternKind::Range { .. } => {
1251+
assert_matches!(val.layout.backend_repr, BackendRepr::Scalar(_));
1252+
},
1253+
}
1254+
}
12431255
_ => {
12441256
// default handler
12451257
try_validation!(
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! Check that pattern types have their validity checked
2+
3+
#![feature(pattern_types)]
4+
#![feature(pattern_type_macro)]
5+
6+
use std::pat::pattern_type;
7+
8+
const BAD: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(0) };
9+
//~^ ERROR: it is undefined behavior to use this value
10+
11+
const BAD_UNINIT: pattern_type!(u32 is 1..) =
12+
//~^ ERROR: evaluation of constant value failed
13+
unsafe { std::mem::transmute(std::mem::MaybeUninit::<u32>::uninit()) };
14+
15+
const BAD_PTR: pattern_type!(usize is 1..) = unsafe { std::mem::transmute(&42) };
16+
//~^ ERROR: evaluation of constant value failed
17+
18+
const BAD_AGGREGATE: (pattern_type!(u32 is 1..), u32) = (unsafe { std::mem::transmute(0) }, 0);
19+
//~^ ERROR: it is undefined behavior to use this value
20+
21+
struct Foo(Bar);
22+
struct Bar(pattern_type!(u32 is 1..));
23+
24+
const BAD_FOO: Foo = Foo(Bar(unsafe { std::mem::transmute(0) }));
25+
//~^ ERROR: it is undefined behavior to use this value
26+
27+
fn main() {}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/validity.rs:8:1
3+
|
4+
LL | const BAD: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(0) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0, but expected something greater or equal to 1
6+
|
7+
= 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.
8+
= note: the raw bytes of the constant (size: 4, align: 4) {
9+
00 00 00 00 │ ....
10+
}
11+
12+
error[E0080]: evaluation of constant value failed
13+
--> $DIR/validity.rs:11:1
14+
|
15+
LL | const BAD_UNINIT: pattern_type!(u32 is 1..) =
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
17+
18+
error[E0080]: evaluation of constant value failed
19+
--> $DIR/validity.rs:15:1
20+
|
21+
LL | const BAD_PTR: pattern_type!(usize is 1..) = unsafe { std::mem::transmute(&42) };
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into integer
23+
|
24+
= help: this code performed an operation that depends on the underlying bytes representing a pointer
25+
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
26+
27+
error[E0080]: it is undefined behavior to use this value
28+
--> $DIR/validity.rs:18:1
29+
|
30+
LL | const BAD_AGGREGATE: (pattern_type!(u32 is 1..), u32) = (unsafe { std::mem::transmute(0) }, 0);
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered 0, but expected something greater or equal to 1
32+
|
33+
= 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.
34+
= note: the raw bytes of the constant (size: 8, align: 4) {
35+
00 00 00 00 00 00 00 00 │ ........
36+
}
37+
38+
error[E0080]: it is undefined behavior to use this value
39+
--> $DIR/validity.rs:24:1
40+
|
41+
LL | const BAD_FOO: Foo = Foo(Bar(unsafe { std::mem::transmute(0) }));
42+
| ^^^^^^^^^^^^^^^^^^ constructing invalid value at .0.0: encountered 0, but expected something greater or equal to 1
43+
|
44+
= 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.
45+
= note: the raw bytes of the constant (size: 4, align: 4) {
46+
00 00 00 00 │ ....
47+
}
48+
49+
error: aborting due to 5 previous errors
50+
51+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)