Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing raw ptr dereference in const fn #75578

Merged
merged 1 commit into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
let base_ty = Place::ty_from(place.local, proj_base, self.body, self.tcx).ty;
match base_ty.kind {
ty::RawPtr(..) => self.require_unsafe(
UnsafetyViolationKind::General,
UnsafetyViolationKind::GeneralAndConstFn,
UnsafetyViolationDetails::DerefOfRawPointer,
),
ty::Adt(adt, _) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// check-pass
#![feature(const_raw_ptr_deref)]
#![feature(raw_ref_macros)]

use std::ptr;

const fn test_fn(x: *const i32) {
let x2 = unsafe { ptr::raw_const!(*x) };
}

fn main() {}
2 changes: 1 addition & 1 deletion src/test/ui/consts/min_const_fn/min_const_fn_unsafe_bad.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fn bad_const_fn_deref_raw(x: *mut usize) -> &'static usize { unsafe { &*x } } //~ is unsafe
const fn bad_const_fn_deref_raw(x: *mut usize) -> &'static usize { unsafe { &*x } }
//~^ dereferencing raw pointers in constant functions

const unsafe fn bad_const_unsafe_deref_raw(x: *mut usize) -> usize { *x }
Expand Down
14 changes: 3 additions & 11 deletions src/test/ui/consts/min_const_fn/min_const_fn_unsafe_bad.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,7 @@ LL | Foo { x: () }.y
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
= help: add `#![feature(const_fn)]` to the crate attributes to enable

error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
--> $DIR/min_const_fn_unsafe_bad.rs:1:77
|
LL | const fn bad_const_fn_deref_raw(x: *mut usize) -> &'static usize { unsafe { &*x } }
| ^^^ 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: aborting due to 5 previous errors
error: aborting due to 4 previous errors

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