Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c34ac87

Browse files
committed
Auto merge of rust-lang#89247 - fee1-dead:const-eval-select, r=oli-obk
Add `const_eval_select` intrinsic Adds an intrinsic that calls a given function when evaluated at compiler time, but generates a call to another function when called at runtime. See rust-lang/const-eval#7 for previous discussion. r? `@oli-obk.`
2 parents 7807a69 + 11fac09 commit c34ac87

File tree

22 files changed

+372
-39
lines changed

22 files changed

+372
-39
lines changed

compiler/rustc_codegen_cranelift/src/abi/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,13 @@ pub(crate) fn codegen_terminator_call<'tcx>(
309309
span: Span,
310310
func: &Operand<'tcx>,
311311
args: &[Operand<'tcx>],
312-
destination: Option<(Place<'tcx>, BasicBlock)>,
312+
mir_dest: Option<(Place<'tcx>, BasicBlock)>,
313313
) {
314314
let fn_ty = fx.monomorphize(func.ty(fx.mir, fx.tcx));
315315
let fn_sig =
316316
fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_ty.fn_sig(fx.tcx));
317317

318-
let destination = destination.map(|(place, bb)| (codegen_place(fx, place), bb));
318+
let destination = mir_dest.map(|(place, bb)| (codegen_place(fx, place), bb));
319319

320320
// Handle special calls like instrinsics and empty drop glue.
321321
let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() {

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,9 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
407407
destination: Option<(CPlace<'tcx>, BasicBlock)>,
408408
span: Span,
409409
) {
410-
let def_id = instance.def_id();
410+
let intrinsic = fx.tcx.item_name(instance.def_id());
411411
let substs = instance.substs;
412412

413-
let intrinsic = fx.tcx.item_name(def_id);
414-
415413
let ret = match destination {
416414
Some((place, _)) => place,
417415
None => {

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,35 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
2626
/// "Intercept" a function call to a panic-related function
2727
/// because we have something special to do for it.
2828
/// If this returns successfully (`Ok`), the function should just be evaluated normally.
29-
fn hook_panic_fn(
29+
fn hook_special_const_fn(
3030
&mut self,
3131
instance: ty::Instance<'tcx>,
3232
args: &[OpTy<'tcx>],
33+
is_const_fn: bool,
3334
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
3435
// The list of functions we handle here must be in sync with
35-
// `is_lang_panic_fn` in `transform/check_consts/mod.rs`.
36+
// `is_lang_special_const_fn` in `transform/check_consts/mod.rs`.
3637
let def_id = instance.def_id();
38+
39+
if is_const_fn {
40+
if Some(def_id) == self.tcx.lang_items().const_eval_select() {
41+
// redirect to const_eval_select_ct
42+
if let Some(const_eval_select) = self.tcx.lang_items().const_eval_select_ct() {
43+
return Ok(Some(
44+
ty::Instance::resolve(
45+
*self.tcx,
46+
ty::ParamEnv::reveal_all(),
47+
const_eval_select,
48+
instance.substs,
49+
)
50+
.unwrap()
51+
.unwrap(),
52+
));
53+
}
54+
}
55+
return Ok(None);
56+
}
57+
3758
if Some(def_id) == self.tcx.lang_items().panic_fn()
3859
|| Some(def_id) == self.tcx.lang_items().panic_str()
3960
|| Some(def_id) == self.tcx.lang_items().panic_display()
@@ -255,31 +276,31 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
255276

256277
// Only check non-glue functions
257278
if let ty::InstanceDef::Item(def) = instance.def {
279+
let mut is_const_fn = true;
280+
258281
// Execution might have wandered off into other crates, so we cannot do a stability-
259282
// sensitive check here. But we can at least rule out functions that are not const
260283
// at all.
261284
if !ecx.tcx.is_const_fn_raw(def.did) {
262285
// allow calling functions marked with #[default_method_body_is_const].
263286
if !ecx.tcx.has_attr(def.did, sym::default_method_body_is_const) {
264-
// Some functions we support even if they are non-const -- but avoid testing
265-
// that for const fn!
266-
if let Some(new_instance) = ecx.hook_panic_fn(instance, args)? {
267-
// We call another const fn instead.
268-
return Self::find_mir_or_eval_fn(
269-
ecx,
270-
new_instance,
271-
_abi,
272-
args,
273-
_ret,
274-
_unwind,
275-
);
276-
} else {
277-
// We certainly do *not* want to actually call the fn
278-
// though, so be sure we return here.
279-
throw_unsup_format!("calling non-const function `{}`", instance)
280-
}
287+
is_const_fn = false;
281288
}
282289
}
290+
291+
// Some functions we support even if they are non-const -- but avoid testing
292+
// that for const fn!
293+
// `const_eval_select` is a const fn because it must use const trait bounds.
294+
if let Some(new_instance) = ecx.hook_special_const_fn(instance, args, is_const_fn)? {
295+
// We call another const fn instead.
296+
return Self::find_mir_or_eval_fn(ecx, new_instance, _abi, args, _ret, _unwind);
297+
}
298+
299+
if !is_const_fn {
300+
// We certainly do *not* want to actually call the fn
301+
// though, so be sure we return here.
302+
throw_unsup_format!("calling non-const function `{}`", instance)
303+
}
283304
}
284305
// This is a const fn. Call it.
285306
Ok(Some(ecx.load_mir(instance.def, None)?))

compiler/rustc_const_eval/src/interpret/terminator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
231231
}
232232

233233
/// Call this function -- pushing the stack frame and initializing the arguments.
234-
fn eval_fn_call(
234+
pub(crate) fn eval_fn_call(
235235
&mut self,
236236
fn_val: FnVal<'tcx, M::ExtraFnVal>,
237237
caller_abi: Abi,

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::ops::Deref;
2424
use super::ops::{self, NonConstOp, Status};
2525
use super::qualifs::{self, CustomEq, HasMutInterior, NeedsNonConstDrop};
2626
use super::resolver::FlowSensitiveAnalysis;
27-
use super::{is_lang_panic_fn, ConstCx, Qualif};
27+
use super::{is_lang_panic_fn, is_lang_special_const_fn, ConstCx, Qualif};
2828
use crate::const_eval::is_unstable_const_fn;
2929

3030
// We are using `MaybeMutBorrowedLocals` as a proxy for whether an item may have been mutated
@@ -259,7 +259,9 @@ impl Checker<'mir, 'tcx> {
259259
self.check_local_or_return_ty(return_ty.skip_binder(), RETURN_PLACE);
260260
}
261261

262-
self.visit_body(&body);
262+
if !tcx.has_attr(def_id.to_def_id(), sym::rustc_do_not_const_check) {
263+
self.visit_body(&body);
264+
}
263265

264266
// Ensure that the end result is `Sync` in a non-thread local `static`.
265267
let should_check_for_sync = self.const_kind()
@@ -886,7 +888,7 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
886888
}
887889

888890
// At this point, we are calling a function, `callee`, whose `DefId` is known...
889-
if is_lang_panic_fn(tcx, callee) {
891+
if is_lang_special_const_fn(tcx, callee) {
890892
// `begin_panic` and `panic_display` are generic functions that accept
891893
// types other than str. Check to enforce that only str can be used in
892894
// const-eval.
@@ -908,7 +910,10 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
908910
}
909911
}
910912

911-
return;
913+
if is_lang_panic_fn(tcx, callee) {
914+
// run stability check on non-panic special const fns.
915+
return;
916+
}
912917
}
913918

914919
if Some(callee) == tcx.lang_items().exchange_malloc_fn() {

compiler/rustc_const_eval/src/transform/check_consts/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ impl ConstCx<'mir, 'tcx> {
7474

7575
/// Returns `true` if this `DefId` points to one of the official `panic` lang items.
7676
pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
77-
// We can allow calls to these functions because `hook_panic_fn` in
78-
// `const_eval/machine.rs` ensures the calls are handled specially.
79-
// Keep in sync with what that function handles!
8077
Some(def_id) == tcx.lang_items().panic_fn()
8178
|| Some(def_id) == tcx.lang_items().panic_str()
8279
|| Some(def_id) == tcx.lang_items().panic_display()
@@ -85,6 +82,15 @@ pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
8582
|| Some(def_id) == tcx.lang_items().begin_panic_fmt()
8683
}
8784

85+
/// Returns `true` if this `DefId` points to one of the lang items that will be handled differently
86+
/// in const_eval.
87+
pub fn is_lang_special_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
88+
// We can allow calls to these functions because `hook_special_const_fn` in
89+
// `const_eval/machine.rs` ensures the calls are handled specially.
90+
// Keep in sync with what that function handles!
91+
is_lang_panic_fn(tcx, def_id) || Some(def_id) == tcx.lang_items().const_eval_select()
92+
}
93+
8894
pub fn rustc_allow_const_fn_unstable(
8995
tcx: TyCtxt<'tcx>,
9096
def_id: DefId,

compiler/rustc_const_eval/src/transform/check_consts/post_drop_elaboration.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_middle::mir::visit::Visitor;
22
use rustc_middle::mir::{self, BasicBlock, Location};
33
use rustc_middle::ty::TyCtxt;
4-
use rustc_span::Span;
4+
use rustc_span::{symbol::sym, Span};
55

66
use super::check::Qualifs;
77
use super::ops::{self, NonConstOp};
@@ -30,6 +30,10 @@ pub fn check_live_drops(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
3030
return;
3131
}
3232

33+
if tcx.has_attr(def_id.to_def_id(), sym::rustc_do_not_const_check) {
34+
return;
35+
}
36+
3337
let ccx = ConstCx { body, tcx, const_kind, param_env: tcx.param_env(def_id) };
3438
if !checking_enabled(&ccx) {
3539
return;

compiler/rustc_const_eval/src/transform/promote_consts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_index::vec::{Idx, IndexVec};
2626
use std::cell::Cell;
2727
use std::{cmp, iter, mem};
2828

29-
use crate::transform::check_consts::{is_lang_panic_fn, qualifs, ConstCx};
29+
use crate::transform::check_consts::{is_lang_special_const_fn, qualifs, ConstCx};
3030
use crate::transform::MirPass;
3131

3232
/// A `MirPass` for promotion.
@@ -657,7 +657,7 @@ impl<'tcx> Validator<'_, 'tcx> {
657657

658658
let is_const_fn = match *fn_ty.kind() {
659659
ty::FnDef(def_id, _) => {
660-
self.tcx.is_const_fn_raw(def_id) || is_lang_panic_fn(self.tcx, def_id)
660+
self.tcx.is_const_fn_raw(def_id) || is_lang_special_const_fn(self.tcx, def_id)
661661
}
662662
_ => false,
663663
};

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
467467

468468
rustc_attr!(rustc_promotable, Normal, template!(Word), IMPL_DETAIL),
469469
rustc_attr!(rustc_legacy_const_generics, Normal, template!(List: "N"), INTERNAL_UNSTABLE),
470+
// Do not const-check this function's body. It will always get replaced during CTFE.
471+
rustc_attr!(rustc_do_not_const_check, Normal, template!(Word), INTERNAL_UNSTABLE),
470472

471473
// ==========================================================================
472474
// Internal attributes, Layout related:

compiler/rustc_hir/src/lang_items.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ language_item_table! {
299299
DropInPlace, sym::drop_in_place, drop_in_place_fn, Target::Fn, GenericRequirement::Minimum(1);
300300
Oom, sym::oom, oom, Target::Fn, GenericRequirement::None;
301301
AllocLayout, sym::alloc_layout, alloc_layout, Target::Struct, GenericRequirement::None;
302+
ConstEvalSelect, sym::const_eval_select, const_eval_select, Target::Fn, GenericRequirement::Exact(4);
303+
ConstConstEvalSelect, sym::const_eval_select_ct,const_eval_select_ct, Target::Fn, GenericRequirement::Exact(4);
302304

303305
Start, sym::start, start_fn, Target::Fn, GenericRequirement::Exact(1);
304306

0 commit comments

Comments
 (0)