-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #3148 - RalfJung:underscore-patterns, r=RalfJung
Underscore pattern tests
- Loading branch information
Showing
7 changed files
with
121 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: Undefined Behavior: constructing invalid value: encountered a value of uninhabited type `main::Void` | ||
--> $DIR/match_binder_checks_validity1.rs:LL:CC | ||
| | ||
LL | _x => println!("hi from the void!"), | ||
| ^^ constructing invalid value: encountered a value of uninhabited type `main::Void` | ||
| | ||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
= note: BACKTRACE: | ||
= note: inside `main` at $DIR/match_binder_checks_validity1.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
fn main() { | ||
#[derive(Copy, Clone)] | ||
union Uninit<T: Copy> { | ||
value: T, | ||
uninit: u8, | ||
} | ||
unsafe { | ||
let x: Uninit<bool> = Uninit { uninit: 3 }; | ||
match x.value { | ||
#[allow(unreachable_patterns)] | ||
_x => println!("hi from the void!"), //~ERROR: invalid value | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: Undefined Behavior: constructing invalid value: encountered 0x03, but expected a boolean | ||
--> $DIR/match_binder_checks_validity2.rs:LL:CC | ||
| | ||
LL | _x => println!("hi from the void!"), | ||
| ^^ constructing invalid value: encountered 0x03, but expected a boolean | ||
| | ||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior | ||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information | ||
= note: BACKTRACE: | ||
= note: inside `main` at $DIR/match_binder_checks_validity2.rs:LL:CC | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to previous error | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Various tests ensuring that underscore patterns really just construct the place, but don't check its contents. | ||
#![feature(strict_provenance)] | ||
use std::ptr; | ||
|
||
fn main() { | ||
dangling_deref_match(); | ||
union_uninhabited_match(); | ||
dangling_let(); | ||
invalid_let(); | ||
dangling_let_type_annotation(); | ||
invalid_let_type_annotation(); | ||
} | ||
|
||
fn dangling_deref_match() { | ||
let p = { | ||
let b = Box::new(42); | ||
&*b as *const i32 | ||
}; | ||
unsafe { | ||
match *p { | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
fn union_uninhabited_match() { | ||
#[derive(Copy, Clone)] | ||
enum Void {} | ||
union Uninit<T: Copy> { | ||
value: T, | ||
uninit: (), | ||
} | ||
unsafe { | ||
let x: Uninit<Void> = Uninit { uninit: () }; | ||
match x.value { | ||
// rustc warns about un unreachable pattern, | ||
// but is wrong in unsafe code. | ||
#[allow(unreachable_patterns)] | ||
_ => println!("hi from the void!"), | ||
} | ||
} | ||
} | ||
|
||
fn dangling_let() { | ||
unsafe { | ||
let ptr = ptr::invalid::<bool>(0x40); | ||
let _ = *ptr; | ||
} | ||
} | ||
|
||
fn invalid_let() { | ||
unsafe { | ||
let val = 3u8; | ||
let ptr = ptr::addr_of!(val).cast::<bool>(); | ||
let _ = *ptr; | ||
} | ||
} | ||
|
||
// Adding a type annotation used to change how MIR is generated, make sure we cover both cases. | ||
fn dangling_let_type_annotation() { | ||
unsafe { | ||
let ptr = ptr::invalid::<bool>(0x40); | ||
let _: bool = *ptr; | ||
} | ||
} | ||
|
||
fn invalid_let_type_annotation() { | ||
unsafe { | ||
let val = 3u8; | ||
let ptr = ptr::addr_of!(val).cast::<bool>(); | ||
let _: bool = *ptr; | ||
} | ||
} | ||
|
||
// FIXME: we should also test `!`, not just `bool` -- but that s currently buggy: | ||
// https://github.com/rust-lang/rust/issues/117288 |
File renamed without changes.