Skip to content

Commit 98ce6d1

Browse files
committed
Auto merge of #3148 - RalfJung:underscore-patterns, r=RalfJung
Underscore pattern tests
2 parents 111a410 + 507fc23 commit 98ce6d1

7 files changed

+121
-17
lines changed

tests/pass/union-uninhabited-match-underscore.rs renamed to tests/fail/validity/match_binder_checks_validity1.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ fn main() {
88
unsafe {
99
let x: Uninit<Void> = Uninit { uninit: () };
1010
match x.value {
11-
// rustc warns about un unreachable pattern,
12-
// but is wrong in unsafe code.
1311
#[allow(unreachable_patterns)]
14-
_ => println!("hi from the void!"),
12+
_x => println!("hi from the void!"), //~ERROR: invalid value
1513
}
1614
}
1715
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: constructing invalid value: encountered a value of uninhabited type `main::Void`
2+
--> $DIR/match_binder_checks_validity1.rs:LL:CC
3+
|
4+
LL | _x => println!("hi from the void!"),
5+
| ^^ constructing invalid value: encountered a value of uninhabited type `main::Void`
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/match_binder_checks_validity1.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to previous error
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fn main() {
2+
#[derive(Copy, Clone)]
3+
union Uninit<T: Copy> {
4+
value: T,
5+
uninit: u8,
6+
}
7+
unsafe {
8+
let x: Uninit<bool> = Uninit { uninit: 3 };
9+
match x.value {
10+
#[allow(unreachable_patterns)]
11+
_x => println!("hi from the void!"), //~ERROR: invalid value
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: constructing invalid value: encountered 0x03, but expected a boolean
2+
--> $DIR/match_binder_checks_validity2.rs:LL:CC
3+
|
4+
LL | _x => println!("hi from the void!"),
5+
| ^^ constructing invalid value: encountered 0x03, but expected a boolean
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/match_binder_checks_validity2.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to previous error
15+

tests/pass/dangling_pointer_deref_match_underscore.rs

-14
This file was deleted.

tests/pass/underscore_pattern.rs

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Various tests ensuring that underscore patterns really just construct the place, but don't check its contents.
2+
#![feature(strict_provenance)]
3+
use std::ptr;
4+
5+
fn main() {
6+
dangling_deref_match();
7+
union_uninhabited_match();
8+
dangling_let();
9+
invalid_let();
10+
dangling_let_type_annotation();
11+
invalid_let_type_annotation();
12+
}
13+
14+
fn dangling_deref_match() {
15+
let p = {
16+
let b = Box::new(42);
17+
&*b as *const i32
18+
};
19+
unsafe {
20+
match *p {
21+
_ => {}
22+
}
23+
}
24+
}
25+
26+
fn union_uninhabited_match() {
27+
#[derive(Copy, Clone)]
28+
enum Void {}
29+
union Uninit<T: Copy> {
30+
value: T,
31+
uninit: (),
32+
}
33+
unsafe {
34+
let x: Uninit<Void> = Uninit { uninit: () };
35+
match x.value {
36+
// rustc warns about un unreachable pattern,
37+
// but is wrong in unsafe code.
38+
#[allow(unreachable_patterns)]
39+
_ => println!("hi from the void!"),
40+
}
41+
}
42+
}
43+
44+
fn dangling_let() {
45+
unsafe {
46+
let ptr = ptr::invalid::<bool>(0x40);
47+
let _ = *ptr;
48+
}
49+
}
50+
51+
fn invalid_let() {
52+
unsafe {
53+
let val = 3u8;
54+
let ptr = ptr::addr_of!(val).cast::<bool>();
55+
let _ = *ptr;
56+
}
57+
}
58+
59+
// Adding a type annotation used to change how MIR is generated, make sure we cover both cases.
60+
fn dangling_let_type_annotation() {
61+
unsafe {
62+
let ptr = ptr::invalid::<bool>(0x40);
63+
let _: bool = *ptr;
64+
}
65+
}
66+
67+
fn invalid_let_type_annotation() {
68+
unsafe {
69+
let val = 3u8;
70+
let ptr = ptr::addr_of!(val).cast::<bool>();
71+
let _: bool = *ptr;
72+
}
73+
}
74+
75+
// FIXME: we should also test `!`, not just `bool` -- but that s currently buggy:
76+
// https://github.com/rust-lang/rust/issues/117288

0 commit comments

Comments
 (0)