Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f2ae7b7
Allow calling `const unsafe fn` in `const fn` behind a feature gate
oli-obk Nov 2, 2018
906a49e
Document unsafe rules with comments and `bug!` calls
oli-obk Nov 3, 2018
cc3470c
Add test for dereferencing raw pointers and immediately referencing a…
oli-obk Nov 3, 2018
02b2232
Make sure the initialization of constrained int range newtypes is unsafe
oli-obk Nov 3, 2018
ec6573f
Make `newtype_index` safe
oli-obk Nov 3, 2018
693c553
Move ref to packed struct field check into projection arm
oli-obk Nov 3, 2018
8bdb11c
Forbid the creation of mutable borrows to fields of layout constraine…
oli-obk Nov 3, 2018
14218e3
Trailing newlines again
oli-obk Nov 3, 2018
c4a8500
Adjust a rustc test to the safety changes
oli-obk Nov 4, 2018
081c497
generalize the message about the creation of layout restricted types
oli-obk Nov 4, 2018
1894a5f
Also make immutable references to non-freeze restricted value range t…
oli-obk Nov 4, 2018
55abc0b
Also prevent mutation fields directly
oli-obk Nov 5, 2018
e5d9065
Comment on the unsafety code for layout constrained fields
oli-obk Nov 5, 2018
4497ff3
Emit feature gate suggestion
oli-obk Nov 5, 2018
37ef5e4
Add tests for stable unsafe features in const fn
oli-obk Nov 6, 2018
3ce211d
Increase code-reuse and -readability
oli-obk Nov 19, 2018
137a640
Automatically generate imports for newtype_index `Deserialize` impls
oli-obk Nov 19, 2018
ae0b00c
Add and update tests
oli-obk Nov 19, 2018
b75d5f1
Clean up the logic in `is_min_const_fn`
oli-obk Nov 30, 2018
932dbe8
Explain unsafety trickery of const functions
oli-obk Nov 30, 2018
b779694
Clear up some code
oli-obk Nov 30, 2018
f411576
Intrinsic checks are just needed for `qualify_min_const_fn`
oli-obk Dec 1, 2018
cb71752
Tidy fixup
oli-obk Dec 4, 2018
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add and update tests
  • Loading branch information
oli-obk committed Dec 4, 2018
commit ae0b00cadace32fd85c6786df24a20d6b55a87d2
1 change: 0 additions & 1 deletion src/test/run-pass-fulldeps/newtype_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#[macro_use] extern crate rustc_data_structures;
extern crate rustc_serialize;
use rustc_serialize::{Decodable, Decoder};

use rustc_data_structures::indexed_vec::Idx;

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/consts/min_const_fn/min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
22 changes: 12 additions & 10 deletions src/test/ui/consts/min_const_fn/min_const_fn_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@
// 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 calls to `const unsafe fn` in const fns are unstable
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 calls to `const unsafe fn` in const fns are unstable
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 calls to `const unsafe fn` in const fns
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() {}
Expand Down
30 changes: 15 additions & 15 deletions src/test/ui/consts/min_const_fn/min_const_fn_unsafe.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
--> $DIR/min_const_fn_unsafe.rs:29: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:36:5
--> $DIR/min_const_fn_unsafe.rs:38:5
|
LL | Foo { x: () }.y //~ ERROR not allowed in const fn
| ^^^^^^^^^^^^^^^
Expand All @@ -17,37 +17,37 @@ LL | Foo { x: () }.y //~ ERROR not allowed in const fn
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 calls to `const unsafe fn` in const fns are unstable
| ^^^^^^
LL | unsafe { ret_i32_no_unsafe() } //~ ERROR calls to `const unsafe fn` in const fns are unstable
| ^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(min_const_unsafe_fn)] to the crate attributes to enable

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 calls to `const unsafe fn` in const fns are unstable
| ^^^^^^^^^^^^^^^^
LL | unsafe { ret_null_ptr_no_unsafe::<String>() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(min_const_unsafe_fn)] to the crate attributes to enable

error[E0658]: calls to `const unsafe fn` in const fns are unstable (see issue #55607)
--> $DIR/min_const_fn_unsafe.rs:27:14
--> $DIR/min_const_fn_unsafe.rs:28:14
|
LL | unsafe { foo6::<Vec<std::cell::Cell<u32>>>() } //~ ERROR calls to `const unsafe fn` in const fns
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | unsafe { ret_null_mut_ptr_no_unsafe::<Vec<std::cell::Cell<u32>>>() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= 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:29: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

error: access to union field is unsafe and unsafe operations are not allowed in const fn
--> $DIR/min_const_fn_unsafe.rs:36:5
--> $DIR/min_const_fn_unsafe.rs:38:5
|
LL | Foo { x: () }.y //~ ERROR not allowed in const fn
| ^^^^^^^^^^^^^^^ access to union field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ LL | const unsafe fn foo30_4(x: *mut usize) -> &'static usize { &*x } //~ ERROR
|
= 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:57:5
--> $DIR/min_const_fn_unsafe_feature_gate.rs:60:5
|
LL | Foo { x: () }.y //~ ERROR not allowed in const fn
| ^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -62,14 +70,22 @@ LL | const unsafe fn foo30_4(x: *mut usize) -> &'static usize { &*x } //~ ERROR
|
= 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: 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:57:5
--> $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

error: aborting due to 9 previous errors
error: aborting due to 11 previous errors

For more information about this error, try `rustc --explain E0658`.
4 changes: 2 additions & 2 deletions src/test/ui/unsafe/ranged_ints4_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ fn main() {}

const fn foo() -> NonZero<u32> {
let mut x = unsafe { NonZero(1) };
x.0 = 0; //~ ERROR statements in constant functions are unstable
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 }; //~ ERROR statements in constant functions are unstable
unsafe { x.0 = 0 }; // this is UB
x
}
23 changes: 3 additions & 20 deletions src/test/ui/unsafe/ranged_ints4_const.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
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
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 3 previous errors
error: aborting due to previous error

Some errors occurred: E0133, E0658.
For more information about an error, try `rustc --explain E0133`.
For more information about this error, try `rustc --explain E0133`.