Skip to content

Commit cb79d1b

Browse files
committed
Test validity of pattern types
1 parent 633a3fe commit cb79d1b

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,14 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValueVisitor<'tcx, M> for ValidityVisitor<'rt,
12401240
self.visit_field(val, 0, &self.ecx.project_index(val, 0)?)?;
12411241
}
12421242
}
1243+
ty::Pat(_base, pat) => {
1244+
// When you extend this match, make sure to also add tests to
1245+
// tests/ui/type/pattern_types/validity.rs
1246+
match **pat {
1247+
// Range patterns are handled fully by looking at the layout
1248+
ty::PatternKind::Range { .. } => {},
1249+
}
1250+
}
12431251
_ => {
12441252
// default handler
12451253
try_validation!(
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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_PTR: pattern_type!(usize is 1..) = unsafe { std::mem::transmute(&42) };
12+
//~^ ERROR: evaluation of constant value failed
13+
14+
const BAD_AGGREGATE: (pattern_type!(u32 is 1..), u32) = (unsafe { std::mem::transmute(0) }, 0);
15+
//~^ ERROR: it is undefined behavior to use this value
16+
17+
struct Foo(Bar);
18+
struct Bar(pattern_type!(u32 is 1..));
19+
20+
const BAD_FOO: Foo = Foo(Bar(unsafe { std::mem::transmute(0) }));
21+
//~^ ERROR: it is undefined behavior to use this value
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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_PTR: pattern_type!(usize is 1..) = unsafe { std::mem::transmute(&42) };
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into integer
17+
|
18+
= help: this code performed an operation that depends on the underlying bytes representing a pointer
19+
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
20+
21+
error[E0080]: it is undefined behavior to use this value
22+
--> $DIR/validity.rs:14:1
23+
|
24+
LL | const BAD_AGGREGATE: (pattern_type!(u32 is 1..), u32) = (unsafe { std::mem::transmute(0) }, 0);
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered 0, but expected something greater or equal to 1
26+
|
27+
= 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.
28+
= note: the raw bytes of the constant (size: 8, align: 4) {
29+
00 00 00 00 00 00 00 00 │ ........
30+
}
31+
32+
error[E0080]: it is undefined behavior to use this value
33+
--> $DIR/validity.rs:20:1
34+
|
35+
LL | const BAD_FOO: Foo = Foo(Bar(unsafe { std::mem::transmute(0) }));
36+
| ^^^^^^^^^^^^^^^^^^ constructing invalid value at .0.0: encountered 0, but expected something greater or equal to 1
37+
|
38+
= 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.
39+
= note: the raw bytes of the constant (size: 4, align: 4) {
40+
00 00 00 00 │ ....
41+
}
42+
43+
error: aborting due to 4 previous errors
44+
45+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)