]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/min_const_fn/min_const_fn_unsafe_feature_gate.rs
b66460d64089b4ce1c93fae4e9d55849bce625de
[rust.git] / src / test / ui / consts / min_const_fn / min_const_fn_unsafe_feature_gate.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 #![feature(min_const_unsafe_fn)]
12
13 // ok
14 const unsafe fn foo4() -> i32 { 42 }
15 const unsafe fn foo5<T>() -> *const T { 0 as *const T }
16 const unsafe fn foo6<T>() -> *mut T { 0 as *mut T }
17 const fn no_unsafe() { unsafe {} }
18
19 const fn foo8() -> i32 {
20     unsafe { foo4() }
21 }
22 const fn foo9() -> *const String {
23     unsafe { foo5::<String>() }
24 }
25 const fn foo10() -> *const Vec<std::cell::Cell<u32>> {
26     unsafe { foo6::<Vec<std::cell::Cell<u32>>>() }
27 }
28 const unsafe fn foo8_3() -> i32 {
29     unsafe { foo4() }
30 }
31 const unsafe fn foo9_3() -> *const String {
32     unsafe { foo5::<String>() }
33 }
34 const unsafe fn foo10_3() -> *const Vec<std::cell::Cell<u32>> {
35     unsafe { foo6::<Vec<std::cell::Cell<u32>>>() }
36 }
37 // not ok
38 const unsafe fn foo8_2() -> i32 {
39     foo4() //~ ERROR not allowed in const fn
40 }
41 const unsafe fn foo9_2() -> *const String {
42     foo5::<String>() //~ ERROR not allowed in const fn
43 }
44 const unsafe fn foo10_2() -> *const Vec<std::cell::Cell<u32>> {
45     foo6::<Vec<std::cell::Cell<u32>>>() //~ ERROR not allowed in const fn
46 }
47 const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
48 //~^ dereferencing raw pointers in constant functions
49
50 const unsafe fn foo30_4(x: *mut usize) -> &'static usize { &*x } //~ ERROR not allowed in const fn
51 //~^ dereferencing raw pointers in constant functions
52
53 fn main() {}
54
55 const unsafe fn no_union() {
56     union Foo { x: (), y: () }
57     Foo { x: () }.y //~ ERROR not allowed in const fn
58     //~^ unions in const fn
59 }