Skip to content

Commit 5f0dd6d

Browse files
committed
remove const_raw_ptr_to_usize_cast feature
1 parent a84d1b2 commit 5f0dd6d

31 files changed

+65
-315
lines changed

compiler/rustc_feature/src/active.rs

-3
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,6 @@ declare_features! (
416416
/// Allows accessing fields of unions inside `const` functions.
417417
(active, const_fn_union, "1.27.0", Some(51909), None),
418418

419-
/// Allows casting raw pointers to `usize` during const eval.
420-
(active, const_raw_ptr_to_usize_cast, "1.27.0", Some(51910), None),
421-
422419
/// Allows dereferencing raw pointers during const eval.
423420
(active, const_raw_ptr_deref, "1.27.0", Some(51911), None),
424421

compiler/rustc_feature/src/removed.rs

+4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ declare_features! (
144144
(removed, external_doc, "1.54.0", Some(44732), None,
145145
Some("use #[doc = include_str!(\"filename\")] instead, which handles macro invocations")),
146146

147+
/// Allows casting raw pointers to `usize` during const eval.
148+
(removed, const_raw_ptr_to_usize_cast, "1.55.0", Some(51910), None,
149+
Some("at compile-time, pointers do not have an integer value, so these casts cannot be properly supported")),
150+
147151
// -------------------------------------------------------------------------
148152
// feature-group-end: removed features
149153
// -------------------------------------------------------------------------

compiler/rustc_mir/src/transform/check_consts/ops.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ impl NonConstOp for PanicNonStr {
397397
}
398398
}
399399

400+
/// Comparing raw pointers for equality.
401+
/// Not currently intended to ever be allowed, even behind a feature gate: operation depends on
402+
/// allocation base addresses that are not known at compile-time.
400403
#[derive(Debug)]
401404
pub struct RawPtrComparison;
402405
impl NonConstOp for RawPtrComparison {
@@ -430,20 +433,22 @@ impl NonConstOp for RawPtrDeref {
430433
}
431434
}
432435

436+
/// Casting raw pointer or function pointer to an integer.
437+
/// Not currently intended to ever be allowed, even behind a feature gate: operation depends on
438+
/// allocation base addresses that are not known at compile-time.
433439
#[derive(Debug)]
434440
pub struct RawPtrToIntCast;
435441
impl NonConstOp for RawPtrToIntCast {
436-
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
437-
Status::Unstable(sym::const_raw_ptr_to_usize_cast)
438-
}
439-
440442
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
441-
feature_err(
442-
&ccx.tcx.sess.parse_sess,
443-
sym::const_raw_ptr_to_usize_cast,
444-
span,
445-
&format!("casting pointers to integers in {}s is unstable", ccx.const_kind(),),
446-
)
443+
let mut err = ccx
444+
.tcx
445+
.sess
446+
.struct_span_err(span, "pointers cannot be cast to integers during const eval.");
447+
err.note("at compile-time, pointers do not have an integer value");
448+
err.note(
449+
"avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior",
450+
);
451+
err
447452
}
448453
}
449454

compiler/rustc_mir/src/transform/check_unsafety.rs

