Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 24a5335

Browse files
authoredDec 23, 2018
Rollup merge of rust-lang#57067 - Centril:stabilize-min_const_unsafe_fn, r=oli-obk
Stabilize min_const_unsafe_fn in 1.33 Fixes rust-lang#55607 r? @oli-obk
2 parents 841af07 + 7ae6fb2 commit 24a5335

15 files changed

+79
-184
lines changed
 

‎src/librustc/ich/impls_mir.rs

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ impl_stable_hash_for!(enum mir::BorrowKind {
4747
impl_stable_hash_for!(enum mir::UnsafetyViolationKind {
4848
General,
4949
GeneralAndConstFn,
50-
GatedConstFnCall,
5150
ExternStatic(lint_node_id),
5251
BorrowPacked(lint_node_id),
5352
});

‎src/librustc/mir/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -2792,9 +2792,6 @@ impl Location {
27922792
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
27932793
pub enum UnsafetyViolationKind {
27942794
General,
2795-
/// Right now function calls to `const unsafe fn` are only permitted behind a feature gate
2796-
/// Also, even `const unsafe fn` need an `unsafe` block to do the allowed operations.
2797-
GatedConstFnCall,
27982795
/// Permitted in const fn and regular fns
27992796
GeneralAndConstFn,
28002797
ExternStatic(ast::NodeId),

‎src/librustc_mir/transform/check_unsafety.rs

+1-28
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc::mir::visit::{PlaceContext, Visitor, MutatingUseContext};
2323

2424
use syntax::ast;
2525
use syntax::symbol::Symbol;
26-
use syntax::feature_gate::{emit_feature_err, GateIssue};
2726

2827
use std::ops::Bound;
2928

@@ -97,7 +96,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
9796
if let hir::Unsafety::Unsafe = sig.unsafety() {
9897
self.require_unsafe("call to unsafe function",
9998
"consult the function's documentation for information on how to avoid \
100-
undefined behavior", UnsafetyViolationKind::GatedConstFnCall)
99+
undefined behavior", UnsafetyViolationKind::GeneralAndConstFn)
101100
}
102101
}
103102
}
@@ -325,11 +324,6 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
325324
// compat lint
326325
violation.kind = UnsafetyViolationKind::General;
327326
},
328-
UnsafetyViolationKind::GatedConstFnCall => {
329-
// safe code can't call unsafe const fns, this `UnsafetyViolationKind`
330-
// is only relevant for `Safety::ExplicitUnsafe` in `unsafe const fn`s
331-
violation.kind = UnsafetyViolationKind::General;
332-
}
333327
}
334328
if !self.violations.contains(&violation) {
335329
self.violations.push(violation)
@@ -346,19 +340,8 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
346340
}
347341
// only some unsafety is allowed in const fn
348342
if self.min_const_fn {
349-
let min_const_unsafe_fn = self.tcx.features().min_const_unsafe_fn;
350343
for violation in violations {
351344
match violation.kind {
352-
UnsafetyViolationKind::GatedConstFnCall if min_const_unsafe_fn => {
353-
// these function calls to unsafe functions are allowed
354-
// if `#![feature(min_const_unsafe_fn)]` is active
355-
},
356-
UnsafetyViolationKind::GatedConstFnCall => {
357-
// without the feature gate, we report errors
358-
if !self.violations.contains(&violation) {
359-
self.violations.push(violation.clone())
360-
}
361-
}
362345
// these unsafe things are stable in const fn
363346
UnsafetyViolationKind::GeneralAndConstFn => {},
364347
// these things are forbidden in const fns
@@ -620,16 +603,6 @@ pub fn check_unsafety<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
620603
.note(&details.as_str()[..])
621604
.emit();
622605
}
623-
UnsafetyViolationKind::GatedConstFnCall => {
624-
emit_feature_err(
625-
&tcx.sess.parse_sess,
626-
"min_const_unsafe_fn",
627-
source_info.span,
628-
GateIssue::Language,
629-
"calls to `const unsafe fn` in const fns are unstable",
630-
);
631-
632-
}
633606
UnsafetyViolationKind::ExternStatic(lint_node_id) => {
634607
tcx.lint_node_note(SAFE_EXTERN_STATICS,
635608
lint_node_id,

‎src/libstd/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@
273273
#![feature(libc)]
274274
#![feature(link_args)]
275275
#![feature(linkage)]
276-
#![cfg_attr(not(stage0), feature(min_const_unsafe_fn))]
277276
#![feature(needs_panic_runtime)]
278277
#![feature(never_type)]
279278
#![feature(nll)]

‎src/libsyntax/feature_gate.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,6 @@ declare_features! (
486486

487487
// `extern crate self as foo;` puts local crate root into extern prelude under name `foo`.
488488
(active, extern_crate_self, "1.31.0", Some(56409), None),
489-
490-
// Allows calling `const unsafe fn` inside `unsafe` blocks in `const fn` functions.
491-
(active, min_const_unsafe_fn, "1.31.0", Some(55607), None),
492489
);
493490

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

699698
// If you change this, please modify `src/doc/unstable-book` as well. You must

‎src/test/run-pass-fulldeps/newtype_index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(rustc_attrs, rustc_private, step_trait, min_const_unsafe_fn)]
1+
#![feature(rustc_attrs, rustc_private, step_trait)]
22

33
#[macro_use] extern crate rustc_data_structures;
44
extern crate rustc_serialize;

‎src/test/rustdoc/const-display.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
issue = "0")]
1616

1717
#![feature(rustc_const_unstable, const_fn, foo, foo2)]
18-
#![feature(min_const_unsafe_fn)]
1918
#![feature(staged_api)]
2019

2120
// @has 'foo/fn.foo.html' '//pre' 'pub unsafe fn foo() -> u32'

‎src/test/ui/consts/min_const_fn/min_const_fn_unsafe.rs

+43-8
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,62 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// gate-test-min_const_unsafe_fn
11+
//------------------------------------------------------------------------------
12+
// OK
13+
//------------------------------------------------------------------------------
1214

13-
// ok
1415
const unsafe fn ret_i32_no_unsafe() -> i32 { 42 }
1516
const unsafe fn ret_null_ptr_no_unsafe<T>() -> *const T { 0 as *const T }
1617
const unsafe fn ret_null_mut_ptr_no_unsafe<T>() -> *mut T { 0 as *mut T }
1718
const fn no_unsafe() { unsafe {} }
1819

19-
// not ok
2020
const fn call_unsafe_const_fn() -> i32 {
21-
unsafe { ret_i32_no_unsafe() } //~ ERROR calls to `const unsafe fn` in const fns are unstable
21+
unsafe { ret_i32_no_unsafe() }
2222
}
2323
const fn call_unsafe_generic_const_fn() -> *const String {
2424
unsafe { ret_null_ptr_no_unsafe::<String>() }
25-
//~^ ERROR calls to `const unsafe fn` in const fns are unstable
2625
}
27-
const fn call_unsafe_generic_cell_const_fn() -> *const Vec<std::cell::Cell<u32>> {
26+
const fn call_unsafe_generic_cell_const_fn()
27+
-> *const Vec<std::cell::Cell<u32>>
28+
{
2829
unsafe { ret_null_mut_ptr_no_unsafe::<Vec<std::cell::Cell<u32>>>() }
29-
//~^ ERROR calls to `const unsafe fn` in const fns
3030
}
31-
const unsafe fn deref_forbidden(x: *mut usize) -> usize { *x }
31+
32+
const unsafe fn call_unsafe_const_unsafe_fn() -> i32 {
33+
unsafe { ret_i32_no_unsafe() }
34+
}
35+
const unsafe fn call_unsafe_generic_const_unsafe_fn() -> *const String {
36+
unsafe { ret_null_ptr_no_unsafe::<String>() }
37+
}
38+
const unsafe fn call_unsafe_generic_cell_const_unsafe_fn()
39+
-> *const Vec<std::cell::Cell<u32>>
40+
{
41+
unsafe { ret_null_mut_ptr_no_unsafe::<Vec<std::cell::Cell<u32>>>() }
42+
}
43+
44+
const unsafe fn call_unsafe_const_unsafe_fn_immediate() -> i32 {
45+
ret_i32_no_unsafe()
46+
}
47+
const unsafe fn call_unsafe_generic_const_unsafe_fn_immediate() -> *const String {
48+
ret_null_ptr_no_unsafe::<String>()
49+
}
50+
const unsafe fn call_unsafe_generic_cell_const_unsafe_fn_immediate()
51+
-> *const Vec<std::cell::Cell<u32>>
52+
{
53+
ret_null_mut_ptr_no_unsafe::<Vec<std::cell::Cell<u32>>>()
54+
}
55+
56+
//------------------------------------------------------------------------------
57+
// NOT OK
58+
//------------------------------------------------------------------------------
59+
60+
const fn bad_const_fn_deref_raw(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ is unsafe
61+
//~^ dereferencing raw pointers in constant functions
62+
63+
const unsafe fn bad_const_unsafe_deref_raw(x: *mut usize) -> usize { *x }
64+
//~^ dereferencing raw pointers in constant functions
65+
66+
const unsafe fn bad_const_unsafe_deref_raw_ref(x: *mut usize) -> &'static usize { &*x }
3267
//~^ dereferencing raw pointers in constant functions
3368

3469
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
11
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
2-
--> $DIR/min_const_fn_unsafe.rs:31:59
2+
--> $DIR/min_const_fn_unsafe.rs:60:77
33
|
4-
LL | const unsafe fn deref_forbidden(x: *mut usize) -> usize { *x }
5-
| ^^
4+
LL | const fn bad_const_fn_deref_raw(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ is unsafe
5+
| ^^^
66
|
77
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable
88

9-
error[E0658]: unions in const fn are unstable (see issue #51909)
10-
--> $DIR/min_const_fn_unsafe.rs:38:5
9+
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
10+
--> $DIR/min_const_fn_unsafe.rs:63:70
1111
|
12-
LL | Foo { x: () }.y
13-
| ^^^^^^^^^^^^^^^
12+
LL | const unsafe fn bad_const_unsafe_deref_raw(x: *mut usize) -> usize { *x }
13+
| ^^
1414
|
15-
= help: add #![feature(const_fn_union)] to the crate attributes to enable
15+
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable
1616

17-
error[E0658]: calls to `const unsafe fn` in const fns are unstable (see issue #55607)
18-
--> $DIR/min_const_fn_unsafe.rs:21:14
17+
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
18+
--> $DIR/min_const_fn_unsafe.rs:66:83
1919
|
20-
LL | unsafe { ret_i32_no_unsafe() } //~ ERROR calls to `const unsafe fn` in const fns are unstable
21-
| ^^^^^^^^^^^^^^^^^^^
20+
LL | const unsafe fn bad_const_unsafe_deref_raw_ref(x: *mut usize) -> &'static usize { &*x }
21+
| ^^^
2222
|
23-
= help: add #![feature(min_const_unsafe_fn)] to the crate attributes to enable
23+
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable
2424

25-
error[E0658]: calls to `const unsafe fn` in const fns are unstable (see issue #55607)
26-
--> $DIR/min_const_fn_unsafe.rs:24:14
25+
error[E0658]: unions in const fn are unstable (see issue #51909)
26+
--> $DIR/min_const_fn_unsafe.rs:73:5
2727
|
28-
LL | unsafe { ret_null_ptr_no_unsafe::<String>() }
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28+
LL | Foo { x: () }.y
29+
| ^^^^^^^^^^^^^^^
3030
|
31-
= help: add #![feature(min_const_unsafe_fn)] to the crate attributes to enable
31+
= help: add #![feature(const_fn_union)] to the crate attributes to enable
3232

33-
error[E0658]: calls to `const unsafe fn` in const fns are unstable (see issue #55607)
34-
--> $DIR/min_const_fn_unsafe.rs:28:14
33+
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
34+
--> $DIR/min_const_fn_unsafe.rs:60:77
3535
|
36-
LL | unsafe { ret_null_mut_ptr_no_unsafe::<Vec<std::cell::Cell<u32>>>() }
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36+
LL | const fn bad_const_fn_deref_raw(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ is unsafe
37+
| ^^^ dereference of raw pointer
3838
|
39-
= help: add #![feature(min_const_unsafe_fn)] to the crate attributes to enable
39+
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
4040

4141
error: aborting due to 5 previous errors
4242

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

‎src/test/ui/consts/min_const_fn/min_const_fn_unsafe_feature_gate.rs

-61
This file was deleted.

‎src/test/ui/consts/min_const_fn/min_const_fn_unsafe_feature_gate.stderr

-44
This file was deleted.

‎src/test/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
issue = "0")]
1515

1616
#![feature(rustc_const_unstable, const_fn, foo, foo2)]
17-
#![feature(min_const_unsafe_fn)]
1817
#![feature(staged_api)]
1918

2019
#[stable(feature = "rust1", since = "1.0.0")]

‎src/test/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
error: can only call other `min_const_fn` within a `min_const_fn`
2-
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:26:41
2+
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:25:41
33
|
44
LL | const unsafe fn bar() -> u32 { unsafe { foo() } } //~ ERROR can only call other `min_const_fn`
55
| ^^^^^
66

77
error: can only call other `min_const_fn` within a `min_const_fn`
8-
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:33:42
8+
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:32:42
99
|
1010
LL | const unsafe fn bar2() -> u32 { unsafe { foo2() } } //~ ERROR can only call other `min_const_fn`
1111
| ^^^^^^
1212

1313
error: only int, `bool` and `char` operations are stable in const fn
14-
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:37:33
14+
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:36:33
1515
|
1616
LL | const unsafe fn bar3() -> u32 { (5f32 + 6f32) as u32 } //~ ERROR only int, `bool` and `char` op
1717
| ^^^^^^^^^^^^^
1818

1919
error: can only call other `min_const_fn` within a `min_const_fn`
20-
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:45:48
20+
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:44:48
2121
|
2222
LL | const unsafe fn bar2_gated() -> u32 { unsafe { foo2_gated() } } //~ ERROR can only call other
2323
| ^^^^^^^^^^^^

‎src/test/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability2.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
issue = "0")]
1515

1616
#![feature(rustc_const_unstable, const_fn, foo, foo2)]
17-
#![feature(min_const_unsafe_fn)]
1817
#![feature(staged_api)]
1918

2019
#[stable(feature = "rust1", since = "1.0.0")]

‎src/test/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability2.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
error: can only call other `min_const_fn` within a `min_const_fn`
2-
--> $DIR/min_const_unsafe_fn_libstd_stability2.rs:26:32
2+
--> $DIR/min_const_unsafe_fn_libstd_stability2.rs:25:32
33
|
44
LL | const unsafe fn bar() -> u32 { foo() } //~ ERROR can only call other `min_const_fn`
55
| ^^^^^
66

77
error: can only call other `min_const_fn` within a `min_const_fn`
8-
--> $DIR/min_const_unsafe_fn_libstd_stability2.rs:33:33
8+
--> $DIR/min_const_unsafe_fn_libstd_stability2.rs:32:33
99
|
1010
LL | const unsafe fn bar2() -> u32 { foo2() } //~ ERROR can only call other `min_const_fn`
1111
| ^^^^^^
1212

1313
error: can only call other `min_const_fn` within a `min_const_fn`
14-
--> $DIR/min_const_unsafe_fn_libstd_stability2.rs:41:39
14+
--> $DIR/min_const_unsafe_fn_libstd_stability2.rs:40:39
1515
|
1616
LL | const unsafe fn bar2_gated() -> u32 { foo2_gated() } //~ ERROR can only call other `min_const_fn`
1717
| ^^^^^^^^^^^^

0 commit comments

Comments
 (0)
Please sign in to comment.