Skip to content

Commit 11fb22d

Browse files
committed
CTFE eval_fn_call: use FnAbi to determine argument skipping and compatibility
1 parent 84f962a commit 11fb22d

File tree

6 files changed

+198
-111
lines changed

6 files changed

+198
-111
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
260260
args: &[OpTy<'tcx>],
261261
_ret: Option<(&PlaceTy<'tcx>, mir::BasicBlock)>,
262262
_unwind: StackPopUnwind, // unwinding is not supported in consts
263-
) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
263+
) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
264264
debug!("find_mir_or_eval_fn: {:?}", instance);
265265

266266
// Only check non-glue functions
@@ -283,7 +283,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
283283
}
284284
}
285285
// This is a const fn. Call it.
286-
Ok(Some(ecx.load_mir(instance.def, None)?))
286+
Ok(Some((ecx.load_mir(instance.def, None)?, instance)))
287287
}
288288

289289
fn call_intrinsic(

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ use rustc_index::vec::IndexVec;
88
use rustc_macros::HashStable;
99
use rustc_middle::mir;
1010
use rustc_middle::mir::interpret::{InterpError, InvalidProgramInfo};
11-
use rustc_middle::ty::layout::{self, LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
11+
use rustc_middle::ty::layout::{
12+
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOf, LayoutOfHelpers,
13+
TyAndLayout,
14+
};
1215
use rustc_middle::ty::{
1316
self, query::TyCtxtAt, subst::SubstsRef, ParamEnv, Ty, TyCtxt, TypeFoldable,
1417
};
1518
use rustc_mir_dataflow::storage::AlwaysLiveLocals;
1619
use rustc_query_system::ich::StableHashingContext;
1720
use rustc_session::Limit;
1821
use rustc_span::{Pos, Span};
19-
use rustc_target::abi::{Align, HasDataLayout, Size, TargetDataLayout};
22+
use rustc_target::abi::{call::FnAbi, Align, HasDataLayout, Size, TargetDataLayout};
2023

2124
use super::{
2225
AllocId, GlobalId, Immediate, InterpErrorInfo, InterpResult, MPlaceTy, Machine, MemPlace,
@@ -333,6 +336,22 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> LayoutOfHelpers<'tcx> for InterpC
333336
}
334337
}
335338

339+
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> FnAbiOfHelpers<'tcx> for InterpCx<'mir, 'tcx, M> {
340+
type FnAbiOfResult = InterpResult<'tcx, &'tcx FnAbi<'tcx, Ty<'tcx>>>;
341+
342+
fn handle_fn_abi_err(
343+
&self,
344+
err: FnAbiError<'tcx>,
345+
_span: Span,
346+
_fn_abi_request: FnAbiRequest<'tcx>,
347+
) -> InterpErrorInfo<'tcx> {
348+
match err {
349+
FnAbiError::Layout(err) => err_inval!(Layout(err)).into(),
350+
FnAbiError::AdjustForForeignAbi(err) => err_inval!(FnAbi(err)).into(),
351+
}
352+
}
353+
}
354+
336355
/// Test if it is valid for a MIR assignment to assign `src`-typed place to `dest`-typed value.
337356
/// This test should be symmetric, as it is primarily about layout compatibility.
338357
pub(super) fn mir_assign_valid_types<'tcx>(

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
167167
args: &[OpTy<'tcx, Self::PointerTag>],
168168
ret: Option<(&PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,
169169
unwind: StackPopUnwind,
170-
) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>>;
170+
) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>>;
171171

172172
/// Execute `fn_val`. It is the hook's responsibility to advance the instruction
173173
/// pointer as appropriate.

0 commit comments

Comments
 (0)