+1-30
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_hir::intravisit;
77
use rustc_hir::Node;
88
use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
99
use rustc_middle::mir::*;
10-
use rustc_middle::ty::cast::CastTy;
1110
use rustc_middle::ty::query::Providers;
1211
use rustc_middle::ty::{self, TyCtxt};
1312
use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE};
@@ -18,7 +17,6 @@ use std::ops::Bound;
1817
pub struct UnsafetyChecker<'a, 'tcx> {
1918
body: &'a Body<'tcx>,
2019
body_did: LocalDefId,
21-
const_context: bool,
2220
violations: Vec<UnsafetyViolation>,
2321
source_info: SourceInfo,
2422
tcx: TyCtxt<'tcx>,
@@ -30,7 +28,6 @@ pub struct UnsafetyChecker<'a, 'tcx> {
3028

3129
impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
3230
fn new(
33-
const_context: bool,
3431
body: &'a Body<'tcx>,
3532
body_did: LocalDefId,
3633
tcx: TyCtxt<'tcx>,
@@ -39,7 +36,6 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
3936
Self {
4037
body,
4138
body_did,
42-
const_context,
4339
violations: vec![],
4440
source_info: SourceInfo::outermost(body.span),
4541
tcx,
@@ -136,25 +132,6 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
136132
self.register_violations(&violations, &unsafe_blocks);
137133
}
138134
},
139-
// casting pointers to ints is unsafe in const fn because the const evaluator cannot
140-
// possibly know what the result of various operations like `address / 2` would be
141-
// pointers during const evaluation have no integral address, only an abstract one
142-
Rvalue::Cast(CastKind::Misc, ref operand, cast_ty)
143-
if self.const_context && self.tcx.features().const_raw_ptr_to_usize_cast =>
144-
{
145-
let operand_ty = operand.ty(self.body, self.tcx);
146-
let cast_in = CastTy::from_ty(operand_ty).expect("bad input type for cast");
147-
let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
148-
match (cast_in, cast_out) {
149-
(CastTy::Ptr(_) | CastTy::FnPtr, CastTy::Int(_)) => {
150-
self.require_unsafe(
151-
UnsafetyViolationKind::General,
152-
UnsafetyViolationDetails::CastOfPointerToInt,
153-
);
154-
}
155-
_ => {}
156-
}
157-
}
158135
_ => {}
159136
}
160137
self.super_rvalue(rvalue, location);
@@ -469,13 +446,7 @@ fn unsafety_check_result<'tcx>(
469446

470447
let param_env = tcx.param_env(def.did);
471448

472-
let id = tcx.hir().local_def_id_to_hir_id(def.did);
473-
let const_context = match tcx.hir().body_owner_kind(id) {
474-
hir::BodyOwnerKind::Closure => false,
475-
hir::BodyOwnerKind::Fn => tcx.is_const_fn_raw(def.did.to_def_id()),
476-
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => true,
477-
};
478-
let mut checker = UnsafetyChecker::new(const_context, body, def.did, tcx, param_env);
449+
let mut checker = UnsafetyChecker::new(body, def.did, tcx, param_env);
479450
checker.visit_body(&body);
480451

481452
check_unused_unsafe(tcx, def.did, &checker.used_unsafe, &mut checker.inherited_blocks);

compiler/rustc_mir_build/src/check_unsafety.rs

-21
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ struct UnsafetyVisitor<'a, 'tcx> {
2525
/// The `#[target_feature]` attributes of the body. Used for checking
2626
/// calls to functions with `#[target_feature]` (RFC 2396).
2727
body_target_features: &'tcx Vec<Symbol>,
28-
is_const: bool,
2928
in_possible_lhs_union_assign: bool,
3029
in_union_destructure: bool,
3130
}
@@ -315,16 +314,6 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
315314
(Bound::Unbounded, Bound::Unbounded) => {}
316315
_ => self.requires_unsafe(expr.span, InitializingTypeWith),
317316
},
318-
ExprKind::Cast { source } => {
319-
let source = &self.thir[source];
320-
if self.tcx.features().const_raw_ptr_to_usize_cast
321-
&& self.is_const
322-
&& (source.ty.is_unsafe_ptr() || source.ty.is_fn_ptr())
323-
&& expr.ty.is_integral()
324-
{
325-
self.requires_unsafe(expr.span, CastOfPointerToInt);
326-
}
327-
}
328317
ExprKind::Closure {
329318
closure_id,
330319
substs: _,
@@ -413,7 +402,6 @@ enum UnsafeOpKind {
413402
CallToUnsafeFunction,
414403
UseOfInlineAssembly,
415404
InitializingTypeWith,
416-
CastOfPointerToInt,
417405
UseOfMutableStatic,
418406
UseOfExternStatic,
419407
DerefOfRawPointer,
@@ -446,9 +434,6 @@ impl UnsafeOpKind {
446434
"initializing a layout restricted type's field with a value outside the valid \
447435
range is undefined behavior",
448436
),
449-
CastOfPointerToInt => {
450-
("cast of pointer to int", "casting pointers to integers in constants")
451-
}
452437
UseOfMutableStatic => (
453438
"use of mutable static",
454439
"mutable statics can be mutated by multiple threads: aliasing violations or data \
@@ -526,19 +511,13 @@ pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalD
526511
let body_target_features = &tcx.codegen_fn_attrs(def.did).target_features;
527512
let safety_context =
528513
if body_unsafety.is_unsafe() { SafetyContext::UnsafeFn } else { SafetyContext::Safe };
529-
let is_const = match tcx.hir().body_owner_kind(hir_id) {
530-
hir::BodyOwnerKind::Closure => false,
531-
hir::BodyOwnerKind::Fn => tcx.is_const_fn_raw(def.did.to_def_id()),
532-
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => true,
533-
};
534514
let mut visitor = UnsafetyVisitor {
535515
tcx,
536516
thir,
537517
safety_context,
538518
hir_context: hir_id,
539519
body_unsafety,
540520
body_target_features,
541-
is_const,
542521
in_possible_lhs_union_assign: false,
543522
in_union_destructure: false,
544523
};

src/test/ui/cast/cast-ptr-to-int-const.mir.stderr

-19
This file was deleted.

src/test/ui/cast/cast-ptr-to-int-const.rs

-19
This file was deleted.

src/test/ui/cast/cast-ptr-to-int-const.thir.stderr

-19
This file was deleted.

src/test/ui/cast/feature-gate-const_raw_ptr_to_usize_cast.rs

-13
This file was deleted.

src/test/ui/cast/feature-gate-const_raw_ptr_to_usize_cast.stderr

-30
This file was deleted.

src/test/ui/const-ptr/ptr_to_usize_cast.rs

-13
This file was deleted.

src/test/ui/const-ptr/ptr_to_usize_cast.stderr

-14
This file was deleted.

src/test/ui/consts/const-eval/const_raw_ptr_ops2.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
#![feature(const_raw_ptr_to_usize_cast, const_raw_ptr_deref)]
1+
#![feature(const_raw_ptr_deref)]
22

33
fn main() {}
44

5-
// unconst and fine
6-
const Y: usize = unsafe { 42usize as *const i32 as usize + 1 };
7-
// unconst and bad, will thus error in miri
8-
const Y2: usize = unsafe { &1 as *const i32 as usize + 1 }; //~ ERROR any use of this
9-
//~| WARN this was previously accepted by the compiler but is being phased out
10-
// unconst and fine
5+
// fine
116
const Z: i32 = unsafe { *(&1 as *const i32) };
12-
// unconst and bad, will thus error in miri
7+
8+
// bad, will thus error in miri
139
const Z2: i32 = unsafe { *(42 as *const i32) }; //~ ERROR any use of this value will cause
1410
//~| WARN this was previously accepted by the compiler but is being phased out
1511
const Z3: i32 = unsafe { *(44 as *const i32) }; //~ ERROR any use of this value will cause
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
error: any use of this value will cause an error
2-
--> $DIR/const_raw_ptr_ops2.rs:8:28
3-
|
4-
LL | const Y2: usize = unsafe { &1 as *const i32 as usize + 1 };
5-
| ---------------------------^^^^^^^^^^^^^^^^^^^^^^^^^-------
6-
| |
7-
| cannot cast pointer to integer because it was not created by cast from integer
8-
|
9-
= note: `#[deny(const_err)]` on by default
10-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
11-
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
12-
13-
error: any use of this value will cause an error
14-
--> $DIR/const_raw_ptr_ops2.rs:13:26
2+
--> $DIR/const_raw_ptr_ops2.rs:9:26
153
|
164
LL | const Z2: i32 = unsafe { *(42 as *const i32) };
175
| -------------------------^^^^^^^^^^^^^^^^^^^---
186
| |
197
| unable to turn bytes into a pointer
208
|
9+
= note: `#[deny(const_err)]` on by default
2110
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2211
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
2312

2413
error: any use of this value will cause an error
25-
--> $DIR/const_raw_ptr_ops2.rs:15:26
14+
--> $DIR/const_raw_ptr_ops2.rs:11:26
2615
|
2716
LL | const Z3: i32 = unsafe { *(44 as *const i32) };
2817
| -------------------------^^^^^^^^^^^^^^^^^^^---
@@ -32,5 +21,5 @@ LL | const Z3: i32 = unsafe { *(44 as *const i32) };
3221
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3322
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
3423

35-
error: aborting due to 3 previous errors
24+
error: aborting due to 2 previous errors
3625

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn main() {
22
[(); { &loop { break } as *const _ as usize } ];
3-
//~^ ERROR casting pointers to integers in constants is unstable
3+
//~^ ERROR pointers cannot be cast to integers during const eval
44
}

0 commit comments

Comments
 (0)