Add const_eval_select intrinsic
This commit is contained in:
parent
0c87288f92
commit
5387b6542f
20 changed files with 334 additions and 38 deletions
|
@ -26,14 +26,35 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
|
|||
/// "Intercept" a function call to a panic-related function
|
||||
/// because we have something special to do for it.
|
||||
/// If this returns successfully (`Ok`), the function should just be evaluated normally.
|
||||
fn hook_panic_fn(
|
||||
fn hook_special_const_fn(
|
||||
&mut self,
|
||||
instance: ty::Instance<'tcx>,
|
||||
args: &[OpTy<'tcx>],
|
||||
is_const_fn: bool,
|
||||
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
|
||||
// The list of functions we handle here must be in sync with
|
||||
// `is_lang_panic_fn` in `transform/check_consts/mod.rs`.
|
||||
// `is_lang_special_const_fn` in `transform/check_consts/mod.rs`.
|
||||
let def_id = instance.def_id();
|
||||
|
||||
if is_const_fn {
|
||||
if Some(def_id) == self.tcx.lang_items().const_eval_select() {
|
||||
// redirect to const_eval_select_ct
|
||||
if let Some(const_eval_select) = self.tcx.lang_items().const_eval_select_ct() {
|
||||
return Ok(Some(
|
||||
ty::Instance::resolve(
|
||||
*self.tcx,
|
||||
ty::ParamEnv::reveal_all(),
|
||||
const_eval_select,
|
||||
instance.substs,
|
||||
)
|
||||
.unwrap()
|
||||
.unwrap(),
|
||||
));
|
||||
}
|
||||
}
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
if Some(def_id) == self.tcx.lang_items().panic_fn()
|
||||
|| Some(def_id) == self.tcx.lang_items().panic_str()
|
||||
|| Some(def_id) == self.tcx.lang_items().panic_display()
|
||||
|
@ -255,31 +276,31 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
|
|||
|
||||
// Only check non-glue functions
|
||||
if let ty::InstanceDef::Item(def) = instance.def {
|
||||
let mut is_const_fn = true;
|
||||
|
||||
// Execution might have wandered off into other crates, so we cannot do a stability-
|
||||
// sensitive check here. But we can at least rule out functions that are not const
|
||||
// at all.
|
||||
if !ecx.tcx.is_const_fn_raw(def.did) {
|
||||
// allow calling functions marked with #[default_method_body_is_const].
|
||||
if !ecx.tcx.has_attr(def.did, sym::default_method_body_is_const) {
|
||||
// Some functions we support even if they are non-const -- but avoid testing
|
||||
// that for const fn!
|
||||
if let Some(new_instance) = ecx.hook_panic_fn(instance, args)? {
|
||||
// We call another const fn instead.
|
||||
return Self::find_mir_or_eval_fn(
|
||||
ecx,
|
||||
new_instance,
|
||||
_abi,
|
||||
args,
|
||||
_ret,
|
||||
_unwind,
|
||||
);
|
||||
} else {
|
||||
// We certainly do *not* want to actually call the fn
|
||||
// though, so be sure we return here.
|
||||
throw_unsup_format!("calling non-const function `{}`", instance)
|
||||
}
|
||||
is_const_fn = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Some functions we support even if they are non-const -- but avoid testing
|
||||
// that for const fn!
|
||||
// `const_eval_select` is a const fn because it must use const trait bounds.
|
||||
if let Some(new_instance) = ecx.hook_special_const_fn(instance, args, is_const_fn)? {
|
||||
// We call another const fn instead.
|
||||
return Self::find_mir_or_eval_fn(ecx, new_instance, _abi, args, _ret, _unwind);
|
||||
}
|
||||
|
||||
if !is_const_fn {
|
||||
// We certainly do *not* want to actually call the fn
|
||||
// though, so be sure we return here.
|
||||
throw_unsup_format!("calling non-const function `{}`", instance)
|
||||
}
|
||||
}
|
||||
// This is a const fn. Call it.
|
||||
Ok(Some(ecx.load_mir(instance.def, None)?))
|
||||
|
|
|
@ -231,7 +231,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||
}
|
||||
|
||||
/// Call this function -- pushing the stack frame and initializing the arguments.
|
||||
fn eval_fn_call(
|
||||
pub(crate) fn eval_fn_call(
|
||||
&mut self,
|
||||
fn_val: FnVal<'tcx, M::ExtraFnVal>,
|
||||
caller_abi: Abi,
|
||||
|
|
|
@ -24,7 +24,7 @@ use std::ops::Deref;
|
|||
use super::ops::{self, NonConstOp, Status};
|
||||
use super::qualifs::{self, CustomEq, HasMutInterior, NeedsNonConstDrop};
|
||||
use super::resolver::FlowSensitiveAnalysis;
|
||||
use super::{is_lang_panic_fn, ConstCx, Qualif};
|
||||
use super::{is_lang_special_const_fn, ConstCx, Qualif};
|
||||
use crate::const_eval::is_unstable_const_fn;
|
||||
|
||||
// We are using `MaybeMutBorrowedLocals` as a proxy for whether an item may have been mutated
|
||||
|
@ -259,7 +259,9 @@ impl Checker<'mir, 'tcx> {
|
|||
self.check_local_or_return_ty(return_ty.skip_binder(), RETURN_PLACE);
|
||||
}
|
||||
|
||||
self.visit_body(&body);
|
||||
if !tcx.has_attr(def_id.to_def_id(), sym::rustc_do_not_const_check) {
|
||||
self.visit_body(&body);
|
||||
}
|
||||
|
||||
// Ensure that the end result is `Sync` in a non-thread local `static`.
|
||||
let should_check_for_sync = self.const_kind()
|
||||
|
@ -886,7 +888,7 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
|
|||
}
|
||||
|
||||
// At this point, we are calling a function, `callee`, whose `DefId` is known...
|
||||
if is_lang_panic_fn(tcx, callee) {
|
||||
if is_lang_special_const_fn(tcx, callee) {
|
||||
// `begin_panic` and `panic_display` are generic functions that accept
|
||||
// types other than str. Check to enforce that only str can be used in
|
||||
// const-eval.
|
||||
|
|
|
@ -74,9 +74,6 @@ impl ConstCx<'mir, 'tcx> {
|
|||
|
||||
/// Returns `true` if this `DefId` points to one of the official `panic` lang items.
|
||||
pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
|
||||
// We can allow calls to these functions because `hook_panic_fn` in
|
||||
// `const_eval/machine.rs` ensures the calls are handled specially.
|
||||
// Keep in sync with what that function handles!
|
||||
Some(def_id) == tcx.lang_items().panic_fn()
|
||||
|| Some(def_id) == tcx.lang_items().panic_str()
|
||||
|| 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 {
|
|||
|| Some(def_id) == tcx.lang_items().begin_panic_fmt()
|
||||
}
|
||||
|
||||
/// Returns `true` if this `DefId` points to one of the lang items that will be handled differently
|
||||
/// in const_eval.
|
||||
pub fn is_lang_special_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
|
||||
// We can allow calls to these functions because `hook_special_const_fn` in
|
||||
// `const_eval/machine.rs` ensures the calls are handled specially.
|
||||
// Keep in sync with what that function handles!
|
||||
is_lang_panic_fn(tcx, def_id) || Some(def_id) == tcx.lang_items().const_eval_select()
|
||||
}
|
||||
|
||||
pub fn rustc_allow_const_fn_unstable(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
def_id: DefId,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use rustc_middle::mir::visit::Visitor;
|
||||
use rustc_middle::mir::{self, BasicBlock, Location};
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_span::Span;
|
||||
use rustc_span::{symbol::sym, Span};
|
||||
|
||||
use super::check::Qualifs;
|
||||
use super::ops::{self, NonConstOp};
|
||||
|
@ -30,6 +30,10 @@ pub fn check_live_drops(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
|
|||
return;
|
||||
}
|
||||
|
||||
if tcx.has_attr(def_id.to_def_id(), sym::rustc_do_not_const_check) {
|
||||
return;
|
||||
}
|
||||
|
||||
let ccx = ConstCx { body, tcx, const_kind, param_env: tcx.param_env(def_id) };
|
||||
if !checking_enabled(&ccx) {
|
||||
return;
|
||||
|
|
|
@ -26,7 +26,7 @@ use rustc_index::vec::{Idx, IndexVec};
|
|||
use std::cell::Cell;
|
||||
use std::{cmp, iter, mem};
|
||||
|
||||
use crate::transform::check_consts::{is_lang_panic_fn, qualifs, ConstCx};
|
||||
use crate::transform::check_consts::{is_lang_special_const_fn, qualifs, ConstCx};
|
||||
use crate::transform::MirPass;
|
||||
|
||||
/// A `MirPass` for promotion.
|
||||
|
@ -657,7 +657,7 @@ impl<'tcx> Validator<'_, 'tcx> {
|
|||
|
||||
let is_const_fn = match *fn_ty.kind() {
|
||||
ty::FnDef(def_id, _) => {
|
||||
self.tcx.is_const_fn_raw(def_id) || is_lang_panic_fn(self.tcx, def_id)
|
||||
self.tcx.is_const_fn_raw(def_id) || is_lang_special_const_fn(self.tcx, def_id)
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue