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.
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.
11 #![feature(min_const_unsafe_fn)]
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 {} }
19 const fn foo8() -> i32 {
22 const fn foo9() -> *const String {
23 unsafe { foo5::<String>() }
25 const fn foo10() -> *const Vec<std::cell::Cell<u32>> {
26 unsafe { foo6::<Vec<std::cell::Cell<u32>>>() }
28 const unsafe fn foo8_3() -> i32 {
31 const unsafe fn foo9_3() -> *const String {
32 unsafe { foo5::<String>() }
34 const unsafe fn foo10_3() -> *const Vec<std::cell::Cell<u32>> {
35 unsafe { foo6::<Vec<std::cell::Cell<u32>>>() }
38 const unsafe fn foo8_2() -> i32 {
39 foo4() //~ ERROR not allowed in const fn
41 const unsafe fn foo9_2() -> *const String {
42 foo5::<String>() //~ ERROR not allowed in const fn
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
47 const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
48 //~^ dereferencing raw pointers in constant functions
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
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