]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/min_const_fn/min_const_fn_unsafe.rs
7a84992e14b36c7b520173fcfffe8e15f5afe7d1
[rust.git] / src / test / ui / consts / min_const_fn / min_const_fn_unsafe.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 // gate-test-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 // not ok
20 const fn foo8() -> i32 {
21     unsafe { foo4() } //~ ERROR unsafe operations are not allowed in const fn
22 }
23 const fn foo9() -> *const String {
24     unsafe { foo5::<String>() } //~ ERROR unsafe operations are not allowed in const fn
25 }
26 const fn foo10() -> *const Vec<std::cell::Cell<u32>> {
27     unsafe { foo6::<Vec<std::cell::Cell<u32>>>() } //~ ERROR not allowed in const fn
28 }
29 const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
30 //~^ dereferencing raw pointers in constant functions
31
32 fn main() {}
33
34 const unsafe fn no_union() {
35     union Foo { x: (), y: () }
36     Foo { x: () }.y //~ ERROR not allowed in const fn
37     //~^ unions in const fn
38 }