Skip to content

Commit 0cdbeaa

Browse files
committedNov 6, 2021
Stabilize const_raw_ptr_deref for *const T
This stabilizes dereferencing immutable raw pointers in const contexts. It does not stabilize `*mut T` dereferencing. This is placed behind the `const_raw_mut_ptr_deref` feature gate.
1 parent 5ec7d1d commit 0cdbeaa

File tree

62 files changed

+114
-193
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+114
-193
lines changed
 

‎compiler/rustc_const_eval/src/transform/check_consts/check.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
725725
match elem {
726726
ProjectionElem::Deref => {
727727
let base_ty = Place::ty_from(place_local, proj_base, self.body, self.tcx).ty;
728-
if let ty::RawPtr(_) = base_ty.kind() {
728+
if base_ty.is_unsafe_ptr() {
729729
if proj_base.is_empty() {
730730
let decl = &self.body.local_decls[place_local];
731731
if let Some(box LocalInfo::StaticRef { def_id, .. }) = decl.local_info {
@@ -734,7 +734,13 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
734734
return;
735735
}
736736
}
737-
self.check_op(ops::RawPtrDeref);
737+
738+
// `*const T` is stable, `*mut T` is not
739+
if !base_ty.is_mutable_ptr() {
740+
return;
741+
}
742+
743+
self.check_op(ops::RawMutPtrDeref);
738744
}
739745

740746
if context.is_mutating_use() {

‎compiler/rustc_const_eval/src/transform/check_consts/ops.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,18 +400,18 @@ impl NonConstOp for RawPtrComparison {
400400
}
401401

402402
#[derive(Debug)]
403-
pub struct RawPtrDeref;
404-
impl NonConstOp for RawPtrDeref {
403+
pub struct RawMutPtrDeref;
404+
impl NonConstOp for RawMutPtrDeref {
405405
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
406-
Status::Unstable(sym::const_raw_ptr_deref)
406+
Status::Unstable(sym::const_mut_refs)
407407
}
408408

409409
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
410410
feature_err(
411411
&ccx.tcx.sess.parse_sess,
412-
sym::const_raw_ptr_deref,
412+
sym::const_mut_refs,
413413
span,
414-
&format!("dereferencing raw pointers in {}s is unstable", ccx.const_kind(),),
414+
&format!("dereferencing raw mutable pointers in {}s is unstable", ccx.const_kind(),),
415415
)
416416
}
417417
}

0 commit comments

Comments
 (0)