]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/panic-uninitialized-zeroed.rs
Rollup merge of #61499 - varkor:issue-53457, r=oli-obk
[rust.git] / src / test / run-pass / panic-uninitialized-zeroed.rs
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
5 #![feature(never_type)]
6
7 use std::{mem, panic};
8
9 #[allow(dead_code)]
10 struct Foo {
11     x: u8,
12     y: !,
13 }
14
15 enum Bar {}
16
17 fn main() {
18     unsafe {
19         assert_eq!(
20             panic::catch_unwind(|| {
21                 mem::uninitialized::<!>()
22             }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
23                 s == "Attempted to instantiate uninhabited type !"
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| {
32                 s == "Attempted to instantiate uninhabited type !"
33             })),
34             Some(true)
35         );
36
37         assert_eq!(
38             panic::catch_unwind(|| {
39                 mem::MaybeUninit::<!>::uninit().assume_init()
40             }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
41                 s == "Attempted to instantiate uninhabited type !"
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| {
50                 s == "Attempted to instantiate uninhabited type Foo"
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| {
59                 s == "Attempted to instantiate uninhabited type Foo"
60             })),
61             Some(true)
62         );
63
64         assert_eq!(
65             panic::catch_unwind(|| {
66                 mem::MaybeUninit::<Foo>::uninit().assume_init()
67             }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
68                 s == "Attempted to instantiate uninhabited type Foo"
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| {
77                 s == "Attempted to instantiate uninhabited type Bar"
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| {
86                 s == "Attempted to instantiate uninhabited type Bar"
87             })),
88             Some(true)
89         );
90
91         assert_eq!(
92             panic::catch_unwind(|| {
93                 mem::MaybeUninit::<Bar>::uninit().assume_init()
94             }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
95                 s == "Attempted to instantiate uninhabited type Bar"
96             })),
97             Some(true)
98         );
99     }
100 }