|
| 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