Skip to content

Commit

Permalink
Rollup merge of rust-lang#57067 - Centril:stabilize-min_const_unsafe_…
Browse files Browse the repository at this point in the history
…fn, r=oli-obk

Stabilize min_const_unsafe_fn in 1.33

Fixes rust-lang#55607

r? @oli-obk
  • Loading branch information
Centril committed Dec 23, 2018
2 parents 841af07 + 7ae6fb2 commit 24a5335
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 184 deletions.
1 change: 0 additions & 1 deletion src/librustc/ich/impls_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl_stable_hash_for!(enum mir::BorrowKind {
impl_stable_hash_for!(enum mir::UnsafetyViolationKind {
General,
GeneralAndConstFn,
GatedConstFnCall,
ExternStatic(lint_node_id),
BorrowPacked(lint_node_id),
});
Expand Down
3 changes: 0 additions & 3 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2792,9 +2792,6 @@ impl Location {
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub enum UnsafetyViolationKind {
General,
/// Right now function calls to `const unsafe fn` are only permitted behind a feature gate
/// Also, even `const unsafe fn` need an `unsafe` block to do the allowed operations.
GatedConstFnCall,
/// Permitted in const fn and regular fns
GeneralAndConstFn,
ExternStatic(ast::NodeId),
Expand Down
29 changes: 1 addition & 28 deletions src/librustc_mir/transform/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use rustc::mir::visit::{PlaceContext, Visitor, MutatingUseContext};

use syntax::ast;
use syntax::symbol::Symbol;
use syntax::feature_gate::{emit_feature_err, GateIssue};

use std::ops::Bound;

Expand Down Expand Up @@ -97,7 +96,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
if let hir::Unsafety::Unsafe = sig.unsafety() {
self.require_unsafe("call to unsafe function",
"consult the function's documentation for information on how to avoid \
undefined behavior", UnsafetyViolationKind::GatedConstFnCall)
undefined behavior", UnsafetyViolationKind::GeneralAndConstFn)
}
}
}
Expand Down Expand Up @@ -325,11 +324,6 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
// compat lint
violation.kind = UnsafetyViolationKind::General;
},
UnsafetyViolationKind::GatedConstFnCall => {
// safe code can't call unsafe const fns, this `UnsafetyViolationKind`
// is only relevant for `Safety::ExplicitUnsafe` in `unsafe const fn`s
violation.kind = UnsafetyViolationKind::General;
}
}
if !self.violations.contains(&violation) {
self.violations.push(violation)
Expand All @@ -346,19 +340,8 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
}
// only some unsafety is allowed in const fn
if self.min_const_fn {
let min_const_unsafe_fn = self.tcx.features().min_const_unsafe_fn;
for violation in violations {
match violation.kind {
UnsafetyViolationKind::GatedConstFnCall if min_const_unsafe_fn => {
// these function calls to unsafe functions are allowed
// if `#![feature(min_const_unsafe_fn)]` is active
},
UnsafetyViolationKind::GatedConstFnCall => {
// without the feature gate, we report errors
if !self.violations.contains(&violation) {
self.violations.push(violation.clone())
}
}
// these unsafe things are stable in const fn
UnsafetyViolationKind::GeneralAndConstFn => {},
// these things are forbidden in const fns
Expand Down Expand Up @@ -620,16 +603,6 @@ pub fn check_unsafety<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
.note(&details.as_str()[..])
.emit();
}
UnsafetyViolationKind::GatedConstFnCall => {
emit_feature_err(
&tcx.sess.parse_sess,
"min_const_unsafe_fn",
source_info.span,
GateIssue::Language,
"calls to `const unsafe fn` in const fns are unstable",
);

}
UnsafetyViolationKind::ExternStatic(lint_node_id) => {
tcx.lint_node_note(SAFE_EXTERN_STATICS,
lint_node_id,
Expand Down
1 change: 0 additions & 1 deletion src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@
#![feature(libc)]
#![feature(link_args)]
#![feature(linkage)]
#![cfg_attr(not(stage0), feature(min_const_unsafe_fn))]
#![feature(needs_panic_runtime)]
#![feature(never_type)]
#![feature(nll)]
Expand Down
5 changes: 2 additions & 3 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,6 @@ declare_features! (

// `extern crate self as foo;` puts local crate root into extern prelude under name `foo`.
(active, extern_crate_self, "1.31.0", Some(56409), None),

// Allows calling `const unsafe fn` inside `unsafe` blocks in `const fn` functions.
(active, min_const_unsafe_fn, "1.31.0", Some(55607), None),
);

declare_features! (
Expand Down Expand Up @@ -694,6 +691,8 @@ declare_features! (
(accepted, underscore_imports, "1.33.0", Some(48216), None),
// Allows `#[repr(packed(N))]` attribute on structs.
(accepted, repr_packed, "1.33.0", Some(33158), None),
// Allows calling `const unsafe fn` inside `unsafe` blocks in `const fn` functions.
(accepted, min_const_unsafe_fn, "1.33.0", Some(55607), None),
);

// If you change this, please modify `src/doc/unstable-book` as well. You must
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass-fulldeps/newtype_index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(rustc_attrs, rustc_private, step_trait, min_const_unsafe_fn)]
#![feature(rustc_attrs, rustc_private, step_trait)]

#[macro_use] extern crate rustc_data_structures;
extern crate rustc_serialize;
Expand Down
1 change: 0 additions & 1 deletion src/test/rustdoc/const-display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
issue = "0")]

#![feature(rustc_const_unstable, const_fn, foo, foo2)]
#![feature(min_const_unsafe_fn)]
#![feature(staged_api)]

// @has 'foo/fn.foo.html' '//pre' 'pub unsafe fn foo() -> u32'
Expand Down
51 changes: 43 additions & 8 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 @@ -8,27 +8,62 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// gate-test-min_const_unsafe_fn
//------------------------------------------------------------------------------
// OK
//------------------------------------------------------------------------------

// ok
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 call_unsafe_const_fn() -> i32 {
unsafe { ret_i32_no_unsafe() } //~ ERROR calls to `const unsafe fn` in const fns are unstable
unsafe { ret_i32_no_unsafe() }
}
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 call_unsafe_generic_cell_const_fn() -> *const Vec<std::cell::Cell<u32>> {
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 deref_forbidden(x: *mut usize) -> usize { *x }

const unsafe fn call_unsafe_const_unsafe_fn() -> i32 {
unsafe { ret_i32_no_unsafe() }
}
const unsafe fn call_unsafe_generic_const_unsafe_fn() -> *const String {
unsafe { ret_null_ptr_no_unsafe::<String>() }
}
const unsafe fn call_unsafe_generic_cell_const_unsafe_fn()
-> *const Vec<std::cell::Cell<u32>>
{
unsafe { ret_null_mut_ptr_no_unsafe::<Vec<std::cell::Cell<u32>>>() }
}

const unsafe fn call_unsafe_const_unsafe_fn_immediate() -> i32 {
ret_i32_no_unsafe()
}
const unsafe fn call_unsafe_generic_const_unsafe_fn_immediate() -> *const String {
ret_null_ptr_no_unsafe::<String>()
}
const unsafe fn call_unsafe_generic_cell_const_unsafe_fn_immediate()
-> *const Vec<std::cell::Cell<u32>>
{
ret_null_mut_ptr_no_unsafe::<Vec<std::cell::Cell<u32>>>()
}

//------------------------------------------------------------------------------
// NOT OK
//------------------------------------------------------------------------------

const fn bad_const_fn_deref_raw(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ is unsafe
//~^ dereferencing raw pointers in constant functions

const unsafe fn bad_const_unsafe_deref_raw(x: *mut usize) -> usize { *x }
//~^ dereferencing raw pointers in constant functions

const unsafe fn bad_const_unsafe_deref_raw_ref(x: *mut usize) -> &'static usize { &*x }
//~^ dereferencing raw pointers in constant functions

fn main() {}
Expand Down
49 changes: 25 additions & 24 deletions src/test/ui/consts/min_const_fn/min_const_fn_unsafe.stderr
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
--> $DIR/min_const_fn_unsafe.rs:31:59
--> $DIR/min_const_fn_unsafe.rs:60:77
|
LL | const unsafe fn deref_forbidden(x: *mut usize) -> usize { *x }
| ^^
LL | const fn bad_const_fn_deref_raw(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ is unsafe
| ^^^
|
= 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:38:5
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
--> $DIR/min_const_fn_unsafe.rs:63:70
|
LL | Foo { x: () }.y
| ^^^^^^^^^^^^^^^
LL | const unsafe fn bad_const_unsafe_deref_raw(x: *mut usize) -> usize { *x }
| ^^
|
= help: add #![feature(const_fn_union)] to the crate attributes to enable
= help: add #![feature(const_raw_ptr_deref)] 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:21:14
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
--> $DIR/min_const_fn_unsafe.rs:66:83
|
LL | unsafe { ret_i32_no_unsafe() } //~ ERROR calls to `const unsafe fn` in const fns are unstable
| ^^^^^^^^^^^^^^^^^^^
LL | const unsafe fn bad_const_unsafe_deref_raw_ref(x: *mut usize) -> &'static usize { &*x }
| ^^^
|
= help: add #![feature(min_const_unsafe_fn)] to the crate attributes to enable
= help: add #![feature(const_raw_ptr_deref)] 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
error[E0658]: unions in const fn are unstable (see issue #51909)
--> $DIR/min_const_fn_unsafe.rs:73:5
|
LL | unsafe { ret_null_ptr_no_unsafe::<String>() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | Foo { x: () }.y
| ^^^^^^^^^^^^^^^
|
= help: add #![feature(min_const_unsafe_fn)] to the crate attributes to enable
= help: add #![feature(const_fn_union)] 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:28:14
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/min_const_fn_unsafe.rs:60:77
|
LL | unsafe { ret_null_mut_ptr_no_unsafe::<Vec<std::cell::Cell<u32>>>() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | const fn bad_const_fn_deref_raw(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ is unsafe
| ^^^ dereference of raw pointer
|
= help: add #![feature(min_const_unsafe_fn)] to the crate attributes to enable
= 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: aborting due to 5 previous errors

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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
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")]
Expand Down
Loading

0 comments on commit 24a5335

Please sign in to comment.