Skip to content

Commit 8d0b64f

Browse files
committed
Make const unsafe fn bodies unsafe
1 parent 4c0116e commit 8d0b64f

File tree

8 files changed

+33
-131
lines changed

8 files changed

+33
-131
lines changed

src/libcore/num/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
7070
#[stable(feature = "nonzero", since = "1.28.0")]
7171
#[inline]
7272
pub const unsafe fn new_unchecked(n: $Int) -> Self {
73-
$Ty(unsafe { NonZero(n) })
73+
$Ty(NonZero(n))
7474
}
7575

7676
/// Create a non-zero if the given value is not zero.

src/libcore/ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2928,7 +2928,7 @@ impl<T: ?Sized> NonNull<T> {
29282928
#[stable(feature = "nonnull", since = "1.25.0")]
29292929
#[inline]
29302930
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
2931-
NonNull { pointer: unsafe { NonZero(ptr as _) } }
2931+
NonNull { pointer: NonZero(ptr as _) }
29322932
}
29332933

29342934
/// Creates a new `NonNull` if `ptr` is non-null.

src/librustc_mir/build/mod.rs

-7
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,6 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'t
111111

112112
let safety = match fn_sig.unsafety {
113113
hir::Unsafety::Normal => Safety::Safe,
114-
hir::Unsafety::Unsafe if tcx.is_min_const_fn(fn_def_id) => {
115-
// As specified in #55607, a `const unsafe fn` differs
116-
// from an `unsafe fn` in that its body is still considered
117-
// safe code by default.
118-
assert!(implicit_argument.is_none());
119-
Safety::Safe
120-
},
121114
hir::Unsafety::Unsafe => Safety::FnUnsafe,
122115
};
123116

src/librustc_mir/transform/check_unsafety.rs

