Skip to content

Commit c34ac87

Browse files
committed
Auto merge of #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

+2-2
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

+1-3
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

+40-19
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

+1-1
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

+9-4
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

+9-3
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

+5-1
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

+2-2
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

+2
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

+2
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

compiler/rustc_span/src/symbol.rs

+3
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ symbols! {
441441
const_compare_raw_pointers,
442442
const_constructor,
443443
const_eval_limit,
444+
const_eval_select,
445+
const_eval_select_ct,
444446
const_evaluatable_checked,
445447
const_extern_fn,
446448
const_fn,
@@ -1097,6 +1099,7 @@ symbols! {
10971099
rustc_diagnostic_item,
10981100
rustc_diagnostic_macros,
10991101
rustc_dirty,
1102+
rustc_do_not_const_check,
11001103
rustc_dummy,
11011104
rustc_dump_env_program_clauses,
11021105
rustc_dump_program_clauses,

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -973,12 +973,16 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
973973
ty::Tuple(_) => stack.extend(ty.tuple_fields().map(|t| (t, depth + 1))),
974974

975975
ty::Closure(_, substs) => {
976-
stack.extend(substs.as_closure().upvar_tys().map(|t| (t, depth + 1)))
976+
let substs = substs.as_closure();
977+
let ty = self.infcx.shallow_resolve(substs.tupled_upvars_ty());
978+
stack.push((ty, depth + 1));
977979
}
978980

979981
ty::Generator(_, substs, _) => {
980982
let substs = substs.as_generator();
981-
stack.extend(substs.upvar_tys().map(|t| (t, depth + 1)));
983+
let ty = self.infcx.shallow_resolve(substs.tupled_upvars_ty());
984+
985+
stack.push((ty, depth + 1));
982986
stack.push((substs.witness(), depth + 1));
983987
}
984988

compiler/rustc_typeck/src/check/callee.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_infer::{
1717
use rustc_middle::ty::adjustment::{
1818
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability,
1919
};
20-
use rustc_middle::ty::subst::SubstsRef;
20+
use rustc_middle::ty::subst::{Subst, SubstsRef};
2121
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
2222
use rustc_span::symbol::{sym, Ident};
2323
use rustc_span::Span;
@@ -317,6 +317,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
317317
) -> Ty<'tcx> {
318318
let (fn_sig, def_id) = match *callee_ty.kind() {
319319
ty::FnDef(def_id, subst) => {
320+
let fn_sig = self.tcx.fn_sig(def_id).subst(self.tcx, subst);
321+
320322
// Unit testing: function items annotated with
321323
// `#[rustc_evaluate_where_clauses]` trigger special output
322324
// to let us test the trait evaluation system.
@@ -342,7 +344,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
342344
.emit();
343345
}
344346
}
345-
(callee_ty.fn_sig(self.tcx), Some(def_id))
347+
(fn_sig, Some(def_id))
346348
}
347349
ty::FnPtr(sig) => (sig, None),
348350
ref t => {

compiler/rustc_typeck/src/check/intrinsic.rs

+2
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
390390

391391
sym::black_box => (1, vec![param(0)], param(0)),
392392

393+
sym::const_eval_select => (4, vec![param(0), param(1), param(2)], param(3)),
394+
393395
other => {
394396
tcx.sess.emit_err(UnrecognizedIntrinsicFunction { span: it.span, name: other });
395397
return;

library/core/src/intrinsics.rs

+71
Original file line numberDiff line numberDiff line change
@@ -2221,3 +2221,74 @@ pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
22212221
// SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
22222222
unsafe { write_bytes(dst, val, count) }
22232223
}
2224+
2225+
/// Selects which function to call depending on the context.
2226+
///
2227+
/// If this function is evaluated at compile-time, then a call to this
2228+
/// intrinsic will be replaced with a call to `called_in_const`. It gets
2229+
/// replaced with a call to `called_at_rt` otherwise.
2230+
///
2231+
/// # Type Requirements
2232+
///
2233+
/// The two functions must be both function items. They cannot be function
2234+
/// pointers or closures.
2235+
///
2236+
/// `arg` will be the arguments that will be passed to either one of the
2237+
/// two functions, therefore, both functions must accept the same type of
2238+
/// arguments. Both functions must return RET.
2239+
///
2240+
/// # Safety
2241+
///
2242+
/// This intrinsic allows breaking [referential transparency] in `const fn`
2243+
/// and is therefore `unsafe`.
2244+
///
2245+
/// Code that uses this intrinsic must be extremely careful to ensure that
2246+
/// `const fn`s remain referentially-transparent independently of when they
2247+
/// are evaluated.
2248+
///
2249+
/// The Rust compiler assumes that it is sound to replace a call to a `const
2250+
/// fn` with the result produced by evaluating it at compile-time. If
2251+
/// evaluating the function at run-time were to produce a different result,
2252+
/// or have any other observable side-effects, the behavior is undefined.
2253+
///
2254+
/// [referential transparency]: https://en.wikipedia.org/wiki/Referential_transparency
2255+
#[cfg(not(bootstrap))]
2256+
#[unstable(
2257+
feature = "const_eval_select",
2258+
issue = "none",
2259+
reason = "const_eval_select will never be stable"
2260+
)]
2261+
#[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2262+
#[lang = "const_eval_select"]
2263+
#[rustc_do_not_const_check]
2264+
pub const unsafe fn const_eval_select<ARG, F, G, RET>(
2265+
arg: ARG,
2266+
_called_in_const: F,
2267+
called_at_rt: G,
2268+
) -> RET
2269+
where
2270+
F: ~const FnOnce<ARG, Output = RET>,
2271+
G: FnOnce<ARG, Output = RET> + ~const Drop,
2272+
{
2273+
called_at_rt.call_once(arg)
2274+
}
2275+
2276+
#[cfg(not(bootstrap))]
2277+
#[unstable(
2278+
feature = "const_eval_select",
2279+
issue = "none",
2280+
reason = "const_eval_select will never be stable"
2281+
)]
2282+
#[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2283+
#[lang = "const_eval_select_ct"]
2284+
pub const unsafe fn const_eval_select_ct<ARG, F, G, RET>(
2285+
arg: ARG,
2286+
called_in_const: F,
2287+
_called_at_rt: G,
2288+
) -> RET
2289+
where
2290+
F: ~const FnOnce<ARG, Output = RET>,
2291+
G: FnOnce<ARG, Output = RET> + ~const Drop,
2292+
{
2293+
called_in_const.call_once(arg)
2294+
}

0 commit comments

Comments
 (0)