diff options
| author | Ralf Jung <post@ralfj.de> | 2023-10-28 11:05:15 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2023-10-28 11:20:12 +0200 |
| commit | 9b5b4dde92921275b9629a02132e2139d47a954b (patch) | |
| tree | 89ce32ba47562df853a83ea0b809d66b2a273926 /src | |
| parent | 6212cc690762057f630c20806926432fc4978bac (diff) | |
| download | rust-9b5b4dde92921275b9629a02132e2139d47a954b.tar.gz rust-9b5b4dde92921275b9629a02132e2139d47a954b.zip | |
consolidate and extend testing for _ patterns discarding the place
Diffstat (limited to 'src')
| -rw-r--r-- | src/tools/miri/tests/pass/dangling_pointer_deref_match_underscore.rs | 14 | ||||
| -rw-r--r-- | src/tools/miri/tests/pass/underscore_pattern.rs | 76 | ||||
| -rw-r--r-- | src/tools/miri/tests/pass/underscore_pattern.stdout (renamed from src/tools/miri/tests/pass/union-uninhabited-match-underscore.stdout) | 0 | ||||
| -rw-r--r-- | src/tools/miri/tests/pass/union-uninhabited-match-underscore.rs | 17 |
4 files changed, 76 insertions, 31 deletions
diff --git a/src/tools/miri/tests/pass/dangling_pointer_deref_match_underscore.rs b/src/tools/miri/tests/pass/dangling_pointer_deref_match_underscore.rs deleted file mode 100644 index c3cff1f4280..00000000000 --- a/src/tools/miri/tests/pass/dangling_pointer_deref_match_underscore.rs +++ /dev/null @@ -1,14 +0,0 @@ -// A `_` binding in a match is a nop, so we do not detect that the pointer is dangling. -//@compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation - -fn main() { - let p = { - let b = Box::new(42); - &*b as *const i32 - }; - unsafe { - match *p { - _ => {} - } - } -} diff --git a/src/tools/miri/tests/pass/underscore_pattern.rs b/src/tools/miri/tests/pass/underscore_pattern.rs new file mode 100644 index 00000000000..f9b42c5bc8e --- /dev/null +++ b/src/tools/miri/tests/pass/underscore_pattern.rs @@ -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 diff --git a/src/tools/miri/tests/pass/union-uninhabited-match-underscore.stdout b/src/tools/miri/tests/pass/underscore_pattern.stdout index ff731696f01..ff731696f01 100644 --- a/src/tools/miri/tests/pass/union-uninhabited-match-underscore.stdout +++ b/src/tools/miri/tests/pass/underscore_pattern.stdout diff --git a/src/tools/miri/tests/pass/union-uninhabited-match-underscore.rs b/src/tools/miri/tests/pass/union-uninhabited-match-underscore.rs deleted file mode 100644 index 33db9c2d347..00000000000 --- a/src/tools/miri/tests/pass/union-uninhabited-match-underscore.rs +++ /dev/null @@ -1,17 +0,0 @@ -fn main() { - #[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!"), - } - } -} |
