Skip to content

[WIP] Add call_if_rt #89291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions compiler/rustc_const_eval/src/const_eval/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ use crate::interpret::{
use super::error::*;

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.
/// "Intercept" a function call to functions that require special handling.
/// If this returns successfully (`Ok`), the function should just be evaluated normally.
fn hook_panic_fn(
fn hook_fn(
&mut self,
instance: ty::Instance<'tcx>,
args: &[OpTy<'tcx>],
Expand Down Expand Up @@ -67,6 +66,19 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
.unwrap(),
));
}
} else if Some(def_id) == self.tcx.lang_items().call_if_rt() {
if let Some(const_call_if_rt) = self.tcx.lang_items().const_call_if_rt() {
return Ok(Some(
ty::Instance::resolve(
*self.tcx,
ty::ParamEnv::reveal_all(),
const_call_if_rt,
instance.substs,
)
.unwrap()
.unwrap(),
));
}
}
Ok(None)
}
Expand Down Expand Up @@ -263,7 +275,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
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)? {
if let Some(new_instance) = ecx.hook_fn(instance, args)? {
// We call another const fn instead.
return Self::find_mir_or_eval_fn(
ecx,
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_const_eval/src/transform/check_consts/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,10 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
return;
}

if Some(callee) == tcx.lang_items().call_if_rt() {
return;
}

if Some(callee) == tcx.lang_items().exchange_malloc_fn() {
self.check_op(ops::HeapAllocation);
return;
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ language_item_table! {
BeginPanic, sym::begin_panic, begin_panic_fn, Target::Fn, GenericRequirement::None;
BeginPanicFmt, sym::begin_panic_fmt, begin_panic_fmt, Target::Fn, GenericRequirement::None;

CallIfRt, sym::call_if_rt, call_if_rt, Target::Fn, GenericRequirement::None;
ConstCallIfRt, sym::const_call_if_rt, const_call_if_rt, Target::Fn, GenericRequirement::None;

ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn, GenericRequirement::None;
BoxFree, sym::box_free, box_free_fn, Target::Fn, GenericRequirement::Minimum(1);
DropInPlace, sym::drop_in_place, drop_in_place_fn, Target::Fn, GenericRequirement::Minimum(1);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ symbols! {
c_unwind,
c_variadic,
call,
call_if_rt,
call_mut,
call_once,
caller_location,
Expand Down Expand Up @@ -429,6 +430,7 @@ symbols! {
console,
const_allocate,
const_async_blocks,
const_call_if_rt,
const_compare_raw_pointers,
const_constructor,
const_eval_limit,
Expand Down
15 changes: 15 additions & 0 deletions library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2221,3 +2221,18 @@ pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
// SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
unsafe { write_bytes(dst, val, count) }
}

#[cfg(not(bootstrap))]
#[unstable(feature = "core_intrinsics", issue = "none")]
#[lang = "call_if_rt"]
#[inline(always)]
pub fn call_if_rt<R, F: FnOnce() -> R + Copy>(f: F) -> Option<R> {
Some(f())
}

#[cfg(not(bootstrap))]
#[unstable(feature = "core_intrinsics", issue = "none")]
#[lang = "const_call_if_rt"]
pub const fn const_call_if_rt<R, F: FnOnce() -> R + Copy>(_f: F) -> Option<R> {
None
}