Skip to content

Commit cd53b4d

Browse files
authored
Rollup merge of #101131 - RalfJung:ctfe-no-needs-rfc, r=oli-obk
CTFE: exposing pointers and calling extern fn is just impossible The remaining "needs RFC" errors are just needlessly confusing, I think -- time to get rid of that error variant. They are anyway only reachable with miri-unleashed (if at all). r? `@oli-obk`
2 parents ecd908a + f29c3c4 commit cd53b4d

File tree

5 files changed

+16
-20
lines changed

5 files changed

+16
-20
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::interpret::{
1515
/// The CTFE machine has some custom error kinds.
1616
#[derive(Clone, Debug)]
1717
pub enum ConstEvalErrKind {
18-
NeedsRfc(String),
1918
ConstAccessesStatic,
2019
ModifiedGlobal,
2120
AssertFailure(AssertKind<ConstInt>),
@@ -42,9 +41,6 @@ impl fmt::Display for ConstEvalErrKind {
4241
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4342
use self::ConstEvalErrKind::*;
4443
match *self {
45-
NeedsRfc(ref msg) => {
46-
write!(f, "\"{}\" needs an rfc before being allowed inside constants", msg)
47-
}
4844
ConstAccessesStatic => write!(f, "constant accesses static"),
4945
ModifiedGlobal => {
5046
write!(f, "modifying a static's initial value from another static's initializer")

compiler/rustc_const_eval/src/const_eval/machine.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,10 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
269269
);
270270
throw_inval!(AlreadyReported(guar));
271271
} else {
272+
// `find_mir_or_eval_fn` checks that this is a const fn before even calling us,
273+
// so this should be unreachable.
272274
let path = ecx.tcx.def_path_str(def.did);
273-
Err(ConstEvalErrKind::NeedsRfc(format!("calling extern function `{}`", path))
274-
.into())
275+
bug!("trying to call extern function `{path}` at compile-time");
275276
}
276277
}
277278
_ => Ok(ecx.tcx.instance_mir(instance)),
@@ -339,11 +340,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
339340

340341
// CTFE-specific intrinsics.
341342
let Some(ret) = target else {
342-
return Err(ConstEvalErrKind::NeedsRfc(format!(
343-
"calling intrinsic `{}`",
344-
intrinsic_name
345-
))
346-
.into());
343+
throw_unsup_format!("intrinsic `{intrinsic_name}` is not supported at compile-time");
347344
};
348345
match intrinsic_name {
349346
sym::ptr_guaranteed_eq | sym::ptr_guaranteed_ne => {
@@ -400,11 +397,9 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
400397
}
401398
}
402399
_ => {
403-
return Err(ConstEvalErrKind::NeedsRfc(format!(
404-
"calling intrinsic `{}`",
405-
intrinsic_name
406-
))
407-
.into());
400+
throw_unsup_format!(
401+
"intrinsic `{intrinsic_name}` is not supported at compile-time"
402+
);
408403
}
409404
}
410405

@@ -447,7 +442,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
447442
_left: &ImmTy<'tcx>,
448443
_right: &ImmTy<'tcx>,
449444
) -> InterpResult<'tcx, (Scalar, bool, Ty<'tcx>)> {
450-
Err(ConstEvalErrKind::NeedsRfc("pointer arithmetic or comparison".to_string()).into())
445+
throw_unsup_format!("pointer arithmetic or comparison is not supported at compile-time");
451446
}
452447

453448
fn before_terminator(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
@@ -469,7 +464,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
469464
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
470465
_ptr: Pointer<AllocId>,
471466
) -> InterpResult<'tcx> {
472-
Err(ConstEvalErrKind::NeedsRfc("exposing pointers".to_string()).into())
467+
// This is only reachable with -Zunleash-the-miri-inside-of-you.
468+
throw_unsup_format!("exposing pointers is not possible at compile-time")
473469
}
474470

475471
#[inline(always)]

compiler/rustc_const_eval/src/interpret/machine.rs

+1
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
490490
) -> InterpResult<$tcx, Pointer<Option<AllocId>>> {
491491
// Allow these casts, but make the pointer not dereferenceable.
492492
// (I.e., they behave like transmutation.)
493+
// This is correct because no pointers can ever be exposed in compile-time evaluation.
493494
Ok(Pointer::from_addr(addr))
494495
}
495496

src/test/ui/consts/miri_unleashed/ptr_arith.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
static PTR_INT_CAST: () = {
99
let x = &0 as *const _ as usize;
1010
//~^ ERROR could not evaluate static initializer
11-
//~| "exposing pointers" needs an rfc before being allowed inside constants
11+
//~| exposing pointers
1212
let _v = x == x;
1313
};
1414

@@ -19,4 +19,7 @@ static PTR_INT_TRANSMUTE: () = unsafe {
1919
//~| unable to turn pointer into raw bytes
2020
};
2121

22+
// I'd love to test pointer comparison, but that is not possible since
23+
// their `PartialEq` impl is non-`const`.
24+
2225
fn main() {}

src/test/ui/consts/miri_unleashed/ptr_arith.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: could not evaluate static initializer
22
--> $DIR/ptr_arith.rs:9:13
33
|
44
LL | let x = &0 as *const _ as usize;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^ "exposing pointers" needs an rfc before being allowed inside constants
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ exposing pointers is not possible at compile-time
66

77
error[E0080]: could not evaluate static initializer
88
--> $DIR/ptr_arith.rs:17:14

0 commit comments

Comments
 (0)