Auto merge of #55635 - oli-obk:min_const_unsafe_fn, r=nikomatsakis

Allow calling `const unsafe fn` in `const fn` behind a feature gate

cc #55607

r? @Centril
This commit is contained in:
bors 2018-12-06 10:18:17 +00:00
commit 128a1fa4e1
38 changed files with 868 additions and 137 deletions

View file

@ -1,4 +1,4 @@
#![feature(rustc_attrs, rustc_private, step_trait)]
#![feature(rustc_attrs, rustc_private, step_trait, min_const_unsafe_fn)]
#[macro_use] extern crate rustc_data_structures;
extern crate rustc_serialize;

View file

@ -78,9 +78,9 @@ const fn i32_ops2(c: i32, d: i32) -> bool { c < d }
const fn i32_ops3(c: i32, d: i32) -> bool { c != d }
const fn i32_ops4(c: i32, d: i32) -> i32 { c + d }
const fn char_cast(u: u8) -> char { u as char }
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 unsafe fn ret_i32_no_unsafe() -> i32 { 42 }
const unsafe fn ret_null_ptr_no_unsafe<T>() -> *const T { 0 as *const T }
const unsafe fn ret_null_mut_ptr_no_unsafe<T>() -> *mut T { 0 as *mut T }
// not ok
const fn foo11<T: std::fmt::Display>(t: T) -> T { t }

View file

