]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/panic-uninitialized-zeroed.rs
New upstream version 1.34.2+dfsg1
[rustc.git] / src / test / run-pass / panic-uninitialized-zeroed.rs
CommitLineData
0bf4aa26
XL
1// ignore-wasm32-bare always compiled as panic=abort right now and this requires unwinding
2// This test checks that instantiating an uninhabited type via `mem::{uninitialized,zeroed}` results
3// in a runtime panic.
4
0731742a 5#![feature(never_type, maybe_uninit)]
0bf4aa26
XL
6
7use std::{mem, panic};
8
9#[allow(dead_code)]
10struct Foo {
11 x: u8,
12 y: !,
13}
14
15enum Bar {}
16
17fn main() {
18 unsafe {
19 assert_eq!(
20 panic::catch_unwind(|| {
21 mem::uninitialized::<!>()
22 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
0731742a 23 s == "Attempted to instantiate uninhabited type !"
0bf4aa26
XL
24 })),
25 Some(true)
26 );
27
28 assert_eq!(
29 panic::catch_unwind(|| {
30 mem::zeroed::<!>()
31 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
0731742a
XL
32 s == "Attempted to instantiate uninhabited type !"
33 })),
34 Some(true)
35 );
36
37 assert_eq!(
38 panic::catch_unwind(|| {
9fa01778 39 mem::MaybeUninit::<!>::uninitialized().into_initialized()
0731742a
XL
40 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
41 s == "Attempted to instantiate uninhabited type !"
0bf4aa26
XL
42 })),
43 Some(true)
44 );
45
46 assert_eq!(
47 panic::catch_unwind(|| {
48 mem::uninitialized::<Foo>()
49 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
0731742a 50 s == "Attempted to instantiate uninhabited type Foo"
0bf4aa26
XL
51 })),
52 Some(true)
53 );
54
55 assert_eq!(
56 panic::catch_unwind(|| {
57 mem::zeroed::<Foo>()
58 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
0731742a
XL
59 s == "Attempted to instantiate uninhabited type Foo"
60 })),
61 Some(true)
62 );
63
64 assert_eq!(
65 panic::catch_unwind(|| {
9fa01778 66 mem::MaybeUninit::<Foo>::uninitialized().into_initialized()
0731742a
XL
67 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
68 s == "Attempted to instantiate uninhabited type Foo"
0bf4aa26
XL
69 })),
70 Some(true)
71 );
72
73 assert_eq!(
74 panic::catch_unwind(|| {
75 mem::uninitialized::<Bar>()
76 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
0731742a 77 s == "Attempted to instantiate uninhabited type Bar"
0bf4aa26
XL
78 })),
79 Some(true)
80 );
81
82 assert_eq!(
83 panic::catch_unwind(|| {
84 mem::zeroed::<Bar>()
85 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
0731742a
XL
86 s == "Attempted to instantiate uninhabited type Bar"
87 })),
88 Some(true)
89 );
90
91 assert_eq!(
92 panic::catch_unwind(|| {
9fa01778 93 mem::MaybeUninit::<Bar>::uninitialized().into_initialized()
0731742a
XL
94 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
95 s == "Attempted to instantiate uninhabited type Bar"
0bf4aa26
XL
96 })),
97 Some(true)
98 );
99 }
100}