2018-08-24 14:39:25 +00:00
|
|
|
// Not in interpret to make sure we do not use private implementation details
|
|
|
|
|
2019-04-11 13:06:42 +00:00
|
|
|
use std::convert::TryInto;
|
2019-12-24 22:38:22 +00:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt;
|
|
|
|
use std::hash::Hash;
|
2018-06-08 02:47:26 +00:00
|
|
|
|
2018-01-02 23:22:09 +00:00
|
|
|
use rustc::mir;
|
2019-12-25 00:09:47 +00:00
|
|
|
use rustc::ty::layout::{self, VariantIdx};
|
|
|
|
use rustc::ty::{self, TyCtxt};
|
2017-12-12 16:14:49 +00:00
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
use syntax::{
|
|
|
|
source_map::{Span, DUMMY_SP},
|
|
|
|
symbol::Symbol,
|
|
|
|
};
|
2017-12-12 16:14:49 +00:00
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
use crate::interpret::{
|
2019-12-25 00:09:47 +00:00
|
|
|
intern_const_alloc_recursive, Allocation, ConstValue, ImmTy, Immediate, InterpCx, OpTy, Scalar,
|
2018-04-26 07:18:19 +00:00
|
|
|
};
|
2017-12-12 16:14:49 +00:00
|
|
|
|
2019-12-25 00:04:32 +00:00
|
|
|
mod error;
|
2019-12-25 00:06:51 +00:00
|
|
|
mod query;
|
2019-12-25 00:04:32 +00:00
|
|
|
|
|
|
|
pub use error::*;
|
2019-12-25 00:06:51 +00:00
|
|
|
pub use query::*;
|
2018-09-20 09:57:45 +00:00
|
|
|
|
2019-06-27 09:36:01 +00:00
|
|
|
/// The `InterpCx` is only meant to be used to do field and index projections into constants for
|
2019-01-09 15:08:07 +00:00
|
|
|
/// `simd_shuffle` and const patterns in match arms.
|
2019-01-09 15:27:33 +00:00
|
|
|
///
|
|
|
|
/// The function containing the `match` that is currently being analyzed may have generic bounds
|
2019-02-08 13:53:55 +00:00
|
|
|
/// that inform us about the generic bounds of the constant. E.g., using an associated constant
|
2019-01-09 15:27:33 +00:00
|
|
|
/// of a function's generic parameter will require knowledge about the bounds on the generic
|
2019-01-09 15:34:31 +00:00
|
|
|
/// parameter. These bounds are passed to `mk_eval_cx` via the `ParamEnv` argument.
|
2019-09-25 01:12:59 +00:00
|
|
|
fn mk_eval_cx<'mir, 'tcx>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-01-14 18:50:49 +00:00
|
|
|
span: Span,
|
2017-12-15 07:55:54 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2019-12-16 14:23:42 +00:00
|
|
|
can_access_statics: bool,
|
2019-06-11 19:03:44 +00:00
|
|
|
) -> CompileTimeEvalContext<'mir, 'tcx> {
|
2019-01-14 16:54:00 +00:00
|
|
|
debug!("mk_eval_cx: {:?}", param_env);
|
2019-12-16 14:23:42 +00:00
|
|
|
InterpCx::new(
|
|
|
|
tcx.at(span),
|
|
|
|
param_env,
|
|
|
|
CompileTimeInterpreter::new(),
|
|
|
|
MemoryExtra { can_access_statics },
|
|
|
|
)
|
2017-12-15 07:55:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 13:51:53 +00:00
|
|
|
fn op_to_const<'tcx>(
|
2019-06-11 19:03:44 +00:00
|
|
|
ecx: &CompileTimeEvalContext<'_, 'tcx>,
|
2018-08-13 14:14:22 +00:00
|
|
|
op: OpTy<'tcx>,
|
2019-04-03 13:29:31 +00:00
|
|
|
) -> &'tcx ty::Const<'tcx> {
|
2019-11-27 03:19:54 +00:00
|
|
|
// We do not have value optimizations for everything.
|
2019-02-10 13:59:13 +00:00
|
|
|
// Only scalars and slices, since they are very common.
|
|
|
|
// Note that further down we turn scalars of undefined bits back to `ByRef`. These can result
|
|
|
|
// from scalar unions that are initialized with one of their zero sized variants. We could
|
|
|
|
// instead allow `ConstValue::Scalar` to store `ScalarMaybeUndef`, but that would affect all
|
|
|
|
// 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 {
|
|
|
|
layout::Abi::Scalar(..) => true,
|
2019-09-16 18:08:35 +00:00
|
|
|
layout::Abi::ScalarPair(..) => match op.layout.ty.kind {
|
|
|
|
ty::Ref(_, inner, _) => match inner.kind {
|
2019-04-11 13:06:42 +00:00
|
|
|
ty::Slice(elem) => elem == ecx.tcx.types.u8,
|
|
|
|
ty::Str => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
},
|
2019-02-16 13:51:53 +00:00
|
|
|
_ => false,
|
|
|
|
};
|
2019-02-10 13:59:13 +00:00
|
|
|
let immediate = if try_as_immediate {
|
|
|
|
Err(ecx.read_immediate(op).expect("normalization works on validated constants"))
|
2018-08-13 14:14:22 +00:00
|
|
|
} else {
|
2019-02-10 13:59:13 +00:00
|
|
|
// 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
|
|
|
|
// by-val is if we are in const_field, i.e., if this is (a field of) something that we
|
|
|
|
// "tried to make immediate" before. We wouldn't do that for non-slice scalar pairs or
|
|
|
|
// structs containing such.
|
2019-02-16 13:51:53 +00:00
|
|
|
op.try_as_mplace()
|
2018-08-13 14:14:22 +00:00
|
|
|
};
|
2019-02-10 13:59:13 +00:00
|
|
|
let val = match immediate {
|
|
|
|
Ok(mplace) => {
|
|
|
|
let ptr = mplace.ptr.to_ptr().unwrap();
|
|
|
|
let alloc = ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id);
|
2019-07-28 11:44:11 +00:00
|
|
|
ConstValue::ByRef { alloc, offset: ptr.offset }
|
2019-12-24 22:38:22 +00:00
|
|
|
}
|
2019-02-10 13:59:13 +00:00
|
|
|
// see comment on `let try_as_immediate` above
|
|
|
|
Err(ImmTy { imm: Immediate::Scalar(x), .. }) => match x {
|
|
|
|
ScalarMaybeUndef::Scalar(s) => ConstValue::Scalar(s),
|
|
|
|
ScalarMaybeUndef::Undef => {
|
|
|
|
// When coming out of "normal CTFE", we'll always have an `Indirect` operand as
|
|
|
|
// argument and we will not need this. The only way we can already have an
|
|
|
|
// `Immediate` is when we are called from `const_field`, and that `Immediate`
|
|
|
|
// comes from a constant so it can happen have `Undef`, because the indirect
|
|
|
|
// memory that was read had undefined bytes.
|
2019-07-06 10:46:08 +00:00
|
|
|
let mplace = op.assert_mem_place();
|
2019-02-10 13:59:13 +00:00
|
|
|
let ptr = mplace.ptr.to_ptr().unwrap();
|
|
|
|
let alloc = ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id);
|
2019-07-28 11:44:11 +00:00
|
|
|
ConstValue::ByRef { alloc, offset: ptr.offset }
|
2019-12-24 22:38:22 +00:00
|
|
|
}
|
2019-02-10 13:59:13 +00:00
|
|
|
},
|
|
|
|
Err(ImmTy { imm: Immediate::ScalarPair(a, b), .. }) => {
|
2019-04-11 13:06:42 +00:00
|
|
|
let (data, start) = match a.not_undef().unwrap() {
|
2019-12-24 22:38:22 +00:00
|
|
|
Scalar::Ptr(ptr) => {
|
|
|
|
(ecx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id), ptr.offset.bytes())
|
|
|
|
}
|
2019-05-25 08:59:09 +00:00
|
|
|
Scalar::Raw { .. } => (
|
2019-12-24 22:38:22 +00:00
|
|
|
ecx.tcx.intern_const_alloc(Allocation::from_byte_aligned_bytes(b"" as &[u8])),
|
2019-04-11 13:06:42 +00:00
|
|
|
0,
|
|
|
|
),
|
|
|
|
};
|
2019-11-06 12:00:14 +00:00
|
|
|
let len = b.to_machine_usize(&ecx.tcx.tcx).unwrap();
|
2019-04-11 13:06:42 +00:00
|
|
|
let start = start.try_into().unwrap();
|
|
|
|
let len: usize = len.try_into().unwrap();
|
2019-12-24 22:38:22 +00:00
|
|
|
ConstValue::Slice { data, start, end: start + len }
|
|
|
|
}
|
2018-08-01 13:29:13 +00:00
|
|
|
};
|
2019-11-08 22:11:51 +00:00
|
|
|
ecx.tcx.mk_const(ty::Const { val: ty::ConstKind::Value(val), ty: op.layout.ty })
|
2018-08-13 14:14:22 +00:00
|
|
|
}
|
2018-12-11 18:56:59 +00:00
|
|
|
|
2019-05-24 19:58:37 +00:00
|
|
|
/// Extracts a field of a (variant of a) const.
|
2019-02-16 19:51:43 +00:00
|
|
|
// this function uses `unwrap` copiously, because an already validated constant must have valid
|
|
|
|
// fields and can thus never fail outside of compiler bugs
|
2019-12-15 16:50:37 +00:00
|
|
|
pub(crate) fn const_field<'tcx>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-12-27 20:32:01 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2018-11-01 18:03:38 +00:00
|
|
|
variant: Option<VariantIdx>,
|
2017-12-27 20:32:01 +00:00
|
|
|
field: mir::Field,
|
2019-04-03 13:29:31 +00:00
|
|
|
value: &'tcx ty::Const<'tcx>,
|
|
|
|
) -> &'tcx ty::Const<'tcx> {
|
2019-01-14 16:54:00 +00:00
|
|
|
trace!("const_field: {:?}, {:?}", field, value);
|
2019-12-16 14:23:42 +00:00
|
|
|
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
|
2019-02-16 19:51:43 +00:00
|
|
|
// get the operand again
|
2019-03-14 09:19:31 +00:00
|
|
|
let op = ecx.eval_const_to_op(value, None).unwrap();
|
2019-02-16 19:51:43 +00:00
|
|
|
// downcast
|
|
|
|
let down = match variant {
|
|
|
|
None => op,
|
|
|
|
Some(variant) => ecx.operand_downcast(op, variant).unwrap(),
|
|
|
|
};
|
|
|
|
// then project
|
|
|
|
let field = ecx.operand_field(down, field.index() as u64).unwrap();
|
|
|
|
// and finally move back to the const world, always normalizing because
|
|
|
|
// this is not called for statics.
|
2019-03-12 16:37:22 +00:00
|
|
|
op_to_const(&ecx, field)
|
2018-01-16 08:24:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 16:50:37 +00:00
|
|
|
pub(crate) fn const_caller_location<'tcx>(
|
2019-10-09 15:25:41 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
(file, line, col): (Symbol, u32, u32),
|
|
|
|
) -> &'tcx ty::Const<'tcx> {
|
|
|
|
trace!("const_caller_location: {}:{}:{}", file, line, col);
|
2019-12-16 14:23:42 +00:00
|
|
|
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), false);
|
2019-10-09 15:25:41 +00:00
|
|
|
|
2019-10-29 03:02:41 +00:00
|
|
|
let loc_ty = tcx.caller_location_ty();
|
2019-11-29 10:29:30 +00:00
|
|
|
let loc_place = ecx.alloc_caller_location(file, line, col);
|
2019-10-09 15:25:41 +00:00
|
|
|
intern_const_alloc_recursive(&mut ecx, None, loc_place).unwrap();
|
|
|
|
let loc_const = ty::Const {
|
|
|
|
ty: loc_ty,
|
2019-11-08 22:11:51 +00:00
|
|
|
val: ty::ConstKind::Value(ConstValue::Scalar(loc_place.ptr.into())),
|
2019-10-09 15:25:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
tcx.mk_const(loc_const)
|
|
|
|
}
|
|
|
|
|
2019-02-16 19:51:43 +00:00
|
|
|
// this function uses `unwrap` copiously, because an already validated constant must have valid
|
|
|
|
// fields and can thus never fail outside of compiler bugs
|
2019-12-15 16:50:37 +00:00
|
|
|
pub(crate) fn const_variant_index<'tcx>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-12-27 20:32:01 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2019-04-03 13:29:31 +00:00
|
|
|
val: &'tcx ty::Const<'tcx>,
|
2019-02-16 19:51:43 +00:00
|
|
|
) -> VariantIdx {
|
2019-01-14 16:54:00 +00:00
|
|
|
trace!("const_variant_index: {:?}", val);
|
2019-12-16 14:23:42 +00:00
|
|
|
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
|
2019-03-14 09:19:31 +00:00
|
|
|
let op = ecx.eval_const_to_op(val, None).unwrap();
|
2019-02-16 19:51:43 +00:00
|
|
|
ecx.read_discriminant(op).unwrap().1
|
2018-01-16 08:24:38 +00:00
|
|
|
}
|