diff options
| author | Oliver Scherer <github35764891676564198441@oli-obk.de> | 2018-11-06 17:43:32 +0100 |
|---|---|---|
| committer | Oliver Scherer <github35764891676564198441@oli-obk.de> | 2018-12-04 10:17:37 +0100 |
| commit | 37ef5e43af477d0cb04f70e9d868361b40cd7a08 (patch) | |
| tree | e6806d934231136144af600ff497939fcfa608f3 | |
| parent | 4497ff37627d860690613249a31cf3ee4c4195ef (diff) | |
| download | rust-37ef5e43af477d0cb04f70e9d868361b40cd7a08.tar.gz rust-37ef5e43af477d0cb04f70e9d868361b40cd7a08.zip | |
Add tests for stable unsafe features in const fn
| -rw-r--r-- | src/libcore/lib.rs | 1 | ||||
| -rw-r--r-- | src/librustc/hir/mod.rs | 1 | ||||
| -rw-r--r-- | src/librustc/lib.rs | 1 | ||||
| -rw-r--r-- | src/librustc_target/abi/mod.rs | 1 | ||||
| -rw-r--r-- | src/test/ui/unsafe/ranged_ints2_const.rs | 20 | ||||
| -rw-r--r-- | src/test/ui/unsafe/ranged_ints2_const.stderr | 24 | ||||
| -rw-r--r-- | src/test/ui/unsafe/ranged_ints3_const.rs | 21 | ||||
| -rw-r--r-- | src/test/ui/unsafe/ranged_ints3_const.stderr | 24 | ||||
| -rw-r--r-- | src/test/ui/unsafe/ranged_ints4_const.rs | 19 | ||||
| -rw-r--r-- | src/test/ui/unsafe/ranged_ints4_const.stderr | 28 | ||||
| -rw-r--r-- | src/test/ui/unsafe/ranged_ints_const.rs | 11 | ||||
| -rw-r--r-- | src/test/ui/unsafe/ranged_ints_const.stderr | 11 |
12 files changed, 160 insertions, 2 deletions
diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index d070160609d..726e891df0c 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -93,7 +93,6 @@ #![feature(never_type)] #![feature(nll)] #![feature(exhaustive_patterns)] -#![cfg_attr(not(stage0), feature(min_const_unsafe_fn))] #![feature(no_core)] #![feature(on_unimplemented)] #![feature(optin_builtin_traits)] diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 1674320165e..597a7738217 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -122,6 +122,7 @@ impl serialize::UseSpecializedDecodable for HirId { // hack to ensure that we don't try to access the private parts of `ItemLocalId` in this module mod item_local_id_inner { use rustc_data_structures::indexed_vec::Idx; + use serialize::{Decodable, Decoder}; /// An `ItemLocalId` uniquely identifies something within a given "item-like", /// that is within a hir::Item, hir::TraitItem, or hir::ImplItem. There is no /// guarantee that the numerical value of a given `ItemLocalId` corresponds to diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 99412c02c43..ddb0c5bf22a 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -69,7 +69,6 @@ #![feature(in_band_lifetimes)] #![feature(crate_visibility_modifier)] #![feature(transpose_result)] -#![cfg_attr(not(stage0), feature(min_const_unsafe_fn))] #![recursion_limit="512"] diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 50ce0ad6915..27a4292543a 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -17,6 +17,7 @@ use std::fmt; use std::ops::{Add, Deref, Sub, Mul, AddAssign, Range, RangeInclusive}; use rustc_data_structures::indexed_vec::{Idx, IndexVec}; +use rustc_serialize::{Decodable, Decoder}; pub mod call; diff --git a/src/test/ui/unsafe/ranged_ints2_const.rs b/src/test/ui/unsafe/ranged_ints2_const.rs new file mode 100644 index 00000000000..a61e3329bdc --- /dev/null +++ b/src/test/ui/unsafe/ranged_ints2_const.rs @@ -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) } +} diff --git a/src/test/ui/unsafe/ranged_ints2_const.stderr b/src/test/ui/unsafe/ranged_ints2_const.stderr new file mode 100644 index 00000000000..f79792ffba9 --- /dev/null +++ b/src/test/ui/unsafe/ranged_ints2_const.stderr @@ -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`. diff --git a/src/test/ui/unsafe/ranged_ints3_const.rs b/src/test/ui/unsafe/ranged_ints3_const.rs new file mode 100644 index 00000000000..6497b611224 --- /dev/null +++ b/src/test/ui/unsafe/ranged_ints3_const.rs @@ -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)) } +} diff --git a/src/test/ui/unsafe/ranged_ints3_const.stderr b/src/test/ui/unsafe/ranged_ints3_const.stderr new file mode 100644 index 00000000000..d83d75787d9 --- /dev/null +++ b/src/test/ui/unsafe/ranged_ints3_const.stderr @@ -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`. diff --git a/src/test/ui/unsafe/ranged_ints4_const.rs b/src/test/ui/unsafe/ranged_ints4_const.rs new file mode 100644 index 00000000000..09689579639 --- /dev/null +++ b/src/test/ui/unsafe/ranged_ints4_const.rs @@ -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 statements in constant functions are unstable + //~^ ERROR mutation of layout constrained field is unsafe + x +} + +const fn bar() -> NonZero<u32> { + let mut x = unsafe { NonZero(1) }; + unsafe { x.0 = 0 }; //~ ERROR statements in constant functions are unstable + x +} diff --git a/src/test/ui/unsafe/ranged_ints4_const.stderr b/src/test/ui/unsafe/ranged_ints4_const.stderr new file mode 100644 index 00000000000..284ba3603af --- /dev/null +++ b/src/test/ui/unsafe/ranged_ints4_const.stderr @@ -0,0 +1,28 @@ +error[E0658]: statements in constant functions are unstable (see issue #48821) + --> $DIR/ranged_ints4_const.rs:10:5 + | +LL | x.0 = 0; //~ ERROR statements in constant functions are unstable + | ^^^^^^^ + | + = help: add #![feature(const_let)] to the crate attributes to enable + +error[E0658]: statements in constant functions are unstable (see issue #48821) + --> $DIR/ranged_ints4_const.rs:17:14 + | +LL | unsafe { x.0 = 0 }; //~ ERROR statements in constant functions are unstable + | ^^^^^^^ + | + = help: add #![feature(const_let)] to the crate attributes to enable + +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; //~ ERROR statements in constant functions are unstable + | ^^^^^^^ 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: E0133, E0658. +For more information about an error, try `rustc --explain E0133`. diff --git a/src/test/ui/unsafe/ranged_ints_const.rs b/src/test/ui/unsafe/ranged_ints_const.rs new file mode 100644 index 00000000000..8477772867e --- /dev/null +++ b/src/test/ui/unsafe/ranged_ints_const.rs @@ -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) } } diff --git a/src/test/ui/unsafe/ranged_ints_const.stderr b/src/test/ui/unsafe/ranged_ints_const.stderr new file mode 100644 index 00000000000..584ad40a92b --- /dev/null +++ b/src/test/ui/unsafe/ranged_ints_const.stderr @@ -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`. |
