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