Skip to content

Commit 8eb805c

Browse files
authored
Rollup merge of rust-lang#75578 - 5M1Sec:master, r=oli-obk
Allowing raw ptr dereference in const fn Reflect on issue rust-lang#75340 Discussion in previous PR rust-lang#75425 ## Updates Change `UnsafetyViolationKind::General` to `UnsafetyViolationKind::GeneralAndConstFn` in check_unsafety.rs Remove `unsafe` in min_const_fn_unsafe_bad.rs Bless min_const_fn Add the test case from issue 75340 *** Sorry for the chaos. I messed up and ended up deleting the repo in the last PR. I have to create a new PR for the new repo. I will make a feature branch next time. I will edit the old PR once I receive the commends. @RalfJung Thank you all for your replies. They are helpful! r? @oli-obk
2 parents d18719b + be1fc40 commit 8eb805c

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/librustc_mir/transform/check_unsafety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
228228
let base_ty = Place::ty_from(place.local, proj_base, self.body, self.tcx).ty;
229229
match base_ty.kind {
230230
ty::RawPtr(..) => self.require_unsafe(
231-
UnsafetyViolationKind::General,
231+
UnsafetyViolationKind::GeneralAndConstFn,
232232
UnsafetyViolationDetails::DerefOfRawPointer,
233233
),
234234
ty::Adt(adt, _) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// check-pass
2+
#![feature(const_raw_ptr_deref)]
3+
#![feature(raw_ref_macros)]
4+
5+
use std::ptr;
6+
7+
const fn test_fn(x: *const i32) {
8+
let x2 = unsafe { ptr::raw_const!(*x) };
9+
}
10+
11+
fn main() {}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fn bad_const_fn_deref_raw(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ is unsafe
1+
const fn bad_const_fn_deref_raw(x: *mut usize) -> &'static usize { unsafe { &*x } }
22
//~^ dereferencing raw pointers in constant functions
33

44
const unsafe fn bad_const_unsafe_deref_raw(x: *mut usize) -> usize { *x }

src/test/ui/consts/min_const_fn/min_const_fn_unsafe_bad.stderr

+3-11
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,7 @@ LL | Foo { x: () }.y
3434
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
3535
= help: add `#![feature(const_fn)]` to the crate attributes to enable
3636

37-
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
38-
--> $DIR/min_const_fn_unsafe_bad.rs:1:77
39-
|
40-
LL | const fn bad_const_fn_deref_raw(x: *mut usize) -> &'static usize { unsafe { &*x } }
41-
| ^^^ dereference of raw pointer
42-
|
43-
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
44-
45-
error: aborting due to 5 previous errors
37+
error: aborting due to 4 previous errors
4638

47-
Some errors have detailed explanations: E0133, E0658, E0723.
48-
For more information about an error, try `rustc --explain E0133`.
39+
Some errors have detailed explanations: E0658, E0723.
40+
For more information about an error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)