]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/panic-uninitialized-zeroed.rs
New upstream version 1.31.0~beta.4+dfsg1
[rustc.git] / src / test / run-pass / panic-uninitialized-zeroed.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-wasm32-bare always compiled as panic=abort right now and this requires unwinding
12 // This test checks that instantiating an uninhabited type via `mem::{uninitialized,zeroed}` results
13 // in a runtime panic.
14
15 #![feature(never_type)]
16
17 use std::{mem, panic};
18
19 #[allow(dead_code)]
20 struct Foo {
21 x: u8,
22 y: !,
23 }
24
25 enum Bar {}
26
27 fn main() {
28 unsafe {
29 assert_eq!(
30 panic::catch_unwind(|| {
31 mem::uninitialized::<!>()
32 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
33 s == "Attempted to instantiate uninhabited type ! using mem::uninitialized"
34 })),
35 Some(true)
36 );
37
38 assert_eq!(
39 panic::catch_unwind(|| {
40 mem::zeroed::<!>()
41 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
42 s == "Attempted to instantiate uninhabited type ! using mem::zeroed"
43 })),
44 Some(true)
45 );
46
47 assert_eq!(
48 panic::catch_unwind(|| {
49 mem::uninitialized::<Foo>()
50 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
51 s == "Attempted to instantiate uninhabited type Foo using mem::uninitialized"
52 })),
53 Some(true)
54 );
55
56 assert_eq!(
57 panic::catch_unwind(|| {
58 mem::zeroed::<Foo>()
59 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
60 s == "Attempted to instantiate uninhabited type Foo using mem::zeroed"
61 })),
62 Some(true)
63 );
64
65 assert_eq!(
66 panic::catch_unwind(|| {
67 mem::uninitialized::<Bar>()
68 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
69 s == "Attempted to instantiate uninhabited type Bar using mem::uninitialized"
70 })),
71 Some(true)
72 );
73
74 assert_eq!(
75 panic::catch_unwind(|| {
76 mem::zeroed::<Bar>()
77 }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
78 s == "Attempted to instantiate uninhabited type Bar using mem::zeroed"
79 })),
80 Some(true)
81 );
82 }
83 }