+5-24
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,9 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
311311
violations: &[UnsafetyViolation],
312312
unsafe_blocks: &[(ast::NodeId, bool)]) {
313313
let safety = self.source_scope_local_data[self.source_info.scope].safety;
314-
let within_unsafe = match (safety, self.min_const_fn) {
315-
// Erring on the safe side, pun intended
316-
(Safety::BuiltinUnsafe, true) |
317-
// mir building encodes const fn bodies as safe, even for `const unsafe fn`
318-
(Safety::FnUnsafe, true) => bug!("const unsafe fn body treated as inherently unsafe"),
314+
let within_unsafe = match safety {
319315
// `unsafe` blocks are required in safe code
320-
(Safety::Safe, _) => {
316+
Safety::Safe => {
321317
for violation in violations {
322318
let mut violation = violation.clone();
323319
match violation.kind {
@@ -342,9 +338,9 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
342338
}
343339
false
344340
}
345-
// regular `unsafe` function bodies allow unsafe without additional unsafe blocks
346-
(Safety::BuiltinUnsafe, false) | (Safety::FnUnsafe, false) => true,
347-
(Safety::ExplicitUnsafe(node_id), _) => {
341+
// `unsafe` function bodies allow unsafe without additional unsafe blocks
342+
Safety::BuiltinUnsafe | Safety::FnUnsafe => true,
343+
Safety::ExplicitUnsafe(node_id) => {
348344
// mark unsafe block as used if there are any unsafe operations inside
349345
if !violations.is_empty() {
350346
self.used_unsafe.insert(node_id);
@@ -616,21 +612,6 @@ pub fn check_unsafety<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
616612
} in violations.iter() {
617613
// Report an error.
618614
match kind {
619-
UnsafetyViolationKind::General if tcx.is_min_const_fn(def_id) => {
620-
let mut err = tcx.sess.struct_span_err(
621-
source_info.span,
622-
&format!("{} is unsafe and unsafe operations \
623-
are not allowed in const fn", description));
624-
err.span_label(source_info.span, &description.as_str()[..])
625-
.note(&details.as_str()[..]);
626-
if tcx.fn_sig(def_id).unsafety() == hir::Unsafety::Unsafe {
627-
err.note(
628-
"unsafe action within a `const unsafe fn` still require an `unsafe` \
629-
block in contrast to regular `unsafe fn`."
630-
);
631-
}
632-
err.emit();
633-
}
634615
UnsafetyViolationKind::GeneralAndConstFn |
635616
UnsafetyViolationKind::General => {
636617
struct_span_err!(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ const fn call_unsafe_generic_cell_const_fn() -> *const Vec<std::cell::Cell<u32>>
2828
unsafe { ret_null_mut_ptr_no_unsafe::<Vec<std::cell::Cell<u32>>>() }
2929
//~^ ERROR calls to `const unsafe fn` in const fns
3030
}
31-
const unsafe fn deref_forbidden(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
31+
const unsafe fn deref_forbidden(x: *mut usize) -> usize { *x }
3232
//~^ dereferencing raw pointers in constant functions
3333

3434
fn main() {}
3535

3636
const unsafe fn no_union() {
3737
union Foo { x: (), y: () }
38-
Foo { x: () }.y //~ ERROR not allowed in const fn
38+
Foo { x: () }.y
3939
//~^ unions in const fn
4040
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
22
--> $DIR/min_const_fn_unsafe.rs:31:59
33
|
4-
LL | const unsafe fn deref_forbidden(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
4+
LL | const unsafe fn deref_forbidden(x: *mut usize) -> usize { *x }
55
| ^^
66
|
77
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable
88

99
error[E0658]: unions in const fn are unstable (see issue #51909)
1010
--> $DIR/min_const_fn_unsafe.rs:38:5
1111
|
12-
LL | Foo { x: () }.y //~ ERROR not allowed in const fn
12+
LL | Foo { x: () }.y
1313
| ^^^^^^^^^^^^^^^
1414
|
1515
= help: add #![feature(const_fn_union)] to the crate attributes to enable
@@ -38,24 +38,6 @@ LL | unsafe { ret_null_mut_ptr_no_unsafe::<Vec<std::cell::Cell<u32>>>() }
3838
|
3939
= help: add #![feature(min_const_unsafe_fn)] to the crate attributes to enable
4040

41-
error: dereference of raw pointer is unsafe and unsafe operations are not allowed in const fn
42-
--> $DIR/min_const_fn_unsafe.rs:31:59
43-
|
44-
LL | const unsafe fn deref_forbidden(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
45-
| ^^ dereference of raw pointer
46-
|
47-
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
48-
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
49-
50-
error: access to union field is unsafe and unsafe operations are not allowed in const fn
51-
--> $DIR/min_const_fn_unsafe.rs:38:5
52-
|
53-
LL | Foo { x: () }.y //~ ERROR not allowed in const fn
54-
| ^^^^^^^^^^^^^^^ access to union field
55-
|
56-
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
57-
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
58-
59-
error: aborting due to 7 previous errors
41+
error: aborting due to 5 previous errors
6042

6143
For more information about this error, try `rustc --explain E0658`.

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

+7-8
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,28 @@ const unsafe fn foo9_3() -> *const String {
3434
const unsafe fn foo10_3() -> *const Vec<std::cell::Cell<u32>> {
3535
unsafe { foo6::<Vec<std::cell::Cell<u32>>>() }
3636
}
37-
// not ok
3837
const unsafe fn foo8_2() -> i32 {
39-
foo4() //~ ERROR not allowed in const fn
38+
foo4()
4039
}
4140
const unsafe fn foo9_2() -> *const String {
42-
foo5::<String>() //~ ERROR not allowed in const fn
41+
foo5::<String>()
4342
}
4443
const unsafe fn foo10_2() -> *const Vec<std::cell::Cell<u32>> {
45-
foo6::<Vec<std::cell::Cell<u32>>>() //~ ERROR not allowed in const fn
44+
foo6::<Vec<std::cell::Cell<u32>>>()
4645
}
47-
const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
46+
const unsafe fn foo30_3(x: *mut usize) -> usize { *x }
4847
//~^ dereferencing raw pointers in constant functions
4948

50-
const unsafe fn foo30_4(x: *mut usize) -> &'static usize { &*x } //~ ERROR not allowed in const fn
49+
const unsafe fn foo30_4(x: *mut usize) -> &'static usize { &*x }
5150
//~^ dereferencing raw pointers in constant functions
5251

53-
const fn foo30_5(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ ERROR not allowed
52+
const fn foo30_5(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ is unsafe
5453
//~^ dereferencing raw pointers in constant functions
5554

5655
fn main() {}
5756

5857
const unsafe fn no_union() {
5958
union Foo { x: (), y: () }
60-
Foo { x: () }.y //~ ERROR not allowed in const fn
59+
Foo { x: () }.y
6160
//~^ unions in const fn
6261
}
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,44 @@
11
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
2-
--> $DIR/min_const_fn_unsafe_feature_gate.rs:47:51
2+
--> $DIR/min_const_fn_unsafe_feature_gate.rs:46:51
33
|
4-
LL | const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
4+
LL | const unsafe fn foo30_3(x: *mut usize) -> usize { *x }
55
| ^^
66
|
77
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable
88

99
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
10-
--> $DIR/min_const_fn_unsafe_feature_gate.rs:50:60
10+
--> $DIR/min_const_fn_unsafe_feature_gate.rs:49:60
1111
|
12-
LL | const unsafe fn foo30_4(x: *mut usize) -> &'static usize { &*x } //~ ERROR not allowed in const fn
12+
LL | const unsafe fn foo30_4(x: *mut usize) -> &'static usize { &*x }
1313
| ^^^
1414
|
1515
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable
1616

1717
error[E0658]: dereferencing raw pointers in constant functions is unstable (see issue #51911)
18-
--> $DIR/min_const_fn_unsafe_feature_gate.rs:53:62
18+
--> $DIR/min_const_fn_unsafe_feature_gate.rs:52:62
1919
|
20-
LL | const fn foo30_5(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ ERROR not allowed
20+
LL | const fn foo30_5(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ is unsafe
2121
| ^^^
2222
|
2323
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable
2424

2525
error[E0658]: unions in const fn are unstable (see issue #51909)
26-
--> $DIR/min_const_fn_unsafe_feature_gate.rs:60:5
26+
--> $DIR/min_const_fn_unsafe_feature_gate.rs:59:5
2727
|
28-
LL | Foo { x: () }.y //~ ERROR not allowed in const fn
28+
LL | Foo { x: () }.y
2929
| ^^^^^^^^^^^^^^^
3030
|
3131
= help: add #![feature(const_fn_union)] to the crate attributes to enable
3232

33-
error: call to unsafe function is unsafe and unsafe operations are not allowed in const fn
34-
--> $DIR/min_const_fn_unsafe_feature_gate.rs:39:5
33+
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
34+
--> $DIR/min_const_fn_unsafe_feature_gate.rs:52:62
3535
|
36-
LL | foo4() //~ ERROR not allowed in const fn
37-
| ^^^^^^ call to unsafe function
38-
|
39-
= note: consult the function's documentation for information on how to avoid undefined behavior
40-
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
41-
42-
error: call to unsafe function is unsafe and unsafe operations are not allowed in const fn
43-
--> $DIR/min_const_fn_unsafe_feature_gate.rs:42:5
44-
|
45-
LL | foo5::<String>() //~ ERROR not allowed in const fn
46-
| ^^^^^^^^^^^^^^^^ call to unsafe function
47-
|
48-
= note: consult the function's documentation for information on how to avoid undefined behavior
49-
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
50-
51-
error: call to unsafe function is unsafe and unsafe operations are not allowed in const fn
52-
--> $DIR/min_const_fn_unsafe_feature_gate.rs:45:5
53-
|
54-
LL | foo6::<Vec<std::cell::Cell<u32>>>() //~ ERROR not allowed in const fn
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
56-
|
57-
= note: consult the function's documentation for information on how to avoid undefined behavior
58-
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
59-
60-
error: dereference of raw pointer is unsafe and unsafe operations are not allowed in const fn
61-
--> $DIR/min_const_fn_unsafe_feature_gate.rs:47:51
62-
|
63-
LL | const unsafe fn foo30_3(x: *mut usize) -> usize { *x } //~ ERROR not allowed in const fn
64-
| ^^ dereference of raw pointer
65-
|
66-
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
67-
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
68-
69-
error: dereference of raw pointer is unsafe and unsafe operations are not allowed in const fn
70-
--> $DIR/min_const_fn_unsafe_feature_gate.rs:50:60
71-
|
72-
LL | const unsafe fn foo30_4(x: *mut usize) -> &'static usize { &*x } //~ ERROR not allowed in const fn
73-
| ^^^ dereference of raw pointer
74-
|
75-
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
76-
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
77-
78-
error: dereference of raw pointer is unsafe and unsafe operations are not allowed in const fn
79-
--> $DIR/min_const_fn_unsafe_feature_gate.rs:53:62
80-
|
81-
LL | const fn foo30_5(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ ERROR not allowed
36+
LL | const fn foo30_5(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ is unsafe
8237
| ^^^ dereference of raw pointer
8338
|
8439
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
8540

86-
error: access to union field is unsafe and unsafe operations are not allowed in const fn
87-
--> $DIR/min_const_fn_unsafe_feature_gate.rs:60:5
88-
|
89-
LL | Foo { x: () }.y //~ ERROR not allowed in const fn
90-
| ^^^^^^^^^^^^^^^ access to union field
91-
|
92-
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
93-
= note: unsafe action within a `const unsafe fn` still require an `unsafe` block in contrast to regular `unsafe fn`.
94-
95-
error: aborting due to 11 previous errors
41+
error: aborting due to 5 previous errors
9642

97-
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`.

0 commit comments

Comments
 (0)