Skip to content

Commit 2555e07

Browse files
authored
Rollup merge of #78428 - JulianKnodt:invalid_patterns, r=lcnr
MinConstGenerics UI test for invalid values for bool & char This adds a test for `feature(min_const_generics)` with some invalid values for bools and chars and ensures that they do not ICE and error with understandable messages. r? @lcnr
2 parents 270d2e0 + eb8e8bd commit 2555e07

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#![feature(min_const_generics)]
2+
use std::mem::transmute;
3+
4+
fn get_flag<const FlagSet: bool, const ShortName: char>() -> Option<char> {
5+
if FlagSet {
6+
Some(ShortName)
7+
} else {
8+
None
9+
}
10+
}
11+
12+
union CharRaw {
13+
byte: u8,
14+
character: char,
15+
}
16+
17+
union BoolRaw {
18+
byte: u8,
19+
boolean: bool,
20+
}
21+
22+
const char_raw: CharRaw = CharRaw { byte: 0xFF };
23+
const bool_raw: BoolRaw = BoolRaw { byte: 0x42 };
24+
25+
fn main() {
26+
// Test that basic cases don't work
27+
assert!(get_flag::<true, 'c'>().is_some());
28+
assert!(get_flag::<false, 'x'>().is_none());
29+
get_flag::<false, 0xFF>();
30+
//~^ ERROR mismatched types
31+
get_flag::<7, 'c'>();
32+
//~^ ERROR mismatched types
33+
get_flag::<42, 0x5ad>();
34+
//~^ ERROR mismatched types
35+
//~| ERROR mismatched types
36+
37+
38+
get_flag::<false, { unsafe { char_raw.character } }>();
39+
//~^ ERROR it is undefined behavior
40+
get_flag::<{ unsafe { bool_raw.boolean } }, 'z'>();
41+
//~^ ERROR it is undefined behavior
42+
get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
43+
//~^ ERROR it is undefined behavior
44+
//~| ERROR it is undefined behavior
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/invalid-patterns.rs:29:21
3+
|
4+
LL | get_flag::<false, 0xFF>();
5+
| ^^^^ expected `char`, found `u8`
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/invalid-patterns.rs:31:14
9+
|
10+
LL | get_flag::<7, 'c'>();
11+
| ^ expected `bool`, found integer
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/invalid-patterns.rs:33:14
15+
|
16+
LL | get_flag::<42, 0x5ad>();
17+
| ^^ expected `bool`, found integer
18+
19+
error[E0308]: mismatched types
20+
--> $DIR/invalid-patterns.rs:33:18
21+
|
22+
LL | get_flag::<42, 0x5ad>();
23+
| ^^^^^ expected `char`, found `u8`
24+
25+
error[E0080]: it is undefined behavior to use this value
26+
--> $DIR/invalid-patterns.rs:38:21
27+
|
28+
LL | get_flag::<false, { unsafe { char_raw.character } }>();
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
30+
|
31+
= 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.
32+
33+
error[E0080]: it is undefined behavior to use this value
34+
--> $DIR/invalid-patterns.rs:40:14
35+
|
36+
LL | get_flag::<{ unsafe { bool_raw.boolean } }, 'z'>();
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
38+
|
39+
= 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.
40+
41+
error[E0080]: it is undefined behavior to use this value
42+
--> $DIR/invalid-patterns.rs:42:14
43+
|
44+
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
45+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
46+
|
47+
= 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.
48+
49+
error[E0080]: it is undefined behavior to use this value
50+
--> $DIR/invalid-patterns.rs:42:47
51+
|
52+
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
54+
|
55+
= 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.
56+
57+
error: aborting due to 8 previous errors
58+
59+
Some errors have detailed explanations: E0080, E0308.
60+
For more information about an error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)