@ -8,23 +8,27 @@
// 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 }
const unsafe fn foo6<T>() -> *mut T { 0 as *mut T }
const unsafe fn ret_i32_no_unsafe() -> i32 { 42 }
const unsafe fn ret_null_ptr_no_unsafe<T>() -> *const T { 0 as *const T }
const unsafe fn ret_null_mut_ptr_no_unsafe<T>() -> *mut T { 0 as *mut T }
const fn no_unsafe() { unsafe {} }
// not ok
const fn foo8() -> i32 {
unsafe { foo4() } //~ ERROR unsafe operations are not allowed in const fn
const fn call_unsafe_const_fn() -> i32 {
unsafe { ret_i32_no_unsafe() } //~ ERROR calls to `const unsafe fn` in const fns are unstable
}
const fn foo9() -> *const String {
unsafe { foo5::<String>() } //~ ERROR unsafe operations are not allowed in const fn
const fn call_unsafe_generic_const_fn() -> *const String {
unsafe { ret_null_ptr_no_unsafe::<String>() }
//~^ ERROR calls to `const unsafe fn` in const fns are unstable
}
const fn foo10() -> *const Vec<std::cell::Cell<u32>> {
unsafe { foo6::<Vec<std::cell::Cell<u32>>>() } //~ ERROR not allowed in const fn
const fn call_unsafe_generic_cell_const_fn() -> *const Vec<std::cell::Cell<u32>> {
unsafe { ret_null_mut_ptr_no_unsafe::<Vec<std::cell::Cell<u32>>>() }
//~^ ERROR calls to `const unsafe fn` in const fns
}
const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
const unsafe fn deref_forbidden(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
//~^ dereferencing raw pointers in constant functions
fn main() {}

View file

@ -1,58 +1,60 @@
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:31:59
|
LL | const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
| ^^
LL | const unsafe fn deref_forbidden(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.rs:34:5
--> $DIR/min_const_fn_unsafe.rs:38: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.rs:19:14
error[E0658]: calls to `const unsafe fn` in const fns are unstable (see issue #55607)
--> $DIR/min_const_fn_unsafe.rs:21:14
|
LL | unsafe { foo4() } //~ ERROR unsafe operations are not allowed in const fn
| ^^^^^^ call to unsafe function
LL | unsafe { ret_i32_no_unsafe() } //~ ERROR calls to `const unsafe fn` in const fns are unstable
| ^^^^^^^^^^^^^^^^^^^
|
= note: consult the function's documentation for information on how to avoid undefined behavior
= help: add #![feature(min_const_unsafe_fn)] 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:22:14
error[E0658]: calls to `const unsafe fn` in const fns are unstable (see issue #55607)
--> $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
LL | unsafe { ret_null_ptr_no_unsafe::<String>() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: consult the function's documentation for information on how to avoid undefined behavior
= help: add #![feature(min_const_unsafe_fn)] 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:25:14
error[E0658]: calls to `const unsafe fn` in const fns are unstable (see issue #55607)
--> $DIR/min_const_fn_unsafe.rs:28:14
|
LL | unsafe { foo6::<Vec<std::cell::Cell<u32>>>() } //~ ERROR not allowed in const fn
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
LL | unsafe { ret_null_mut_ptr_no_unsafe::<Vec<std::cell::Cell<u32>>>() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: consult the function's documentation for information on how to avoid undefined behavior
= help: add #![feature(min_const_unsafe_fn)] to the crate attributes to enable
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:31:59
|
LL | const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
| ^^ dereference of raw pointer
LL | const unsafe fn deref_forbidden(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
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
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:38: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
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
error: aborting due to 7 previous errors

View file

@ -0,0 +1,62 @@
// 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
const unsafe fn foo30_4(x: *mut usize) -> &'static usize { &*x } //~ ERROR not allowed in const fn
//~^ dereferencing raw pointers in constant functions
const fn foo30_5(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ ERROR not allowed
//~^ 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
}

View file

@ -0,0 +1,97 @@
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]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
--> $DIR/min_const_fn_unsafe_feature_gate.rs:50:60
|
LL | const unsafe fn foo30_4(x: *mut usize) -> &'static usize { &*x } //~ ERROR not allowed in const fn
| ^^^
|
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
--> $DIR/min_const_fn_unsafe_feature_gate.rs:53:62
|
LL | const fn foo30_5(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ ERROR not allowed
| ^^^
|
= 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:60: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
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
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
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
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
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
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
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
error: dereference of raw pointer is unsafe and unsafe operations are not allowed in const fn
--> $DIR/min_const_fn_unsafe_feature_gate.rs:50:60
|
LL | const unsafe fn foo30_4(x: *mut usize) -> &'static 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
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
error: dereference of raw pointer is unsafe and unsafe operations are not allowed in const fn
--> $DIR/min_const_fn_unsafe_feature_gate.rs:53:62
|
LL | const fn foo30_5(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ ERROR not allowed
| ^^^ 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:60: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
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
error: aborting due to 11 previous errors
For more information about this error, try `rustc --explain E0658`.

View file

@ -0,0 +1,47 @@
// 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.
#![unstable(feature = "humans",
reason = "who ever let humans program computers,
we're apparently really bad at it",
issue = "0")]
#![feature(rustc_const_unstable, const_fn, foo, foo2)]
#![feature(min_const_unsafe_fn)]
#![feature(staged_api)]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature="foo")]
const unsafe fn foo() -> u32 { 42 }
#[stable(feature = "rust1", since = "1.0.0")]
// can't call non-min_const_fn
const unsafe fn bar() -> u32 { unsafe { foo() } } //~ ERROR can only call other `min_const_fn`
#[unstable(feature = "rust1", issue="0")]
const unsafe fn foo2() -> u32 { 42 }
#[stable(feature = "rust1", since = "1.0.0")]
// can't call non-min_const_fn
const unsafe fn bar2() -> u32 { unsafe { foo2() } } //~ ERROR can only call other `min_const_fn`
#[stable(feature = "rust1", since = "1.0.0")]
// conformity is required, even with `const_fn` feature gate
const unsafe fn bar3() -> u32 { (5f32 + 6f32) as u32 } //~ ERROR only int, `bool` and `char` op
// check whether this function cannot be called even with the feature gate active
#[unstable(feature = "foo2", issue="0")]
const unsafe fn foo2_gated() -> u32 { 42 }
#[stable(feature = "rust1", since = "1.0.0")]
// can't call non-min_const_fn
const unsafe fn bar2_gated() -> u32 { unsafe { foo2_gated() } } //~ ERROR can only call other
fn main() {}

View file

@ -0,0 +1,26 @@
error: can only call other `min_const_fn` within a `min_const_fn`
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:26:41
|
LL | const unsafe fn bar() -> u32 { unsafe { foo() } } //~ ERROR can only call other `min_const_fn`
| ^^^^^
error: can only call other `min_const_fn` within a `min_const_fn`
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:33:42
|
LL | const unsafe fn bar2() -> u32 { unsafe { foo2() } } //~ ERROR can only call other `min_const_fn`
| ^^^^^^
error: only int, `bool` and `char` operations are stable in const fn
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:37:33
|
LL | const unsafe fn bar3() -> u32 { (5f32 + 6f32) as u32 } //~ ERROR only int, `bool` and `char` op
| ^^^^^^^^^^^^^
error: can only call other `min_const_fn` within a `min_const_fn`
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:45:48
|
LL | const unsafe fn bar2_gated() -> u32 { unsafe { foo2_gated() } } //~ ERROR can only call other
| ^^^^^^^^^^^^
error: aborting due to 4 previous errors

View file

@ -0,0 +1,43 @@
// 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.
#![unstable(feature = "humans",
reason = "who ever let humans program computers,
we're apparently really bad at it",
issue = "0")]
#![feature(rustc_const_unstable, const_fn, foo, foo2)]
#![feature(min_const_unsafe_fn)]
#![feature(staged_api)]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature="foo")]
const fn foo() -> u32 { 42 }
#[stable(feature = "rust1", since = "1.0.0")]
// can't call non-min_const_fn
const unsafe fn bar() -> u32 { foo() } //~ ERROR can only call other `min_const_fn`
#[unstable(feature = "rust1", issue="0")]
const fn foo2() -> u32 { 42 }
#[stable(feature = "rust1", since = "1.0.0")]
// can't call non-min_const_fn
const unsafe fn bar2() -> u32 { foo2() } //~ ERROR can only call other `min_const_fn`
// check whether this function cannot be called even with the feature gate active
#[unstable(feature = "foo2", issue="0")]
const fn foo2_gated() -> u32 { 42 }
#[stable(feature = "rust1", since = "1.0.0")]
// can't call non-min_const_fn
const unsafe fn bar2_gated() -> u32 { foo2_gated() } //~ ERROR can only call other `min_const_fn`
fn main() {}

View file

@ -0,0 +1,20 @@
error: can only call other `min_const_fn` within a `min_const_fn`
--> $DIR/min_const_unsafe_fn_libstd_stability2.rs:26:32
|
LL | const unsafe fn bar() -> u32 { foo() } //~ ERROR can only call other `min_const_fn`
| ^^^^^
error: can only call other `min_const_fn` within a `min_const_fn`
--> $DIR/min_const_unsafe_fn_libstd_stability2.rs:33:33
|
LL | const unsafe fn bar2() -> u32 { foo2() } //~ ERROR can only call other `min_const_fn`
| ^^^^^^
error: can only call other `min_const_fn` within a `min_const_fn`
--> $DIR/min_const_unsafe_fn_libstd_stability2.rs:41:39
|
LL | const unsafe fn bar2_gated() -> u32 { foo2_gated() } //~ ERROR can only call other `min_const_fn`
| ^^^^^^^^^^^^
error: aborting due to 3 previous errors

View file

@ -0,0 +1,8 @@
#![feature(rustc_attrs)]
#[rustc_layout_scalar_valid_range_start(1)]
#[repr(transparent)]
pub(crate) struct NonZero<T>(pub(crate) T);
fn main() {
let _x = NonZero(0); //~ ERROR initializing type with `rustc_layout_scalar_valid_range` attr
}

View file

@ -0,0 +1,11 @@
error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
--> $DIR/ranged_ints.rs:7:14
|
LL | let _x = NonZero(0); //~ ERROR initializing type with `rustc_layout_scalar_valid_range` attr
| ^^^^^^^^^^ initializing type with `rustc_layout_scalar_valid_range` attr
|
= note: initializing a layout restricted type's field with a value outside the valid range is undefined behavior
error: aborting due to previous error
For more information about this error, try `rustc --explain E0133`.

View file

@ -0,0 +1,9 @@
#![feature(rustc_attrs)]
#[rustc_layout_scalar_valid_range_start(1)]
#[repr(transparent)]
pub(crate) struct NonZero<T>(pub(crate) T);
fn main() {
let mut x = unsafe { NonZero(1) };
let y = &mut x.0; //~ ERROR mutation of layout constrained field is unsafe
}

View file

@ -0,0 +1,11 @@
error[E0133]: mutation of layout constrained field is unsafe and requires unsafe function or block
--> $DIR/ranged_ints2.rs:8:13
|
LL | let y = &mut x.0; //~ ERROR mutation of layout constrained field is unsafe
| ^^^^^^^^ mutation of layout constrained field
|
= note: mutating layout constrained fields cannot statically be checked for valid values
error: aborting due to previous error
For more information about this error, try `rustc --explain E0133`.

View file

@ -0,0 +1,20 @@
#![feature(rustc_attrs, const_let, const_fn)]
#[rustc_layout_scalar_valid_range_start(1)]
#[repr(transparent)]
pub(crate) struct NonZero<T>(pub(crate) T);
fn main() {
}
const fn foo() -> NonZero<u32> {
let mut x = unsafe { NonZero(1) };
let y = &mut x.0; //~ ERROR references in constant functions may only refer to immutable
//~^ ERROR mutation of layout constrained field is unsafe
unsafe { NonZero(1) }
}
const fn bar() -> NonZero<u32> {
let mut x = unsafe { NonZero(1) };
let y = unsafe { &mut x.0 }; //~ ERROR references in constant functions may only refer to immut
unsafe { NonZero(1) }
}

View file

@ -0,0 +1,24 @@
error[E0017]: references in constant functions may only refer to immutable values
--> $DIR/ranged_ints2_const.rs:11:13
|
LL | let y = &mut x.0; //~ ERROR references in constant functions may only refer to immutable
| ^^^^^^^^ constant functions require immutable values
error[E0017]: references in constant functions may only refer to immutable values
--> $DIR/ranged_ints2_const.rs:18:22
|
LL | let y = unsafe { &mut x.0 }; //~ ERROR references in constant functions may only refer to immut
| ^^^^^^^^ constant functions require immutable values
error[E0133]: mutation of layout constrained field is unsafe and requires unsafe function or block
--> $DIR/ranged_ints2_const.rs:11:13
|
LL | let y = &mut x.0; //~ ERROR references in constant functions may only refer to immutable
| ^^^^^^^^ mutation of layout constrained field
|
= note: mutating layout constrained fields cannot statically be checked for valid values
error: aborting due to 3 previous errors
Some errors occurred: E0017, E0133.
For more information about an error, try `rustc --explain E0017`.

View file

@ -0,0 +1,11 @@
#![feature(rustc_attrs)]
use std::cell::Cell;
#[rustc_layout_scalar_valid_range_start(1)]
#[repr(transparent)]
pub(crate) struct NonZero<T>(pub(crate) T);
fn main() {
let mut x = unsafe { NonZero(Cell::new(1)) };
let y = &x.0; //~ ERROR borrow of layout constrained field with interior mutability
}

View file

@ -0,0 +1,11 @@
error[E0133]: borrow of layout constrained field with interior mutability is unsafe and requires unsafe function or block
--> $DIR/ranged_ints3.rs:10:13
|
LL | let y = &x.0; //~ ERROR borrow of layout constrained field with interior mutability
| ^^^^ borrow of layout constrained field with interior mutability
|
= note: references to fields of layout constrained fields lose the constraints. Coupled with interior mutability, the field can be changed to invalid values
error: aborting due to previous error
For more information about this error, try `rustc --explain E0133`.

View file

@ -0,0 +1,21 @@
#![feature(rustc_attrs, const_let, const_fn)]
use std::cell::Cell;
#[rustc_layout_scalar_valid_range_start(1)]
#[repr(transparent)]
pub(crate) struct NonZero<T>(pub(crate) T);
fn main() {}
const fn foo() -> NonZero<Cell<u32>> {
let mut x = unsafe { NonZero(Cell::new(1)) };
let y = &x.0; //~ ERROR cannot borrow a constant which may contain interior mutability
//~^ ERROR borrow of layout constrained field with interior mutability
unsafe { NonZero(Cell::new(1)) }
}
const fn bar() -> NonZero<Cell<u32>> {
let mut x = unsafe { NonZero(Cell::new(1)) };
let y = unsafe { &x.0 }; //~ ERROR cannot borrow a constant which may contain interior mut
unsafe { NonZero(Cell::new(1)) }
}

View file

@ -0,0 +1,24 @@
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/ranged_ints3_const.rs:12:13
|
LL | let y = &x.0; //~ ERROR cannot borrow a constant which may contain interior mutability
| ^^^^
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/ranged_ints3_const.rs:19:22
|
LL | let y = unsafe { &x.0 }; //~ ERROR cannot borrow a constant which may contain interior mut
| ^^^^
error[E0133]: borrow of layout constrained field with interior mutability is unsafe and requires unsafe function or block
--> $DIR/ranged_ints3_const.rs:12:13
|
LL | let y = &x.0; //~ ERROR cannot borrow a constant which may contain interior mutability
| ^^^^ borrow of layout constrained field with interior mutability
|
= note: references to fields of layout constrained fields lose the constraints. Coupled with interior mutability, the field can be changed to invalid values
error: aborting due to 3 previous errors
Some errors occurred: E0133, E0492.
For more information about an error, try `rustc --explain E0133`.

View file

@ -0,0 +1,9 @@
#![feature(rustc_attrs)]
#[rustc_layout_scalar_valid_range_start(1)]
#[repr(transparent)]
pub(crate) struct NonZero<T>(pub(crate) T);
fn main() {
let mut x = unsafe { NonZero(1) };
x.0 = 0; //~ ERROR mutation of layout constrained field is unsafe
}

View file

@ -0,0 +1,11 @@
error[E0133]: mutation of layout constrained field is unsafe and requires unsafe function or block
--> $DIR/ranged_ints4.rs:8:5
|
LL | x.0 = 0; //~ ERROR mutation of layout constrained field is unsafe
| ^^^^^^^ mutation of layout constrained field
|
= note: mutating layout constrained fields cannot statically be checked for valid values
error: aborting due to previous error
For more information about this error, try `rustc --explain E0133`.

View file

@ -0,0 +1,19 @@
#![feature(rustc_attrs, const_let, const_fn)]
#[rustc_layout_scalar_valid_range_start(1)]
#[repr(transparent)]
pub(crate) struct NonZero<T>(pub(crate) T);
fn main() {}
const fn foo() -> NonZero<u32> {
let mut x = unsafe { NonZero(1) };
x.0 = 0;
//~^ ERROR mutation of layout constrained field is unsafe
x
}
const fn bar() -> NonZero<u32> {
let mut x = unsafe { NonZero(1) };
unsafe { x.0 = 0 }; // this is UB
x
}

View file

@ -0,0 +1,11 @@
error[E0133]: mutation of layout constrained field is unsafe and requires unsafe function or block
--> $DIR/ranged_ints4_const.rs:10:5
|
LL | x.0 = 0;
| ^^^^^^^ mutation of layout constrained field
|
= note: mutating layout constrained fields cannot statically be checked for valid values
error: aborting due to previous error
For more information about this error, try `rustc --explain E0133`.

View file

@ -0,0 +1,11 @@
#![feature(rustc_attrs)]
#[rustc_layout_scalar_valid_range_start(1)]
#[repr(transparent)]
pub(crate) struct NonZero<T>(pub(crate) T);
fn main() {}
const fn foo() -> NonZero<u32> { NonZero(0) }
//~^ ERROR initializing type with `rustc_layout_scalar_valid_range` attr is unsafe
const fn bar() -> NonZero<u32> { unsafe { NonZero(0) } }

View file

@ -0,0 +1,11 @@
error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
--> $DIR/ranged_ints_const.rs:8:34
|
LL | const fn foo() -> NonZero<u32> { NonZero(0) }
| ^^^^^^^^^^ initializing type with `rustc_layout_scalar_valid_range` attr
|
= note: initializing a layout restricted type's field with a value outside the valid range is undefined behavior
error: aborting due to previous error
For more information about this error, try `rustc --explain E0133`.