2022-11-21 16:51:16 +00:00
|
|
|
use crate::const_eval::CheckAlignment;
|
2023-05-17 10:30:14 +00:00
|
|
|
use crate::errors::ConstEvalError;
|
2022-11-18 10:18:32 +01:00
|
|
|
|
|
|
|
|
use either::{Left, Right};
|
2020-08-09 15:37:32 +02:00
|
|
|
|
2020-12-11 18:19:30 +03:00
|
|
|
use rustc_hir::def::DefKind;
|
2023-05-17 10:30:14 +00:00
|
|
|
use rustc_middle::mir::interpret::{ErrorHandled, InterpErrorInfo};
|
|
|
|
|
use rustc_middle::mir::pretty::write_allocation_bytes;
|
2023-09-16 09:36:22 +02:00
|
|
|
use rustc_middle::mir::{self, ConstAlloc, ConstValue};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::traits::Reveal;
|
2021-08-30 17:38:27 +03:00
|
|
|
use rustc_middle::ty::layout::LayoutOf;
|
2020-09-02 10:40:56 +03:00
|
|
|
use rustc_middle::ty::print::with_no_trimmed_paths;
|
2022-09-16 15:31:10 +02:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2020-01-01 19:25:28 +01:00
|
|
|
use rustc_span::source_map::Span;
|
2022-03-03 12:02:52 +00:00
|
|
|
use rustc_target::abi::{self, Abi};
|
2022-11-18 10:18:32 +01:00
|
|
|
|
2023-06-24 20:40:40 +00:00
|
|
|
use super::{CanAccessStatics, CompileTimeEvalContext, CompileTimeInterpreter};
|
2023-05-17 10:30:14 +00:00
|
|
|
use crate::errors;
|
2022-11-18 10:18:32 +01:00
|
|
|
use crate::interpret::eval_nullary_intrinsic;
|
|
|
|
|
use crate::interpret::{
|
2023-09-16 09:36:22 +02:00
|
|
|
intern_const_alloc_recursive, CtfeValidationMode, GlobalId, Immediate, InternKind, InterpCx,
|
|
|
|
|
InterpError, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking, StackPopCleanup,
|
2022-11-18 10:18:32 +01:00
|
|
|
};
|
2019-12-25 01:06:51 +01:00
|
|
|
|
2019-12-22 22:20:46 +01:00
|
|
|
// Returns a pointer to where the result lives
|
|
|
|
|
fn eval_body_using_ecx<'mir, 'tcx>(
|
|
|
|
|
ecx: &mut CompileTimeEvalContext<'mir, 'tcx>,
|
|
|
|
|
cid: GlobalId<'tcx>,
|
|
|
|
|
body: &'mir mir::Body<'tcx>,
|
|
|
|
|
) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
|
|
|
|
|
debug!("eval_body_using_ecx: {:?}, {:?}", cid, ecx.param_env);
|
2020-12-11 01:59:05 +03:00
|
|
|
let tcx = *ecx.tcx;
|
2020-12-09 14:53:35 +03:00
|
|
|
assert!(
|
|
|
|
|
cid.promoted.is_some()
|
|
|
|
|
|| matches!(
|
2020-12-11 18:19:30 +03:00
|
|
|
ecx.tcx.def_kind(cid.instance.def_id()),
|
|
|
|
|
DefKind::Const
|
2022-03-29 17:11:12 +02:00
|
|
|
| DefKind::Static(_)
|
2020-12-11 18:19:30 +03:00
|
|
|
| DefKind::ConstParam
|
|
|
|
|
| DefKind::AnonConst
|
2021-10-02 12:59:26 +01:00
|
|
|
| DefKind::InlineConst
|
2020-12-11 18:19:30 +03:00
|
|
|
| DefKind::AssocConst
|
|
|
|
|
),
|
|
|
|
|
"Unexpected DefKind: {:?}",
|
|
|
|
|
ecx.tcx.def_kind(cid.instance.def_id())
|
2020-12-09 14:53:35 +03:00
|
|
|
);
|
2023-07-11 22:35:29 +01:00
|
|
|
let layout = ecx.layout_of(body.bound_return_ty().instantiate(tcx, cid.instance.args))?;
|
2022-11-13 12:14:59 +01:00
|
|
|
assert!(layout.is_sized());
|
2021-06-12 19:49:48 -04:00
|
|
|
let ret = ecx.allocate(layout, MemoryKind::Stack)?;
|
2019-12-22 22:20:46 +01:00
|
|
|
|
2021-09-14 00:00:00 +00:00
|
|
|
trace!(
|
|
|
|
|
"eval_body_using_ecx: pushing stack frame for global: {}{}",
|
2023-02-09 10:51:29 +11:00
|
|
|
with_no_trimmed_paths!(ecx.tcx.def_path_str(cid.instance.def_id())),
|
2023-07-25 23:17:39 +02:00
|
|
|
cid.promoted.map_or_else(String::new, |p| format!("::promoted[{p:?}]"))
|
2021-09-14 00:00:00 +00:00
|
|
|
);
|
2019-12-22 22:20:46 +01:00
|
|
|
|
|
|
|
|
ecx.push_stack_frame(
|
|
|
|
|
cid.instance,
|
|
|
|
|
body,
|
2023-07-25 22:35:07 +02:00
|
|
|
&ret.clone().into(),
|
2022-01-03 23:07:07 +01:00
|
|
|
StackPopCleanup::Root { cleanup: false },
|
2019-12-22 22:20:46 +01:00
|
|
|
)?;
|
2023-08-06 18:40:37 +02:00
|
|
|
ecx.storage_live_for_always_live_locals()?;
|
2019-12-22 22:20:46 +01:00
|
|
|
|
|
|
|
|
// The main interpreter loop.
|
2022-11-27 15:20:26 +01:00
|
|
|
while ecx.step()? {}
|
2019-12-22 22:20:46 +01:00
|
|
|
|
|
|
|
|
// Intern the result
|
2020-10-24 17:23:45 +02:00
|
|
|
let intern_kind = if cid.promoted.is_some() {
|
|
|
|
|
InternKind::Promoted
|
|
|
|
|
} else {
|
|
|
|
|
match tcx.static_mutability(cid.instance.def_id()) {
|
|
|
|
|
Some(m) => InternKind::Static(m),
|
|
|
|
|
None => InternKind::Constant,
|
|
|
|
|
}
|
2019-12-25 13:58:02 +01:00
|
|
|
};
|
2022-11-21 16:51:16 +00:00
|
|
|
ecx.machine.check_alignment = CheckAlignment::No; // interning doesn't need to respect alignment
|
2021-02-15 00:00:00 +00:00
|
|
|
intern_const_alloc_recursive(ecx, intern_kind, &ret)?;
|
2022-08-26 08:08:10 -04:00
|
|
|
// we leave alignment checks off, since this `ecx` will not be used for further evaluation anyway
|
2019-12-22 22:20:46 +01:00
|
|
|
|
2023-09-04 17:53:38 +02:00
|
|
|
debug!("eval_body_using_ecx done: {:?}", ret);
|
2019-12-22 22:20:46 +01:00
|
|
|
Ok(ret)
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-20 16:00:39 -08:00
|
|
|
/// The `InterpCx` is only meant to be used to do field and index projections into constants for
|
2022-08-21 20:00:38 -04:00
|
|
|
/// `simd_shuffle` and const patterns in match arms. It never performs alignment checks.
|
2019-12-22 22:20:46 +01:00
|
|
|
///
|
|
|
|
|
/// The function containing the `match` that is currently being analyzed may have generic bounds
|
|
|
|
|
/// that inform us about the generic bounds of the constant. E.g., using an associated constant
|
|
|
|
|
/// of a function's generic parameter will require knowledge about the bounds on the generic
|
|
|
|
|
/// parameter. These bounds are passed to `mk_eval_cx` via the `ParamEnv` argument.
|
|
|
|
|
pub(super) fn mk_eval_cx<'mir, 'tcx>(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-06-01 10:15:17 +02:00
|
|
|
root_span: Span,
|
2019-12-22 22:20:46 +01:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2023-06-24 20:40:40 +00:00
|
|
|
can_access_statics: CanAccessStatics,
|
2019-12-22 22:20:46 +01:00
|
|
|
) -> CompileTimeEvalContext<'mir, 'tcx> {
|
|
|
|
|
debug!("mk_eval_cx: {:?}", param_env);
|
|
|
|
|
InterpCx::new(
|
2020-06-01 10:15:17 +02:00
|
|
|
tcx,
|
|
|
|
|
root_span,
|
2019-12-22 22:20:46 +01:00
|
|
|
param_env,
|
2022-11-02 11:57:40 +00:00
|
|
|
CompileTimeInterpreter::new(can_access_statics, CheckAlignment::No),
|
2019-12-22 22:20:46 +01:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 15:57:40 +02:00
|
|
|
/// This function converts an interpreter value into a MIR constant.
|
2022-04-05 16:33:42 +02:00
|
|
|
#[instrument(skip(ecx), level = "debug")]
|
2019-12-22 22:20:46 +01:00
|
|
|
pub(super) fn op_to_const<'tcx>(
|
|
|
|
|
ecx: &CompileTimeEvalContext<'_, 'tcx>,
|
2021-02-15 00:00:00 +00:00
|
|
|
op: &OpTy<'tcx>,
|
2020-02-15 11:56:23 +13:00
|
|
|
) -> ConstValue<'tcx> {
|
2023-09-12 07:49:25 +02:00
|
|
|
// Handle ZST consistently and early.
|
|
|
|
|
if op.layout.is_zst() {
|
|
|
|
|
return ConstValue::ZeroSized;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 15:57:40 +02:00
|
|
|
// All scalar types should be stored as `ConstValue::Scalar`. This is needed to make
|
|
|
|
|
// `ConstValue::try_to_scalar` efficient; we want that to work for *all* constants of scalar
|
|
|
|
|
// type (it's used throughout the compiler and having it work just on literals is not enough)
|
|
|
|
|
// and we want it to be fast (i.e., don't go to an `Allocation` and reconstruct the `Scalar`
|
|
|
|
|
// from its byte-serialized form).
|
|
|
|
|
let force_as_immediate = match op.layout.abi {
|
2022-03-03 12:02:52 +00:00
|
|
|
Abi::Scalar(abi::Scalar::Initialized { .. }) => true,
|
2023-09-12 15:57:40 +02:00
|
|
|
// We don't *force* `ConstValue::Slice` for `ScalarPair`. This has the advantage that if the
|
|
|
|
|
// input `op` is a place, then turning it into a `ConstValue` and back into a `OpTy` will
|
|
|
|
|
// not have to generate any duplicate allocations (we preserve the original `AllocId` in
|
|
|
|
|
// `ConstValue::Indirect`). It means accessing the contents of a slice can be slow (since
|
|
|
|
|
// they can be stored as `ConstValue::Indirect`), but that's not relevant since we barely
|
|
|
|
|
// ever have to do this. (`try_get_slice_bytes_for_diagnostics` exists to provide this
|
|
|
|
|
// functionality.)
|
2019-12-22 22:20:46 +01:00
|
|
|
_ => false,
|
|
|
|
|
};
|
2023-09-12 15:57:40 +02:00
|
|
|
let immediate = if force_as_immediate {
|
2022-11-18 10:18:32 +01:00
|
|
|
Right(ecx.read_immediate(op).expect("normalization works on validated constants"))
|
2019-12-22 22:20:46 +01:00
|
|
|
} else {
|
2022-11-18 10:18:32 +01:00
|
|
|
op.as_mplace_or_imm()
|
2019-12-22 22:20:46 +01:00
|
|
|
};
|
2019-12-26 23:32:34 +01:00
|
|
|
|
2022-04-05 16:33:42 +02:00
|
|
|
debug!(?immediate);
|
|
|
|
|
|
2020-02-15 11:56:23 +13:00
|
|
|
match immediate {
|
2023-09-12 07:49:25 +02:00
|
|
|
Left(ref mplace) => {
|
|
|
|
|
// We know `offset` is relative to the allocation, so we can use `into_parts`.
|
|
|
|
|
let (alloc_id, offset) = mplace.ptr().into_parts();
|
|
|
|
|
let alloc_id = alloc_id.expect("cannot have `fake` place fot non-ZST type");
|
|
|
|
|
ConstValue::Indirect { alloc_id, offset }
|
|
|
|
|
}
|
2023-09-12 15:57:40 +02:00
|
|
|
// see comment on `let force_as_immediate` above
|
2022-11-18 10:18:32 +01:00
|
|
|
Right(imm) => match *imm {
|
2022-08-01 19:05:20 -04:00
|
|
|
Immediate::Scalar(x) => ConstValue::Scalar(x),
|
2020-04-13 17:07:54 +02:00
|
|
|
Immediate::ScalarPair(a, b) => {
|
2022-04-05 16:33:42 +02:00
|
|
|
debug!("ScalarPair(a: {:?}, b: {:?})", a, b);
|
2023-09-15 15:59:47 +02:00
|
|
|
// This codepath solely exists for `valtree_to_const_value` to not need to generate
|
|
|
|
|
// a `ConstValue::Indirect` for wide references, so it is tightly restricted to just
|
|
|
|
|
// that case.
|
|
|
|
|
let pointee_ty = imm.layout.ty.builtin_deref(false).unwrap().ty; // `false` = no raw ptrs
|
|
|
|
|
debug_assert!(
|
|
|
|
|
matches!(
|
|
|
|
|
ecx.tcx.struct_tail_without_normalization(pointee_ty).kind(),
|
|
|
|
|
ty::Str | ty::Slice(..),
|
|
|
|
|
),
|
|
|
|
|
"`ConstValue::Slice` is for slice-tailed types only, but got {}",
|
|
|
|
|
imm.layout.ty,
|
|
|
|
|
);
|
|
|
|
|
let msg = "`op_to_const` on an immediate scalar pair must only be used on slice references to the beginning of an actual allocation";
|
2021-07-12 20:29:05 +02:00
|
|
|
// We know `offset` is relative to the allocation, so we can use `into_parts`.
|
2023-09-12 15:57:40 +02:00
|
|
|
let (alloc_id, offset) = a.to_pointer(ecx).expect(msg).into_parts();
|
|
|
|
|
let alloc_id = alloc_id.expect(msg);
|
|
|
|
|
let data = ecx.tcx.global_alloc(alloc_id).unwrap_memory();
|
2023-09-15 15:59:47 +02:00
|
|
|
assert!(offset == abi::Size::ZERO, "{}", msg);
|
|
|
|
|
let meta = b.to_target_usize(ecx).expect(msg);
|
|
|
|
|
ConstValue::Slice { data, meta }
|
2020-04-13 17:07:54 +02:00
|
|
|
}
|
2023-09-12 07:49:25 +02:00
|
|
|
Immediate::Uninit => bug!("`Uninit` is not a valid value for {}", op.layout.ty),
|
2020-04-13 18:05:05 +02:00
|
|
|
},
|
2020-02-15 11:56:23 +13:00
|
|
|
}
|
2019-12-22 22:20:46 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-28 15:18:07 +00:00
|
|
|
#[instrument(skip(tcx), level = "debug", ret)]
|
2022-02-16 10:56:01 +01:00
|
|
|
pub(crate) fn turn_into_const_value<'tcx>(
|
2019-12-25 01:06:51 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-09-07 17:30:38 +02:00
|
|
|
constant: ConstAlloc<'tcx>,
|
2019-12-25 01:06:51 +01:00
|
|
|
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
|
2020-08-10 11:57:20 +02:00
|
|
|
) -> ConstValue<'tcx> {
|
2019-12-25 01:06:51 +01:00
|
|
|
let cid = key.value;
|
|
|
|
|
let def_id = cid.instance.def.def_id();
|
|
|
|
|
let is_static = tcx.is_static(def_id);
|
2023-04-10 22:02:52 +02:00
|
|
|
// This is just accessing an already computed constant, so no need to check alignment here.
|
2022-08-21 20:00:38 -04:00
|
|
|
let ecx = mk_eval_cx(
|
|
|
|
|
tcx,
|
|
|
|
|
tcx.def_span(key.value.instance.def_id()),
|
|
|
|
|
key.param_env,
|
2023-06-24 20:40:40 +00:00
|
|
|
CanAccessStatics::from(is_static),
|
2022-08-21 20:00:38 -04:00
|
|
|
);
|
2019-12-25 01:06:51 +01:00
|
|
|
|
2020-08-10 11:57:20 +02:00
|
|
|
let mplace = ecx.raw_const_to_mplace(constant).expect(
|
|
|
|
|
"can only fail if layout computation failed, \
|
|
|
|
|
which should have given a good error before ever invoking this function",
|
|
|
|
|
);
|
2020-07-31 13:27:54 +02:00
|
|
|
assert!(
|
|
|
|
|
!is_static || cid.promoted.is_some(),
|
2020-09-19 10:57:14 +02:00
|
|
|
"the `eval_to_const_value_raw` query should not be used for statics, use `eval_to_allocation` instead"
|
2020-07-31 13:27:54 +02:00
|
|
|
);
|
2022-04-08 13:09:24 +02:00
|
|
|
|
2022-04-26 10:58:45 +02:00
|
|
|
// Turn this into a proper constant.
|
2022-06-28 15:18:07 +00:00
|
|
|
op_to_const(&ecx, &mplace.into())
|
2019-12-25 01:06:51 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-16 10:56:01 +01:00
|
|
|
#[instrument(skip(tcx), level = "debug")]
|
2020-09-19 10:57:14 +02:00
|
|
|
pub fn eval_to_const_value_raw_provider<'tcx>(
|
2019-12-25 01:06:51 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
|
2020-08-20 18:55:07 +02:00
|
|
|
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
|
2021-02-13 14:42:30 +00:00
|
|
|
// see comment in eval_to_allocation_raw_provider for what we're doing here
|
2020-07-02 20:52:40 -04:00
|
|
|
if key.param_env.reveal() == Reveal::All {
|
2020-01-22 16:30:15 +01:00
|
|
|
let mut key = key;
|
2020-07-02 20:52:40 -04:00
|
|
|
key.param_env = key.param_env.with_user_facing();
|
2020-09-19 10:57:14 +02:00
|
|
|
match tcx.eval_to_const_value_raw(key) {
|
2019-12-25 01:06:51 +01:00
|
|
|
// try again with reveal all as requested
|
2023-09-11 09:52:45 +02:00
|
|
|
Err(ErrorHandled::TooGeneric(_)) => {}
|
2020-03-06 12:13:55 +01:00
|
|
|
// deduplicate calls
|
2019-12-25 01:06:51 +01:00
|
|
|
other => return other,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We call `const_eval` for zero arg intrinsics, too, in order to cache their value.
|
|
|
|
|
// Catch such calls and evaluate them instead of trying to load a constant's MIR.
|
|
|
|
|
if let ty::InstanceDef::Intrinsic(def_id) = key.value.instance.def {
|
2020-06-22 13:57:03 +01:00
|
|
|
let ty = key.value.instance.ty(tcx, key.param_env);
|
2023-07-11 22:35:29 +01:00
|
|
|
let ty::FnDef(_, args) = ty.kind() else {
|
2022-02-19 00:47:43 +01:00
|
|
|
bug!("intrinsic with type {:?}", ty);
|
2019-12-25 01:06:51 +01:00
|
|
|
};
|
2023-07-11 22:35:29 +01:00
|
|
|
return eval_nullary_intrinsic(tcx, key.param_env, def_id, args).map_err(|error| {
|
2019-12-25 01:06:51 +01:00
|
|
|
let span = tcx.def_span(def_id);
|
2023-05-17 10:30:14 +00:00
|
|
|
|
|
|
|
|
super::report(
|
|
|
|
|
tcx,
|
|
|
|
|
error.into_kind(),
|
|
|
|
|
Some(span),
|
|
|
|
|
|| (span, vec![]),
|
|
|
|
|
|span, _| errors::NullaryIntrinsicError { span },
|
|
|
|
|
)
|
2019-12-25 01:06:51 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 17:30:38 +02:00
|
|
|
tcx.eval_to_allocation_raw(key).map(|val| turn_into_const_value(tcx, val, key))
|
2019-12-25 01:06:51 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-16 10:56:01 +01:00
|
|
|
#[instrument(skip(tcx), level = "debug")]
|
2020-08-20 18:55:07 +02:00
|
|
|
pub fn eval_to_allocation_raw_provider<'tcx>(
|
2019-12-25 01:06:51 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
|
2020-08-20 18:55:07 +02:00
|
|
|
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
|
2019-12-25 01:06:51 +01:00
|
|
|
// Because the constant is computed twice (once per value of `Reveal`), we are at risk of
|
|
|
|
|
// reporting the same error twice here. To resolve this, we check whether we can evaluate the
|
|
|
|
|
// constant in the more restrictive `Reveal::UserFacing`, which most likely already was
|
|
|
|
|
// computed. For a large percentage of constants that will already have succeeded. Only
|
|
|
|
|
// associated constants of generic functions will fail due to not enough monomorphization
|
|
|
|
|
// information being available.
|
|
|
|
|
|
|
|
|
|
// In case we fail in the `UserFacing` variant, we just do the real computation.
|
2020-07-02 20:52:40 -04:00
|
|
|
if key.param_env.reveal() == Reveal::All {
|
2020-01-22 16:30:15 +01:00
|
|
|
let mut key = key;
|
2020-07-02 20:52:40 -04:00
|
|
|
key.param_env = key.param_env.with_user_facing();
|
2020-08-20 18:55:07 +02:00
|
|
|
match tcx.eval_to_allocation_raw(key) {
|
2019-12-25 01:06:51 +01:00
|
|
|
// try again with reveal all as requested
|
2023-09-11 09:52:45 +02:00
|
|
|
Err(ErrorHandled::TooGeneric(_)) => {}
|
2020-03-06 12:13:55 +01:00
|
|
|
// deduplicate calls
|
2019-12-25 01:06:51 +01:00
|
|
|
other => return other,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
|
// Make sure we format the instance even if we do not print it.
|
|
|
|
|
// This serves as a regression test against an ICE on printing.
|
|
|
|
|
// The next two lines concatenated contain some discussion:
|
|
|
|
|
// https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/
|
|
|
|
|
// subject/anon_const_instance_printing/near/135980032
|
2022-02-16 13:04:48 -05:00
|
|
|
let instance = with_no_trimmed_paths!(key.value.instance.to_string());
|
2019-12-25 01:06:51 +01:00
|
|
|
trace!("const eval: {:?} ({})", key, instance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cid = key.value;
|
2022-05-08 15:53:19 +02:00
|
|
|
let def = cid.instance.def.def_id();
|
|
|
|
|
let is_static = tcx.is_static(def);
|
2019-12-25 01:06:51 +01:00
|
|
|
|
|
|
|
|
let mut ecx = InterpCx::new(
|
2020-06-01 10:15:17 +02:00
|
|
|
tcx,
|
2022-05-08 15:53:19 +02:00
|
|
|
tcx.def_span(def),
|
2019-12-25 01:06:51 +01:00
|
|
|
key.param_env,
|
2021-01-24 12:50:30 +01:00
|
|
|
// Statics (and promoteds inside statics) may access other statics, because unlike consts
|
|
|
|
|
// they do not have to behave "as if" they were evaluated at runtime.
|
2022-08-21 20:00:38 -04:00
|
|
|
CompileTimeInterpreter::new(
|
2023-06-24 20:40:40 +00:00
|
|
|
CanAccessStatics::from(is_static),
|
2022-11-21 16:51:16 +00:00
|
|
|
if tcx.sess.opts.unstable_opts.extra_const_ub_checks {
|
|
|
|
|
CheckAlignment::Error
|
|
|
|
|
} else {
|
|
|
|
|
CheckAlignment::FutureIncompat
|
|
|
|
|
},
|
2022-08-21 20:00:38 -04:00
|
|
|
),
|
2019-12-25 01:06:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let res = ecx.load_mir(cid.instance.def, cid.promoted);
|
2020-08-10 12:04:01 +02:00
|
|
|
match res.and_then(|body| eval_body_using_ecx(&mut ecx, cid, &body)) {
|
|
|
|
|
Err(error) => {
|
2023-05-17 10:30:14 +00:00
|
|
|
let (error, backtrace) = error.into_parts();
|
|
|
|
|
backtrace.print_backtrace();
|
|
|
|
|
|
|
|
|
|
let (kind, instance) = if is_static {
|
|
|
|
|
("static", String::new())
|
2019-12-25 01:06:51 +01:00
|
|
|
} else {
|
2022-09-21 13:05:20 +02:00
|
|
|
// If the current item has generics, we'd like to enrich the message with the
|
2023-07-11 22:35:29 +01:00
|
|
|
// instance and its args: to show the actual compile-time values, in addition to
|
2022-09-21 13:05:20 +02:00
|
|
|
// the expression, leading to the const eval error.
|
|
|
|
|
let instance = &key.value.instance;
|
2023-07-11 22:35:29 +01:00
|
|
|
if !instance.args.is_empty() {
|
2022-09-21 13:05:20 +02:00
|
|
|
let instance = with_no_trimmed_paths!(instance.to_string());
|
2023-05-17 10:30:14 +00:00
|
|
|
("const_with_path", instance)
|
2021-01-24 12:50:30 +01:00
|
|
|
} else {
|
2023-05-17 10:30:14 +00:00
|
|
|
("const", String::new())
|
2022-09-21 13:05:20 +02:00
|
|
|
}
|
|
|
|
|
};
|
2021-05-27 22:42:07 +02:00
|
|
|
|
2023-05-17 10:30:14 +00:00
|
|
|
Err(super::report(
|
|
|
|
|
*ecx.tcx,
|
|
|
|
|
error,
|
|
|
|
|
None,
|
|
|
|
|
|| super::get_span_and_frames(&ecx),
|
|
|
|
|
|span, frames| ConstEvalError {
|
|
|
|
|
span,
|
|
|
|
|
error_kind: kind,
|
|
|
|
|
instance,
|
|
|
|
|
frame_notes: frames,
|
|
|
|
|
},
|
|
|
|
|
))
|
2020-08-10 12:04:01 +02:00
|
|
|
}
|
|
|
|
|
Ok(mplace) => {
|
2021-01-24 12:50:30 +01:00
|
|
|
// Since evaluation had no errors, validate the resulting constant.
|
|
|
|
|
// This is a separate `try` block to provide more targeted error reporting.
|
2023-05-17 10:30:14 +00:00
|
|
|
let validation: Result<_, InterpErrorInfo<'_>> = try {
|
2023-07-25 22:35:07 +02:00
|
|
|
let mut ref_tracking = RefTracking::new(mplace.clone());
|
2020-12-20 15:49:08 +01:00
|
|
|
let mut inner = false;
|
|
|
|
|
while let Some((mplace, path)) = ref_tracking.todo.pop() {
|
|
|
|
|
let mode = match tcx.static_mutability(cid.instance.def_id()) {
|
2020-12-20 19:34:29 +01:00
|
|
|
Some(_) if cid.promoted.is_some() => {
|
|
|
|
|
// Promoteds in statics are allowed to point to statics.
|
|
|
|
|
CtfeValidationMode::Const { inner, allow_static_ptrs: true }
|
|
|
|
|
}
|
|
|
|
|
Some(_) => CtfeValidationMode::Regular, // a `static`
|
|
|
|
|
None => CtfeValidationMode::Const { inner, allow_static_ptrs: false },
|
2020-12-20 15:49:08 +01:00
|
|
|
};
|
2021-02-15 00:00:00 +00:00
|
|
|
ecx.const_validate_operand(&mplace.into(), path, &mut ref_tracking, mode)?;
|
2020-12-20 15:49:08 +01:00
|
|
|
inner = true;
|
2020-07-30 17:58:39 +02:00
|
|
|
}
|
|
|
|
|
};
|
2023-09-04 17:53:38 +02:00
|
|
|
let alloc_id = mplace.ptr().provenance.unwrap();
|
2023-05-17 10:30:14 +00:00
|
|
|
|
2023-09-12 13:41:42 +02:00
|
|
|
// Validation failed, report an error.
|
2020-07-30 17:58:39 +02:00
|
|
|
if let Err(error) = validation {
|
2023-05-17 10:30:14 +00:00
|
|
|
let (error, backtrace) = error.into_parts();
|
|
|
|
|
backtrace.print_backtrace();
|
|
|
|
|
|
|
|
|
|
let ub_note = matches!(error, InterpError::UndefinedBehavior(_)).then(|| {});
|
|
|
|
|
|
|
|
|
|
let alloc = ecx.tcx.global_alloc(alloc_id).unwrap_memory().inner();
|
|
|
|
|
let mut bytes = String::new();
|
|
|
|
|
if alloc.size() != abi::Size::ZERO {
|
|
|
|
|
bytes = "\n".into();
|
|
|
|
|
// FIXME(translation) there might be pieces that are translatable.
|
|
|
|
|
write_allocation_bytes(*ecx.tcx, alloc, &mut bytes, " ").unwrap();
|
|
|
|
|
}
|
|
|
|
|
let raw_bytes = errors::RawBytesNote {
|
|
|
|
|
size: alloc.size().bytes(),
|
|
|
|
|
align: alloc.align.bytes(),
|
|
|
|
|
bytes,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Err(super::report(
|
|
|
|
|
*ecx.tcx,
|
|
|
|
|
error,
|
|
|
|
|
None,
|
|
|
|
|
|| super::get_span_and_frames(&ecx),
|
|
|
|
|
move |span, frames| errors::UndefinedBehavior {
|
|
|
|
|
span,
|
|
|
|
|
ub_note,
|
|
|
|
|
frames,
|
|
|
|
|
raw_bytes,
|
2020-07-30 17:58:39 +02:00
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
// Convert to raw constant
|
2021-07-12 18:22:15 +02:00
|
|
|
Ok(ConstAlloc { alloc_id, ty: mplace.layout.ty })
|
2020-07-30 17:58:39 +02:00
|
|
|
}
|
2020-08-10 12:04:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-12-25 01:06:51 +01:00
|
|
|
}
|