2020-08-09 13:37:32 +00:00
|
|
|
use super::{CompileTimeEvalContext, CompileTimeInterpreter, ConstEvalErr, MemoryExtra};
|
2019-12-25 00:06:51 +00:00
|
|
|
use crate::interpret::eval_nullary_intrinsic;
|
2019-12-23 14:02:55 +00:00
|
|
|
use crate::interpret::{
|
2020-10-24 18:49:17 +00:00
|
|
|
intern_const_alloc_recursive, Allocation, ConstAlloc, ConstValue, CtfeValidationMode, GlobalId,
|
|
|
|
Immediate, InternKind, InterpCx, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking, Scalar,
|
2020-04-22 07:20:40 +00:00
|
|
|
ScalarMaybeUninit, StackPopCleanup,
|
2019-12-23 14:02:55 +00:00
|
|
|
};
|
2020-08-09 13:37:32 +00:00
|
|
|
|
2020-11-06 15:57:05 +00:00
|
|
|
use rustc_errors::ErrorReported;
|
2020-12-11 15:19:30 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir;
|
2020-08-09 13:37:32 +00:00
|
|
|
use rustc_middle::mir::interpret::ErrorHandled;
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::traits::Reveal;
|
2020-09-02 07:40:56 +00:00
|
|
|
use rustc_middle::ty::print::with_no_trimmed_paths;
|
2020-03-31 16:16:47 +00:00
|
|
|
use rustc_middle::ty::{self, subst::Subst, TyCtxt};
|
2020-01-01 18:25:28 +00:00
|
|
|
use rustc_span::source_map::Span;
|
2020-03-31 16:16:47 +00:00
|
|
|
use rustc_target::abi::{Abi, LayoutOf};
|
2020-10-01 10:51:44 +00:00
|
|
|
use std::convert::TryInto;
|
2019-12-25 00:06:51 +00:00
|
|
|
|
|
|
|
pub fn note_on_undefined_behavior_error() -> &'static str {
|
|
|
|
"The rules on what exactly is undefined behavior aren't clear, \
|
|
|
|
so this check might be overzealous. Please open an issue on the rustc \
|
|
|
|
repository if you believe it should not be considered undefined behavior."
|
|
|
|
}
|
|
|
|
|
2019-12-22 21:20:46 +00: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-10 22:59:05 +00:00
|
|
|
let tcx = *ecx.tcx;
|
2020-12-09 11:53:35 +00:00
|
|
|
assert!(
|
|
|
|
cid.promoted.is_some()
|
|
|
|
|| matches!(
|
2020-12-11 15:19:30 +00:00
|
|
|
ecx.tcx.def_kind(cid.instance.def_id()),
|
|
|
|
DefKind::Const
|
|
|
|
| DefKind::Static
|
|
|
|
| DefKind::ConstParam
|
|
|
|
| DefKind::AnonConst
|
|
|
|
| DefKind::AssocConst
|
|
|
|
),
|
|
|
|
"Unexpected DefKind: {:?}",
|
|
|
|
ecx.tcx.def_kind(cid.instance.def_id())
|
2020-12-09 11:53:35 +00:00
|
|
|
);
|
2019-12-22 21:20:46 +00:00
|
|
|
let layout = ecx.layout_of(body.return_ty().subst(tcx, cid.instance.substs))?;
|
|
|
|
assert!(!layout.is_unsized());
|
|
|
|
let ret = ecx.allocate(layout, MemoryKind::Stack);
|
|
|
|
|
2020-09-02 07:40:56 +00:00
|
|
|
let name =
|
|
|
|
with_no_trimmed_paths(|| ty::tls::with(|tcx| tcx.def_path_str(cid.instance.def_id())));
|
2019-12-22 21:20:46 +00:00
|
|
|
let prom = cid.promoted.map_or(String::new(), |p| format!("::promoted[{:?}]", p));
|
|
|
|
trace!("eval_body_using_ecx: pushing stack frame for global: {}{}", name, prom);
|
|
|
|
|
|
|
|
ecx.push_stack_frame(
|
|
|
|
cid.instance,
|
|
|
|
body,
|
|
|
|
Some(ret.into()),
|
|
|
|
StackPopCleanup::None { cleanup: false },
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// The main interpreter loop.
|
|
|
|
ecx.run()?;
|
|
|
|
|
|
|
|
// Intern the result
|
2020-10-24 15:23:45 +00: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 12:58:02 +00:00
|
|
|
};
|
2020-11-04 16:53:43 +00:00
|
|
|
intern_const_alloc_recursive(ecx, intern_kind, ret)?;
|
2019-12-22 21:20:46 +00:00
|
|
|
|
|
|
|
debug!("eval_body_using_ecx done: {:?}", *ret);
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:00:39 +00:00
|
|
|
/// The `InterpCx` is only meant to be used to do field and index projections into constants for
|
|
|
|
/// `simd_shuffle` and const patterns in match arms.
|
2019-12-22 21:20:46 +00: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 08:15:17 +00:00
|
|
|
root_span: Span,
|
2019-12-22 21:20:46 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
can_access_statics: bool,
|
|
|
|
) -> CompileTimeEvalContext<'mir, 'tcx> {
|
|
|
|
debug!("mk_eval_cx: {:?}", param_env);
|
|
|
|
InterpCx::new(
|
2020-06-01 08:15:17 +00:00
|
|
|
tcx,
|
|
|
|
root_span,
|
2019-12-22 21:20:46 +00:00
|
|
|
param_env,
|
2020-05-16 04:44:28 +00:00
|
|
|
CompileTimeInterpreter::new(tcx.sess.const_eval_limit()),
|
2019-12-22 21:20:46 +00:00
|
|
|
MemoryExtra { can_access_statics },
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-10 09:48:52 +00:00
|
|
|
/// This function converts an interpreter value into a constant that is meant for use in the
|
|
|
|
/// type system.
|
2019-12-22 21:20:46 +00:00
|
|
|
pub(super) fn op_to_const<'tcx>(
|
|
|
|
ecx: &CompileTimeEvalContext<'_, 'tcx>,
|
|
|
|
op: OpTy<'tcx>,
|
2020-02-14 22:56:23 +00:00
|
|
|
) -> ConstValue<'tcx> {
|
2019-12-22 21:20:46 +00:00
|
|
|
// We do not have value optimizations for everything.
|
|
|
|
// Only scalars and slices, since they are very common.
|
2020-04-22 07:20:40 +00:00
|
|
|
// Note that further down we turn scalars of uninitialized bits back to `ByRef`. These can result
|
2019-12-22 21:20:46 +00:00
|
|
|
// from scalar unions that are initialized with one of their zero sized variants. We could
|
2020-04-22 07:20:40 +00:00
|
|
|
// instead allow `ConstValue::Scalar` to store `ScalarMaybeUninit`, but that would affect all
|
2019-12-22 21:20:46 +00:00
|
|
|
// the usual cases of extracting e.g. a `usize`, without there being a real use case for the
|
|
|
|
// `Undef` situation.
|
|
|
|
let try_as_immediate = match op.layout.abi {
|
2020-03-31 16:16:47 +00:00
|
|
|
Abi::Scalar(..) => true,
|
2020-08-02 22:49:11 +00:00
|
|
|
Abi::ScalarPair(..) => match op.layout.ty.kind() {
|
|
|
|
ty::Ref(_, inner, _) => match *inner.kind() {
|
2019-12-22 21:20:46 +00:00
|
|
|
ty::Slice(elem) => elem == ecx.tcx.types.u8,
|
|
|
|
ty::Str => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
let immediate = if try_as_immediate {
|
|
|
|
Err(ecx.read_immediate(op).expect("normalization works on validated constants"))
|
|
|
|
} else {
|
|
|
|
// It is guaranteed that any non-slice scalar pair is actually ByRef here.
|
|
|
|
// When we come back from raw const eval, we are always by-ref. The only way our op here is
|
2020-05-24 14:08:54 +00:00
|
|
|
// by-val is if we are in destructure_const, i.e., if this is (a field of) something that we
|
2019-12-22 21:20:46 +00:00
|
|
|
// "tried to make immediate" before. We wouldn't do that for non-slice scalar pairs or
|
|
|
|
// structs containing such.
|
2019-12-21 22:55:34 +00:00
|
|
|
op.try_as_mplace(ecx)
|
2019-12-22 21:20:46 +00:00
|
|
|
};
|
2019-12-26 22:32:34 +00:00
|
|
|
|
|
|
|
let to_const_value = |mplace: MPlaceTy<'_>| match mplace.ptr {
|
|
|
|
Scalar::Ptr(ptr) => {
|
2020-05-08 08:58:53 +00:00
|
|
|
let alloc = ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory();
|
2019-12-26 22:32:34 +00:00
|
|
|
ConstValue::ByRef { alloc, offset: ptr.offset }
|
|
|
|
}
|
2020-11-01 16:57:03 +00:00
|
|
|
Scalar::Int(int) => {
|
2020-01-09 11:03:37 +00:00
|
|
|
assert!(mplace.layout.is_zst());
|
2020-01-07 14:51:43 +00:00
|
|
|
assert_eq!(
|
2020-10-01 10:51:44 +00:00
|
|
|
int.assert_bits(ecx.tcx.data_layout.pointer_size)
|
|
|
|
% u128::from(mplace.layout.align.abi.bytes()),
|
2020-09-30 08:40:49 +00:00
|
|
|
0,
|
|
|
|
"this MPlaceTy must come from a validated constant, thus we can assume the \
|
|
|
|
alignment is correct",
|
2020-01-07 14:51:43 +00:00
|
|
|
);
|
2020-11-01 17:04:13 +00:00
|
|
|
ConstValue::Scalar(Scalar::ZST)
|
2019-12-26 22:32:34 +00:00
|
|
|
}
|
|
|
|
};
|
2020-02-14 22:56:23 +00:00
|
|
|
match immediate {
|
2019-12-26 22:32:34 +00:00
|
|
|
Ok(mplace) => to_const_value(mplace),
|
2019-12-22 21:20:46 +00:00
|
|
|
// see comment on `let try_as_immediate` above
|
2020-04-13 15:07:54 +00:00
|
|
|
Err(imm) => match *imm {
|
|
|
|
Immediate::Scalar(x) => match x {
|
2020-04-22 07:20:40 +00:00
|
|
|
ScalarMaybeUninit::Scalar(s) => ConstValue::Scalar(s),
|
|
|
|
ScalarMaybeUninit::Uninit => to_const_value(op.assert_mem_place(ecx)),
|
2020-04-13 15:07:54 +00:00
|
|
|
},
|
|
|
|
Immediate::ScalarPair(a, b) => {
|
2020-07-21 21:17:32 +00:00
|
|
|
let (data, start) = match a.check_init().unwrap() {
|
2020-04-13 15:07:54 +00:00
|
|
|
Scalar::Ptr(ptr) => {
|
2020-05-08 08:58:53 +00:00
|
|
|
(ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory(), ptr.offset.bytes())
|
2020-04-13 15:07:54 +00:00
|
|
|
}
|
2020-11-01 16:57:03 +00:00
|
|
|
Scalar::Int { .. } => (
|
2020-04-13 16:05:05 +00:00
|
|
|
ecx.tcx
|
|
|
|
.intern_const_alloc(Allocation::from_byte_aligned_bytes(b"" as &[u8])),
|
2020-04-13 15:07:54 +00:00
|
|
|
0,
|
|
|
|
),
|
|
|
|
};
|
2020-06-01 08:15:17 +00:00
|
|
|
let len = b.to_machine_usize(ecx).unwrap();
|
2020-04-13 15:07:54 +00:00
|
|
|
let start = start.try_into().unwrap();
|
|
|
|
let len: usize = len.try_into().unwrap();
|
|
|
|
ConstValue::Slice { data, start, end: start + len }
|
|
|
|
}
|
2020-04-13 16:05:05 +00:00
|
|
|
},
|
2020-02-14 22:56:23 +00:00
|
|
|
}
|
2019-12-22 21:20:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 15:30:38 +00:00
|
|
|
fn turn_into_const_value<'tcx>(
|
2019-12-25 00:06:51 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-09-07 15:30:38 +00:00
|
|
|
constant: ConstAlloc<'tcx>,
|
2019-12-25 00:06:51 +00:00
|
|
|
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
|
2020-08-10 09:57:20 +00:00
|
|
|
) -> ConstValue<'tcx> {
|
2019-12-25 00:06:51 +00:00
|
|
|
let cid = key.value;
|
|
|
|
let def_id = cid.instance.def.def_id();
|
|
|
|
let is_static = tcx.is_static(def_id);
|
|
|
|
let ecx = mk_eval_cx(tcx, tcx.def_span(key.value.instance.def_id()), key.param_env, is_static);
|
|
|
|
|
2020-08-10 09:57:20 +00: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 11:27:54 +00:00
|
|
|
assert!(
|
|
|
|
!is_static || cid.promoted.is_some(),
|
2020-09-19 08:57:14 +00:00
|
|
|
"the `eval_to_const_value_raw` query should not be used for statics, use `eval_to_allocation` instead"
|
2020-07-31 11:27:54 +00:00
|
|
|
);
|
|
|
|
// Turn this into a proper constant.
|
2020-08-10 09:57:20 +00:00
|
|
|
op_to_const(&ecx, mplace.into())
|
2019-12-25 00:06:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 08:57:14 +00:00
|
|
|
pub fn eval_to_const_value_raw_provider<'tcx>(
|
2019-12-25 00:06:51 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
|
2020-08-20 16:55:07 +00: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-03 00:52:40 +00:00
|
|
|
if key.param_env.reveal() == Reveal::All {
|
2020-01-22 15:30:15 +00:00
|
|
|
let mut key = key;
|
2020-07-03 00:52:40 +00:00
|
|
|
key.param_env = key.param_env.with_user_facing();
|
2020-09-19 08:57:14 +00:00
|
|
|
match tcx.eval_to_const_value_raw(key) {
|
2019-12-25 00:06:51 +00:00
|
|
|
// try again with reveal all as requested
|
2019-12-27 16:44:36 +00:00
|
|
|
Err(ErrorHandled::TooGeneric) => {}
|
2020-03-06 11:13:55 +00:00
|
|
|
// deduplicate calls
|
2019-12-25 00:06:51 +00: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 12:57:03 +00:00
|
|
|
let ty = key.value.instance.ty(tcx, key.param_env);
|
2020-08-02 22:49:11 +00:00
|
|
|
let substs = match ty.kind() {
|
2019-12-25 00:06:51 +00:00
|
|
|
ty::FnDef(_, substs) => substs,
|
|
|
|
_ => bug!("intrinsic with type {:?}", ty),
|
|
|
|
};
|
|
|
|
return eval_nullary_intrinsic(tcx, key.param_env, def_id, substs).map_err(|error| {
|
|
|
|
let span = tcx.def_span(def_id);
|
|
|
|
let error = ConstEvalErr { error: error.kind, stacktrace: vec![], span };
|
|
|
|
error.report_as_error(tcx.at(span), "could not evaluate nullary intrinsic")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-07 15:30:38 +00:00
|
|
|
tcx.eval_to_allocation_raw(key).map(|val| turn_into_const_value(tcx, val, key))
|
2019-12-25 00:06:51 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 16:55:07 +00:00
|
|
|
pub fn eval_to_allocation_raw_provider<'tcx>(
|
2019-12-25 00:06:51 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
|
2020-08-20 16:55:07 +00:00
|
|
|
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
|
2019-12-25 00:06:51 +00: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-03 00:52:40 +00:00
|
|
|
if key.param_env.reveal() == Reveal::All {
|
2020-01-22 15:30:15 +00:00
|
|
|
let mut key = key;
|
2020-07-03 00:52:40 +00:00
|
|
|
key.param_env = key.param_env.with_user_facing();
|
2020-08-20 16:55:07 +00:00
|
|
|
match tcx.eval_to_allocation_raw(key) {
|
2019-12-25 00:06:51 +00:00
|
|
|
// try again with reveal all as requested
|
|
|
|
Err(ErrorHandled::TooGeneric) => {}
|
2020-03-06 11:13:55 +00:00
|
|
|
// deduplicate calls
|
2019-12-25 00:06:51 +00: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
|
2020-09-02 07:40:56 +00:00
|
|
|
let instance = with_no_trimmed_paths(|| key.value.instance.to_string());
|
2019-12-25 00:06:51 +00:00
|
|
|
trace!("const eval: {:?} ({})", key, instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
let cid = key.value;
|
2020-07-03 17:13:39 +00:00
|
|
|
let def = cid.instance.def.with_opt_param();
|
2019-12-25 00:06:51 +00:00
|
|
|
|
2020-07-03 17:13:39 +00:00
|
|
|
if let Some(def) = def.as_local() {
|
2020-07-17 08:47:04 +00:00
|
|
|
if tcx.has_typeck_results(def.did) {
|
|
|
|
if let Some(error_reported) = tcx.typeck_opt_const_arg(def).tainted_by_errors {
|
2020-04-17 14:17:01 +00:00
|
|
|
return Err(ErrorHandled::Reported(error_reported));
|
|
|
|
}
|
2020-04-12 01:24:25 +00:00
|
|
|
}
|
2020-11-09 17:45:11 +00:00
|
|
|
if !tcx.is_mir_available(def.did) {
|
|
|
|
tcx.sess.delay_span_bug(
|
|
|
|
tcx.def_span(def.did),
|
|
|
|
&format!("no MIR body is available for {:?}", def.did),
|
|
|
|
);
|
|
|
|
return Err(ErrorHandled::Reported(ErrorReported {}));
|
|
|
|
}
|
2020-11-10 09:29:44 +00:00
|
|
|
if let Some(error_reported) = tcx.mir_const_qualif_opt_const_arg(def).error_occured {
|
|
|
|
return Err(ErrorHandled::Reported(error_reported));
|
2020-11-06 15:16:38 +00:00
|
|
|
}
|
2019-12-25 00:06:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 17:13:39 +00:00
|
|
|
let is_static = tcx.is_static(def.did);
|
2019-12-25 00:06:51 +00:00
|
|
|
|
|
|
|
let mut ecx = InterpCx::new(
|
2020-06-01 08:15:17 +00:00
|
|
|
tcx,
|
2020-07-03 17:13:39 +00:00
|
|
|
tcx.def_span(def.did),
|
2019-12-25 00:06:51 +00:00
|
|
|
key.param_env,
|
2020-05-16 04:44:28 +00:00
|
|
|
CompileTimeInterpreter::new(tcx.sess.const_eval_limit()),
|
2021-01-24 11:50:30 +00: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.
|
2019-12-25 00:06:51 +00:00
|
|
|
MemoryExtra { can_access_statics: is_static },
|
|
|
|
);
|
|
|
|
|
|
|
|
let res = ecx.load_mir(cid.instance.def, cid.promoted);
|
2020-08-10 10:04:01 +00:00
|
|
|
match res.and_then(|body| eval_body_using_ecx(&mut ecx, cid, &body)) {
|
|
|
|
Err(error) => {
|
2020-08-09 13:37:32 +00:00
|
|
|
let err = ConstEvalErr::new(&ecx, error, None);
|
2021-01-24 11:50:30 +00:00
|
|
|
// Some CTFE errors raise just a lint, not a hard error; see
|
|
|
|
// <https://github.com/rust-lang/rust/issues/71800>.
|
|
|
|
let emit_as_lint = if let Some(def) = def.as_local() {
|
|
|
|
// (Associated) consts only emit a lint, since they might be unused.
|
|
|
|
matches!(tcx.def_kind(def.did.to_def_id()), DefKind::Const | DefKind::AssocConst)
|
2019-12-25 00:06:51 +00:00
|
|
|
} else {
|
2021-01-24 11:50:30 +00:00
|
|
|
// use of broken constant from other crate: always an error
|
|
|
|
false
|
|
|
|
};
|
|
|
|
if emit_as_lint {
|
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def.as_local().unwrap().did);
|
|
|
|
Err(err.report_as_lint(
|
|
|
|
tcx.at(tcx.def_span(def.did)),
|
|
|
|
"any use of this value will cause an error",
|
|
|
|
hir_id,
|
|
|
|
Some(err.span),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
let msg = if is_static {
|
|
|
|
"could not evaluate static initializer"
|
|
|
|
} else {
|
|
|
|
"evaluation of constant value failed"
|
|
|
|
};
|
|
|
|
Err(err.report_as_error(ecx.tcx.at(ecx.cur_span()), msg))
|
2019-12-25 00:06:51 +00:00
|
|
|
}
|
2020-08-10 10:04:01 +00:00
|
|
|
}
|
|
|
|
Ok(mplace) => {
|
2021-01-24 11:50:30 +00:00
|
|
|
// Since evaluation had no errors, validate the resulting constant.
|
|
|
|
// This is a separate `try` block to provide more targeted error reporting.
|
2020-07-30 15:58:39 +00:00
|
|
|
let validation = try {
|
2020-12-20 14:49:08 +00:00
|
|
|
let mut ref_tracking = RefTracking::new(mplace);
|
|
|
|
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 18:34:29 +00: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 14:49:08 +00:00
|
|
|
};
|
|
|
|
ecx.const_validate_operand(mplace.into(), path, &mut ref_tracking, mode)?;
|
|
|
|
inner = true;
|
2020-07-30 15:58:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Err(error) = validation {
|
2021-01-24 11:50:30 +00:00
|
|
|
// Validation failed, report an error. This is always a hard error.
|
2020-07-30 15:58:39 +00:00
|
|
|
let err = ConstEvalErr::new(&ecx, error, None);
|
|
|
|
Err(err.struct_error(
|
|
|
|
ecx.tcx,
|
|
|
|
"it is undefined behavior to use this value",
|
|
|
|
|mut diag| {
|
|
|
|
diag.note(note_on_undefined_behavior_error());
|
|
|
|
diag.emit();
|
|
|
|
},
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
// Convert to raw constant
|
2020-09-07 15:30:38 +00:00
|
|
|
Ok(ConstAlloc { alloc_id: mplace.ptr.assert_ptr().alloc_id, ty: mplace.layout.ty })
|
2020-07-30 15:58:39 +00:00
|
|
|
}
|
2020-08-10 10:04:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-25 00:06:51 +00:00
|
|
|
}
|