Skip to content

Commit 0cdbeaa

Browse files
committed
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
}

compiler/rustc_feature/src/accepted.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ declare_features! (
299299
(accepted, const_panic, "1.57.0", Some(51999), None),
300300
/// Lessens the requirements for structs to implement `Unsize`.
301301
(accepted, relaxed_struct_unsize, "1.58.0", Some(81793), None),
302+
/// Allows dereferencing raw pointers during const eval.
303+
(accepted, const_raw_ptr_deref, "1.58.0", Some(51911), None),
302304

303305
// -------------------------------------------------------------------------
304306
// feature-group-end: accepted features

compiler/rustc_feature/src/active.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,6 @@ declare_features! (
408408
/// Allows inferring `'static` outlives requirements (RFC 2093).
409409
(active, infer_static_outlives_requirements, "1.26.0", Some(54185), None),
410410

411-
/// Allows dereferencing raw pointers during const eval.
412-
(active, const_raw_ptr_deref, "1.27.0", Some(51911), None),
413-
414411
/// Allows inconsistent bounds in where clauses.
415412
(active, trivial_bounds, "1.28.0", Some(48214), None),
416413

library/core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
#![feature(const_impl_trait)]
157157
#![feature(const_mut_refs)]
158158
#![feature(const_precise_live_drops)]
159-
#![feature(const_raw_ptr_deref)]
159+
#![cfg_attr(bootstrap, feature(const_raw_ptr_deref))]
160160
#![feature(const_refs_to_cell)]
161161
#![feature(decl_macro)]
162162
#![feature(doc_cfg)]

library/core/tests/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
#![feature(const_mut_refs)]
5757
#![feature(const_pin)]
5858
#![feature(const_slice_from_raw_parts)]
59-
#![feature(const_raw_ptr_deref)]
59+
#![cfg_attr(bootstrap, feature(const_raw_ptr_deref))]
6060
#![feature(never_type)]
6161
#![feature(unwrap_infallible)]
6262
#![feature(result_into_ok_or_err)]

library/std/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@
264264
#![feature(const_ipv4)]
265265
#![feature(const_ipv6)]
266266
#![feature(const_option)]
267-
#![feature(const_raw_ptr_deref)]
267+
#![cfg_attr(bootstrap, feature(const_raw_ptr_deref))]
268+
#![cfg_attr(not(bootstrap), feature(const_mut_refs))]
268269
#![feature(const_socketaddr)]
269270
#![feature(const_trait_impl)]
270271
#![feature(container_error_extra)]

src/test/ui/consts/const-deref-ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// Check that you can't dereference raw pointers in constants.
1+
// Check that you can't dereference invalid raw pointers in constants.
22

33
fn main() {
44
static C: u64 = unsafe {*(0xdeadbeef as *const u64)};
5-
//~^ ERROR dereferencing raw pointers in statics is unstable
5+
//~^ ERROR could not evaluate static initializer
66
println!("{}", C);
77
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
error[E0658]: dereferencing raw pointers in statics is unstable
1+
error[E0080]: could not evaluate static initializer
22
--> $DIR/const-deref-ptr.rs:4:29
33
|
44
LL | static C: u64 = unsafe {*(0xdeadbeef as *const u64)};
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: see issue #51911 <https://github.com/rust-lang/rust/issues/51911> for more information
8-
= help: add `#![feature(const_raw_ptr_deref)]` to the crate attributes to enable
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 0xdeadbeef is not a valid pointer
96

107
error: aborting due to previous error
118

12-
For more information about this error, try `rustc --explain E0658`.
9+
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-eval/assign-to-static-within-other-static-2.rs

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)