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-01-05 01:37:57 +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-11-06 15:16:38 +00:00
|
|
|
use rustc_errors::ErrorReported;
|
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-06-14 13:02:51 +00:00
|
|
|
let tcx = *ecx.tcx;
|
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);
|
|
|
|
|
|
|
|
// Assert all args (if any) are zero-sized types; `eval_body_using_ecx` doesn't
|
|
|
|
// make sense if the body is expecting nontrivial arguments.
|
|
|
|
// (The alternative would be to use `eval_fn_call` with an args slice.)
|
|
|
|
for arg in body.args_iter() {
|
|
|
|
let decl = body.local_decls.get(arg).expect("arg missing from local_decls");
|
|
|
|
let layout = ecx.layout_of(decl.ty.subst(tcx, cid.instance.substs))?;
|
|
|
|
assert!(layout.is_zst())
|
|
|
|
}
|
|
|
|
|
|
|
|
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> {
|
|
|
|
// see comment in const_eval_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-06 15:16:38 +00:00
|
|
|
let qualif = tcx.mir_const_qualif_opt_const_arg(def);
|
|
|
|
if qualif.error_occured {
|
|
|
|
return Err(ErrorHandled::Reported(ErrorReported {}));
|
|
|
|
}
|
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()),
|
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);
|
2019-12-25 00:06:51 +00:00
|
|
|
// errors in statics are always emitted as fatal errors
|
|
|
|
if is_static {
|
|
|
|
// Ensure that if the above error was either `TooGeneric` or `Reported`
|
|
|
|
// an error must be reported.
|
2020-06-11 07:53:38 +00:00
|
|
|
let v = err.report_as_error(
|
|
|
|
ecx.tcx.at(ecx.cur_span()),
|
|
|
|
"could not evaluate static initializer",
|
|
|
|
);
|
2019-12-27 16:44:36 +00:00
|
|
|
|
|
|
|
// If this is `Reveal:All`, then we need to make sure an error is reported but if
|
|
|
|
// this is `Reveal::UserFacing`, then it's expected that we could get a
|
|
|
|
// `TooGeneric` error. When we fall back to `Reveal::All`, then it will either
|
|
|
|
// succeed or we'll report this error then.
|
2020-07-03 00:52:40 +00:00
|
|
|
if key.param_env.reveal() == Reveal::All {
|
2019-12-27 16:44:36 +00:00
|
|
|
tcx.sess.delay_span_bug(
|
|
|
|
err.span,
|
|
|
|
&format!("static eval failure did not emit an error: {:#?}", v),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-10 10:04:01 +00:00
|
|
|
Err(v)
|
2020-07-03 17:13:39 +00:00
|
|
|
} else if let Some(def) = def.as_local() {
|
2019-12-25 00:06:51 +00:00
|
|
|
// constant defined in this crate, we can figure out a lint level!
|
2020-07-03 17:13:39 +00:00
|
|
|
match tcx.def_kind(def.did.to_def_id()) {
|
2019-12-25 00:06:51 +00:00
|
|
|
// constants never produce a hard error at the definition site. Anything else is
|
2019-12-23 16:31:55 +00:00
|
|
|
// a backwards compatibility hazard (and will break old versions of winapi for
|
|
|
|
// sure)
|
2019-12-25 00:06:51 +00:00
|
|
|
//
|
|
|
|
// note that validation may still cause a hard error on this very same constant,
|
2019-12-23 16:31:55 +00:00
|
|
|
// because any code that existed before validation could not have failed
|
|
|
|
// validation thus preventing such a hard error from being a backwards
|
|
|
|
// compatibility hazard
|
2020-04-17 18:55:17 +00:00
|
|
|
DefKind::Const | DefKind::AssocConst => {
|
2020-08-12 10:22:56 +00:00
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
|
2020-08-10 10:04:01 +00:00
|
|
|
Err(err.report_as_lint(
|
2020-07-03 17:13:39 +00:00
|
|
|
tcx.at(tcx.def_span(def.did)),
|
2019-12-25 00:06:51 +00:00
|
|
|
"any use of this value will cause an error",
|
|
|
|
hir_id,
|
|
|
|
Some(err.span),
|
2020-08-10 10:04:01 +00:00
|
|
|
))
|
2019-12-25 00:06:51 +00:00
|
|
|
}
|
2019-12-23 16:31:55 +00:00
|
|
|
// promoting runtime code is only allowed to error if it references broken
|
|
|
|
// constants any other kind of error will be reported to the user as a
|
|
|
|
// deny-by-default lint
|
2019-12-25 00:06:51 +00:00
|
|
|
_ => {
|
|
|
|
if let Some(p) = cid.promoted {
|
2020-10-05 06:49:21 +00:00
|
|
|
let span = tcx.promoted_mir_opt_const_arg(def.to_global())[p].span;
|
2019-12-25 00:06:51 +00:00
|
|
|
if let err_inval!(ReferencedConstant) = err.error {
|
2020-08-10 10:04:01 +00:00
|
|
|
Err(err.report_as_error(
|
2019-12-25 00:06:51 +00:00
|
|
|
tcx.at(span),
|
|
|
|
"evaluation of constant expression failed",
|
2020-08-10 10:04:01 +00:00
|
|
|
))
|
2019-12-25 00:06:51 +00:00
|
|
|
} else {
|
2020-08-10 10:04:01 +00:00
|
|
|
Err(err.report_as_lint(
|
2019-12-25 00:06:51 +00:00
|
|
|
tcx.at(span),
|
|
|
|
"reaching this expression at runtime will panic or abort",
|
2020-08-12 10:22:56 +00:00
|
|
|
tcx.hir().local_def_id_to_hir_id(def.did),
|
2019-12-25 00:06:51 +00:00
|
|
|
Some(err.span),
|
2020-08-10 10:04:01 +00:00
|
|
|
))
|
2019-12-25 00:06:51 +00:00
|
|
|
}
|
2019-12-23 16:31:55 +00:00
|
|
|
// anything else (array lengths, enum initializers, constant patterns) are
|
|
|
|
// reported as hard errors
|
2019-12-25 00:06:51 +00:00
|
|
|
} else {
|
2020-08-10 10:04:01 +00:00
|
|
|
Err(err.report_as_error(
|
2020-06-11 07:53:38 +00:00
|
|
|
ecx.tcx.at(ecx.cur_span()),
|
|
|
|
"evaluation of constant value failed",
|
2020-08-10 10:04:01 +00:00
|
|
|
))
|
2019-12-25 00:06:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// use of broken constant from other crate
|
2020-08-10 10:04:01 +00:00
|
|
|
Err(err.report_as_error(ecx.tcx.at(ecx.cur_span()), "could not evaluate constant"))
|
2019-12-25 00:06:51 +00:00
|
|
|
}
|
2020-08-10 10:04:01 +00:00
|
|
|
}
|
|
|
|
Ok(mplace) => {
|
2020-07-30 15:58:39 +00:00
|
|
|
// Since evaluation had no errors, valiate the resulting constant:
|
|
|
|
let validation = try {
|
|
|
|
// FIXME do not validate promoteds until a decision on
|
2020-10-25 10:12:19 +00:00
|
|
|
// https://github.com/rust-lang/rust/issues/67465 and
|
|
|
|
// https://github.com/rust-lang/rust/issues/67534 is made.
|
|
|
|
// Promoteds can contain unexpected `UnsafeCell` and reference `static`s, but their
|
|
|
|
// otherwise restricted form ensures that this is still sound. We just lose the
|
|
|
|
// extra safety net of some of the dynamic checks. They can also contain invalid
|
|
|
|
// values, but since we do not usually check intermediate results of a computation
|
|
|
|
// for validity, it might be surprising to do that here.
|
2020-07-30 15:58:39 +00:00
|
|
|
if cid.promoted.is_none() {
|
|
|
|
let mut ref_tracking = RefTracking::new(mplace);
|
2020-10-24 18:49:17 +00:00
|
|
|
let mut inner = false;
|
2020-07-30 15:58:39 +00:00
|
|
|
while let Some((mplace, path)) = ref_tracking.todo.pop() {
|
2020-10-24 18:49:17 +00:00
|
|
|
let mode = match tcx.static_mutability(cid.instance.def_id()) {
|
|
|
|
Some(_) => CtfeValidationMode::Regular, // a `static`
|
|
|
|
None => CtfeValidationMode::Const { inner },
|
|
|
|
};
|
|
|
|
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 {
|
|
|
|
// Validation failed, report an error
|
|
|
|
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
|
|
|
}
|