Skip to content

Commit 37ef5e4

Browse files
committed
Add tests for stable unsafe features in const fn
1 parent 4497ff3 commit 37ef5e4

File tree

12 files changed

+160
-2
lines changed

12 files changed

+160
-2
lines changed

src/libcore/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
#![feature(never_type)]
9494
#![feature(nll)]
9595
#![feature(exhaustive_patterns)]
96-
#![cfg_attr(not(stage0), feature(min_const_unsafe_fn))]
9796
#![feature(no_core)]
9897
#![feature(on_unimplemented)]
9998
#![feature(optin_builtin_traits)]

src/librustc/hir/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ impl serialize::UseSpecializedDecodable for HirId {
122122
// hack to ensure that we don't try to access the private parts of `ItemLocalId` in this module
123123
mod item_local_id_inner {
124124
use rustc_data_structures::indexed_vec::Idx;
125+
use serialize::{Decodable, Decoder};
125126
/// An `ItemLocalId` uniquely identifies something within a given "item-like",
126127
/// that is within a hir::Item, hir::TraitItem, or hir::ImplItem. There is no
127128
/// guarantee that the numerical value of a given `ItemLocalId` corresponds to

src/librustc/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
#![feature(in_band_lifetimes)]
7070
#![feature(crate_visibility_modifier)]
7171
#![feature(transpose_result)]
72-
#![cfg_attr(not(stage0), feature(min_const_unsafe_fn))]
7372

7473
#![recursion_limit="512"]
7574

src/librustc_target/abi/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::fmt;
1717
use std::ops::{Add, Deref, Sub, Mul, AddAssign, Range, RangeInclusive};
1818

1919
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
20+
use rustc_serialize::{Decodable, Decoder};
2021

2122
pub mod call;
2223

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(rustc_attrs, const_let, const_fn)]
2+
3+
#[rustc_layout_scalar_valid_range_start(1)]
4+
#[repr(transparent)]
5+
pub(crate) struct NonZero<T>(pub(crate) T);
6+
fn main() {
7+
}
8+
9+
const fn foo() -> NonZero<u32> {
10+
let mut x = unsafe { NonZero(1) };
11+
let y = &mut x.0; //~ ERROR references in constant functions may only refer to immutable
12+
//~^ ERROR mutation of layout constrained field is unsafe
13+
unsafe { NonZero(1) }
14+
}
15+
16+
const fn bar() -> NonZero<u32> {
17+
let mut x = unsafe { NonZero(1) };
18+
let y = unsafe { &mut x.0 }; //~ ERROR references in constant functions may only refer to immut
19+
unsafe { NonZero(1) }
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0017]: references in constant functions may only refer to immutable values
2+
--> $DIR/ranged_ints2_const.rs:11:13
3+
|
4+
LL | let y = &mut x.0; //~ ERROR references in constant functions may only refer to immutable
5+
| ^^^^^^^^ constant functions require immutable values
6+
7+
error[E0017]: references in constant functions may only refer to immutable values
8+
--> $DIR/ranged_ints2_const.rs:18:22
9+
|
10+
LL | let y = unsafe { &mut x.0 }; //~ ERROR references in constant functions may only refer to immut
11+
| ^^^^^^^^ constant functions require immutable values
12+
13+
error[E0133]: mutation of layout constrained field is unsafe and requires unsafe function or block
14+
--> $DIR/ranged_ints2_const.rs:11:13
15+
|
16+
LL | let y = &mut x.0; //~ ERROR references in constant functions may only refer to immutable
17+
| ^^^^^^^^ mutation of layout constrained field
18+
|
19+
= note: mutating layout constrained fields cannot statically be checked for valid values
20+
21+
error: aborting due to 3 previous errors
22+
23+
Some errors occurred: E0017, E0133.
24+
For more information about an error, try `rustc --explain E0017`.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(rustc_attrs, const_let, const_fn)]
2+
3+
use std::cell::Cell;
4+
5+
#[rustc_layout_scalar_valid_range_start(1)]
6+
#[repr(transparent)]
7+
pub(crate) struct NonZero<T>(pub(crate) T);
8+
fn main() {}
9+
10+
const fn foo() -> NonZero<Cell<u32>> {
11+
let mut x = unsafe { NonZero(Cell::new(1)) };
12+
let y = &x.0; //~ ERROR cannot borrow a constant which may contain interior mutability
13+
//~^ ERROR borrow of layout constrained field with interior mutability
14+
unsafe { NonZero(Cell::new(1)) }
15+
}
16+
17+
const fn bar() -> NonZero<Cell<u32>> {
18+
let mut x = unsafe { NonZero(Cell::new(1)) };
19+
let y = unsafe { &x.0 }; //~ ERROR cannot borrow a constant which may contain interior mut
20+
unsafe { NonZero(Cell::new(1)) }
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
2+
--> $DIR/ranged_ints3_const.rs:12:13
3+
|
4+
LL | let y = &x.0; //~ ERROR cannot borrow a constant which may contain interior mutability
5+
| ^^^^
6+
7+
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
8+
--> $DIR/ranged_ints3_const.rs:19:22
9+
|
10+
LL | let y = unsafe { &x.0 }; //~ ERROR cannot borrow a constant which may contain interior mut
11+
| ^^^^
12+
13+
error[E0133]: borrow of layout constrained field with interior mutability is unsafe and requires unsafe function or block
14+
--> $DIR/ranged_ints3_const.rs:12:13
15+
|
16+
LL | let y = &x.0; //~ ERROR cannot borrow a constant which may contain interior mutability
17+
| ^^^^ borrow of layout constrained field with interior mutability
18+
|
19+
= note: references to fields of layout constrained fields lose the constraints. Coupled with interior mutability, the field can be changed to invalid values
20+
21+
error: aborting due to 3 previous errors
22+
23+
Some errors occurred: E0133, E0492.
24+
For more information about an error, try `rustc --explain E0133`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(rustc_attrs, const_let, const_fn)]
2+
3+
#[rustc_layout_scalar_valid_range_start(1)]
4+
#[repr(transparent)]
5+
pub(crate) struct NonZero<T>(pub(crate) T);
6+
fn main() {}
7+
8+
const fn foo() -> NonZero<u32> {
9+
let mut x = unsafe { NonZero(1) };
10+
x.0 = 0; //~ ERROR statements in constant functions are unstable
11+
//~^ ERROR mutation of layout constrained field is unsafe
12+
x
13+
}
14+
15+
const fn bar() -> NonZero<u32> {
16+
let mut x = unsafe { NonZero(1) };
17+
unsafe { x.0 = 0 }; //~ ERROR statements in constant functions are unstable
18+
x
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0658]: statements in constant functions are unstable (see issue #48821)
2+
--> $DIR/ranged_ints4_const.rs:10:5
3+
|
4+
LL | x.0 = 0; //~ ERROR statements in constant functions are unstable
5+
| ^^^^^^^
6+
|
7+
= help: add #![feature(const_let)] to the crate attributes to enable
8+
9+
error[E0658]: statements in constant functions are unstable (see issue #48821)
10+
--> $DIR/ranged_ints4_const.rs:17:14
11+
|
12+
LL | unsafe { x.0 = 0 }; //~ ERROR statements in constant functions are unstable
13+
| ^^^^^^^
14+
|
15+
= help: add #![feature(const_let)] to the crate attributes to enable
16+
17+
error[E0133]: mutation of layout constrained field is unsafe and requires unsafe function or block
18+
--> $DIR/ranged_ints4_const.rs:10:5
19+
|
20+
LL | x.0 = 0; //~ ERROR statements in constant functions are unstable
21+
| ^^^^^^^ mutation of layout constrained field
22+
|
23+
= note: mutating layout constrained fields cannot statically be checked for valid values
24+
25+
error: aborting due to 3 previous errors
26+
27+
Some errors occurred: E0133, E0658.
28+
For more information about an error, try `rustc --explain E0133`.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(rustc_attrs)]
2+
3+
#[rustc_layout_scalar_valid_range_start(1)]
4+
#[repr(transparent)]
5+
pub(crate) struct NonZero<T>(pub(crate) T);
6+
fn main() {}
7+
8+
const fn foo() -> NonZero<u32> { NonZero(0) }
9+
//~^ ERROR initializing type with `rustc_layout_scalar_valid_range` attr is unsafe
10+
11+
const fn bar() -> NonZero<u32> { unsafe { NonZero(0) } }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: initializing type with `rustc_layout_scalar_valid_range` attr is unsafe and requires unsafe function or block
2+
--> $DIR/ranged_ints_const.rs:8:34
3+
|
4+
LL | const fn foo() -> NonZero<u32> { NonZero(0) }
5+
| ^^^^^^^^^^ initializing type with `rustc_layout_scalar_valid_range` attr
6+
|
7+
= note: initializing a layout restricted type's field with a value outside the valid range is undefined behavior
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)