]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/panic-uninitialized-zeroed.rs
improve the run-pass test
[rust.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 // This test checks that instantiating an uninhabited type via `mem::{uninitialized,zeroed}` results
12 // in a runtime panic.
13
14 #![feature(never_type)]
15
16 use std::{mem, panic};
17
18 struct Foo {
19     x: u8,
20     y: !,
21 }
22
23 fn main() {
24     unsafe {
25         assert_eq!(
26             panic::catch_unwind(|| {
27                 mem::uninitialized::<!>()
28             }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
29                 s == "Attempted to instantiate uninhabited type ! using mem::uninitialized"
30             })),
31             Some(true)
32         );
33
34         assert_eq!(
35             panic::catch_unwind(|| {
36                 mem::zeroed::<!>()
37             }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
38                 s == "Attempted to instantiate uninhabited type ! using mem::zeroed"
39             })),
40             Some(true)
41         );
42
43         assert_eq!(
44             panic::catch_unwind(|| {
45                 mem::uninitialized::<Foo>()
46             }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
47                 s == "Attempted to instantiate uninhabited type Foo using mem::uninitialized"
48             })),
49             Some(true)
50         );
51
52         assert_eq!(
53             panic::catch_unwind(|| {
54                 mem::zeroed::<Foo>()
55             }).err().and_then(|a| a.downcast_ref::<String>().map(|s| {
56                 s == "Attempted to instantiate uninhabited type Foo using mem::zeroed"
57             })),
58             Some(true)
59         );
60     }
61 }