]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-forbid-static-unsafe-interior.rs
doc/guide-ffi: A few minor typo/language fixes
[rust.git] / src / test / compile-fail / borrowck-forbid-static-unsafe-interior.rs
1 // Copyright 2014 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 // Verify that it is not possible to take the address of
12 // static items with usnafe interior.
13
14 use std::kinds::marker;
15 use std::ty::Unsafe;
16
17 struct MyUnsafe<T> {
18     value: Unsafe<T>
19 }
20
21 impl<T> MyUnsafe<T> {
22     fn forbidden(&self) {}
23 }
24
25 enum UnsafeEnum<T> {
26     VariantSafe,
27     VariantUnsafe(Unsafe<T>)
28 }
29
30 static STATIC1: UnsafeEnum<int> = VariantSafe;
31
32 static STATIC2: Unsafe<int> = Unsafe{value: 1, marker1: marker::InvariantType};
33 static STATIC3: MyUnsafe<int> = MyUnsafe{value: STATIC2};
34
35 static STATIC4: &'static Unsafe<int> = &'static STATIC2;
36 //~^ ERROR borrow of immutable static items with unsafe interior is not allowed
37
38 struct Wrap<T> {
39     value: T
40 }
41
42 static UNSAFE: Unsafe<int> = Unsafe{value: 1, marker1: marker::InvariantType};
43 static WRAPPED_UNSAFE: Wrap<&'static Unsafe<int>> = Wrap { value: &UNSAFE };
44 //~^ ERROR borrow of immutable static items with unsafe interior is not allowed
45
46 fn main() {
47     let a = &STATIC1;
48     //~^ ERROR borrow of immutable static items with unsafe interior is not allowed
49
50     STATIC3.forbidden()
51     //~^ ERROR borrow of immutable static items with unsafe interior is not allowed
52 }
53
54