Allow calling const unsafe fn in const fn behind a feature gate
This commit is contained in:
parent
91d5d56c00
commit
f2ae7b78d6
9 changed files with 194 additions and 37 deletions
|
|
@ -8,6 +8,8 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// gate-test-min_const_unsafe_fn
|
||||
|
||||
// ok
|
||||
const unsafe fn foo4() -> i32 { 42 }
|
||||
const unsafe fn foo5<T>() -> *const T { 0 as *const T }
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
|
||||
--> $DIR/min_const_fn_unsafe.rs:27:51
|
||||
--> $DIR/min_const_fn_unsafe.rs:29:51
|
||||
|
|
||||
LL | const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
|
||||
| ^^
|
||||
|
|
@ -7,7 +7,7 @@ LL | const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowe
|
|||
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: unions in const fn are unstable (see issue #51909)
|
||||
--> $DIR/min_const_fn_unsafe.rs:34:5
|
||||
--> $DIR/min_const_fn_unsafe.rs:36:5
|
||||
|
|
||||
LL | Foo { x: () }.y //~ ERROR not allowed in const fn
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
|
@ -15,7 +15,7 @@ LL | Foo { x: () }.y //~ ERROR not allowed in const fn
|
|||
= help: add #![feature(const_fn_union)] to the crate attributes to enable
|
||||
|
||||
error: call to unsafe function is unsafe and unsafe operations are not allowed in const fn
|
||||
--> $DIR/min_const_fn_unsafe.rs:19:14
|
||||
--> $DIR/min_const_fn_unsafe.rs:21:14
|
||||
|
|
||||
LL | unsafe { foo4() } //~ ERROR unsafe operations are not allowed in const fn
|
||||
| ^^^^^^ call to unsafe function
|
||||
|
|
@ -23,7 +23,7 @@ LL | unsafe { foo4() } //~ ERROR unsafe operations are not allowed in const
|
|||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error: call to unsafe function is unsafe and unsafe operations are not allowed in const fn
|
||||
--> $DIR/min_const_fn_unsafe.rs:22:14
|
||||
--> $DIR/min_const_fn_unsafe.rs:24:14
|
||||
|
|
||||
LL | unsafe { foo5::<String>() } //~ ERROR unsafe operations are not allowed in const fn
|
||||
| ^^^^^^^^^^^^^^^^ call to unsafe function
|
||||
|
|
@ -31,7 +31,7 @@ LL | unsafe { foo5::<String>() } //~ ERROR unsafe operations are not allowed
|
|||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error: call to unsafe function is unsafe and unsafe operations are not allowed in const fn
|
||||
--> $DIR/min_const_fn_unsafe.rs:25:14
|
||||
--> $DIR/min_const_fn_unsafe.rs:27:14
|
||||
|
|
||||
LL | unsafe { foo6::<Vec<std::cell::Cell<u32>>>() } //~ ERROR not allowed in const fn
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
|
||||
|
|
@ -39,7 +39,7 @@ LL | unsafe { foo6::<Vec<std::cell::Cell<u32>>>() } //~ ERROR not allowed in
|
|||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error: dereference of raw pointer is unsafe and unsafe operations are not allowed in const fn
|
||||
--> $DIR/min_const_fn_unsafe.rs:27:51
|
||||
--> $DIR/min_const_fn_unsafe.rs:29:51
|
||||
|
|
||||
LL | const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
|
||||
| ^^ dereference of raw pointer
|
||||
|
|
@ -47,7 +47,7 @@ LL | const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowe
|
|||
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
|
||||
|
||||
error: access to union field is unsafe and unsafe operations are not allowed in const fn
|
||||
--> $DIR/min_const_fn_unsafe.rs:34:5
|
||||
--> $DIR/min_const_fn_unsafe.rs:36:5
|
||||
|
|
||||
LL | Foo { x: () }.y //~ ERROR not allowed in const fn
|
||||
| ^^^^^^^^^^^^^^^ access to union field
|
||||
|
|
|
|||
|
|
@ -0,0 +1,56 @@
|
|||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(min_const_unsafe_fn)]
|
||||
|
||||
// ok
|
||||
const unsafe fn foo4() -> i32 { 42 }
|
||||
const unsafe fn foo5<T>() -> *const T { 0 as *const T }
|
||||
const unsafe fn foo6<T>() -> *mut T { 0 as *mut T }
|
||||
const fn no_unsafe() { unsafe {} }
|
||||
|
||||
const fn foo8() -> i32 {
|
||||
unsafe { foo4() }
|
||||
}
|
||||
const fn foo9() -> *const String {
|
||||
unsafe { foo5::<String>() }
|
||||
}
|
||||
const fn foo10() -> *const Vec<std::cell::Cell<u32>> {
|
||||
unsafe { foo6::<Vec<std::cell::Cell<u32>>>() }
|
||||
}
|
||||
const unsafe fn foo8_3() -> i32 {
|
||||
unsafe { foo4() }
|
||||
}
|
||||
const unsafe fn foo9_3() -> *const String {
|
||||
unsafe { foo5::<String>() }
|
||||
}
|
||||
const unsafe fn foo10_3() -> *const Vec<std::cell::Cell<u32>> {
|
||||
unsafe { foo6::<Vec<std::cell::Cell<u32>>>() }
|
||||
}
|
||||
// not ok
|
||||
const unsafe fn foo8_2() -> i32 {
|
||||
foo4() //~ ERROR not allowed in const fn
|
||||
}
|
||||
const unsafe fn foo9_2() -> *const String {
|
||||
foo5::<String>() //~ ERROR not allowed in const fn
|
||||
}
|
||||
const unsafe fn foo10_2() -> *const Vec<std::cell::Cell<u32>> {
|
||||
foo6::<Vec<std::cell::Cell<u32>>>() //~ ERROR not allowed in const fn
|
||||
}
|
||||
const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
|
||||
//~^ dereferencing raw pointers in constant functions
|
||||
|
||||
fn main() {}
|
||||
|
||||
const unsafe fn no_union() {
|
||||
union Foo { x: (), y: () }
|
||||
Foo { x: () }.y //~ ERROR not allowed in const fn
|
||||
//~^ unions in const fn
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
|
||||
--> $DIR/min_const_fn_unsafe_feature_gate.rs:47:51
|
||||
|
|
||||
LL | const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
|
||||
| ^^
|
||||
|
|
||||
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: unions in const fn are unstable (see issue #51909)
|
||||
--> $DIR/min_const_fn_unsafe_feature_gate.rs:54:5
|
||||
|
|
||||
LL | Foo { x: () }.y //~ ERROR not allowed in const fn
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: add #![feature(const_fn_union)] to the crate attributes to enable
|
||||
|
||||
error: call to unsafe function is unsafe and unsafe operations are not allowed in const fn
|
||||
--> $DIR/min_const_fn_unsafe_feature_gate.rs:39:5
|
||||
|
|
||||
LL | foo4() //~ ERROR not allowed in const fn
|
||||
| ^^^^^^ call to unsafe function
|
||||
|
|
||||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error: call to unsafe function is unsafe and unsafe operations are not allowed in const fn
|
||||
--> $DIR/min_const_fn_unsafe_feature_gate.rs:42:5
|
||||
|
|
||||
LL | foo5::<String>() //~ ERROR not allowed in const fn
|
||||
| ^^^^^^^^^^^^^^^^ call to unsafe function
|
||||
|
|
||||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error: call to unsafe function is unsafe and unsafe operations are not allowed in const fn
|
||||
--> $DIR/min_const_fn_unsafe_feature_gate.rs:45:5
|
||||
|
|
||||
LL | foo6::<Vec<std::cell::Cell<u32>>>() //~ ERROR not allowed in const fn
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
|
||||
|
|
||||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error: dereference of raw pointer is unsafe and unsafe operations are not allowed in const fn
|
||||
--> $DIR/min_const_fn_unsafe_feature_gate.rs:47:51
|
||||
|
|
||||
LL | const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
|
||||
| ^^ dereference of raw pointer
|
||||
|
|
||||
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
|
||||
|
||||
error: access to union field is unsafe and unsafe operations are not allowed in const fn
|
||||
--> $DIR/min_const_fn_unsafe_feature_gate.rs:54:5
|
||||
|
|
||||
LL | Foo { x: () }.y //~ ERROR not allowed in const fn
|
||||
| ^^^^^^^^^^^^^^^ access to union field
|
||||
|
|
||||
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0658`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue