2018-08-24 14:39:25 +00:00
|
|
|
// Not in interpret to make sure we do not use private implementation details
|
|
|
|
|
2020-03-21 16:17:01 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2020-07-01 09:41:38 +00:00
|
|
|
use rustc_hir::Mutability;
|
2022-04-14 19:56:27 +00:00
|
|
|
use rustc_middle::ty::layout::HasTyCtxt;
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2021-02-22 14:09:24 +00:00
|
|
|
use rustc_middle::{
|
|
|
|
mir::{self, interpret::ConstAlloc},
|
2022-04-08 11:09:24 +00:00
|
|
|
ty::ScalarInt,
|
2021-02-22 14:09:24 +00:00
|
|
|
};
|
2020-01-01 18:25:28 +00:00
|
|
|
use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
|
2022-03-29 09:38:08 +00:00
|
|
|
use rustc_target::abi::VariantIdx;
|
2017-12-12 16:14:49 +00:00
|
|
|
|
2020-07-01 09:41:38 +00:00
|
|
|
use crate::interpret::{
|
2022-04-12 14:08:59 +00:00
|
|
|
intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, InterpResult, MPlaceTy,
|
|
|
|
MemPlaceMeta, Scalar,
|
2020-07-01 09:41:38 +00:00
|
|
|
};
|
2017-12-12 16:14:49 +00:00
|
|
|
|
2019-12-25 00:04:32 +00:00
|
|
|
mod error;
|
2019-12-23 11:55:16 +00:00
|
|
|
mod eval_queries;
|
2020-01-01 17:06:00 +00:00
|
|
|
mod fn_queries;
|
2019-12-25 00:28:30 +00:00
|
|
|
mod machine;
|
2019-12-25 00:04:32 +00:00
|
|
|
|
|
|
|
pub use error::*;
|
2019-12-23 11:55:16 +00:00
|
|
|
pub use eval_queries::*;
|
2020-01-01 17:06:00 +00:00
|
|
|
pub use fn_queries::*;
|
2019-12-25 00:28:30 +00:00
|
|
|
pub use machine::*;
|
2018-09-20 09:57:45 +00:00
|
|
|
|
2020-03-06 23:56:32 +00:00
|
|
|
pub(crate) fn const_caller_location(
|
2021-12-14 03:34:51 +00:00
|
|
|
tcx: TyCtxt<'_>,
|
2019-10-09 15:25:41 +00:00
|
|
|
(file, line, col): (Symbol, u32, u32),
|
2021-12-14 03:34:51 +00:00
|
|
|
) -> ConstValue<'_> {
|
2019-10-09 15:25:41 +00:00
|
|
|
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-11-29 10:29:30 +00:00
|
|
|
let loc_place = ecx.alloc_caller_location(file, line, col);
|
2021-02-15 00:00:00 +00:00
|
|
|
if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() {
|
2020-11-04 16:53:43 +00:00
|
|
|
bug!("intern_const_alloc_recursive should not error in this case")
|
|
|
|
}
|
2022-04-18 16:47:38 +00:00
|
|
|
ConstValue::Scalar(Scalar::from_maybe_pointer(loc_place.ptr, &tcx))
|
2019-10-09 15:25:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 14:09:24 +00:00
|
|
|
/// Convert an evaluated constant to a type level constant
|
|
|
|
pub(crate) fn const_to_valtree<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
raw: ConstAlloc<'tcx>,
|
2021-02-22 14:34:23 +00:00
|
|
|
) -> Option<ty::ValTree<'tcx>> {
|
2021-03-15 11:52:00 +00:00
|
|
|
let ecx = mk_eval_cx(
|
|
|
|
tcx, DUMMY_SP, param_env,
|
|
|
|
// It is absolutely crucial for soundness that
|
|
|
|
// we do not read from static items or other mutable memory.
|
|
|
|
false,
|
|
|
|
);
|
2021-02-22 14:09:24 +00:00
|
|
|
let place = ecx.raw_const_to_mplace(raw).unwrap();
|
|
|
|
const_to_valtree_inner(&ecx, &place)
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:38:08 +00:00
|
|
|
#[instrument(skip(ecx), level = "debug")]
|
|
|
|
fn branches<'tcx>(
|
|
|
|
ecx: &CompileTimeEvalContext<'tcx, 'tcx>,
|
|
|
|
place: &MPlaceTy<'tcx>,
|
|
|
|
n: usize,
|
|
|
|
variant: Option<VariantIdx>,
|
|
|
|
) -> Option<ty::ValTree<'tcx>> {
|
|
|
|
let place = match variant {
|
|
|
|
Some(variant) => ecx.mplace_downcast(&place, variant).unwrap(),
|
|
|
|
None => *place,
|
|
|
|
};
|
|
|
|
let variant = variant.map(|variant| Some(ty::ValTree::Leaf(ScalarInt::from(variant.as_u32()))));
|
|
|
|
debug!(?place, ?variant);
|
|
|
|
|
|
|
|
let fields = (0..n).map(|i| {
|
|
|
|
let field = ecx.mplace_field(&place, i).unwrap();
|
|
|
|
const_to_valtree_inner(ecx, &field)
|
|
|
|
});
|
2022-04-14 19:56:27 +00:00
|
|
|
// For enums, we prepend their variant index before the variant's fields so we can figure out
|
2022-03-29 09:38:08 +00:00
|
|
|
// the variant again when just seeing a valtree.
|
|
|
|
let branches = variant.into_iter().chain(fields);
|
|
|
|
Some(ty::ValTree::Branch(ecx.tcx.arena.alloc_from_iter(branches.collect::<Option<Vec<_>>>()?)))
|
|
|
|
}
|
|
|
|
|
2022-04-08 11:09:24 +00:00
|
|
|
fn slice_branches<'tcx>(
|
|
|
|
ecx: &CompileTimeEvalContext<'tcx, 'tcx>,
|
|
|
|
place: &MPlaceTy<'tcx>,
|
|
|
|
) -> Option<ty::ValTree<'tcx>> {
|
2022-04-14 19:56:27 +00:00
|
|
|
let n = place.len(&ecx.tcx()).expect(&format!("expected to use len of place {:?}", place));
|
|
|
|
let branches = (0..n).map(|i| {
|
2022-04-08 11:09:24 +00:00
|
|
|
let place_elem = ecx.mplace_index(place, i).unwrap();
|
|
|
|
const_to_valtree_inner(ecx, &place_elem)
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(ty::ValTree::Branch(ecx.tcx.arena.alloc_from_iter(branches.collect::<Option<Vec<_>>>()?)))
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:38:08 +00:00
|
|
|
#[instrument(skip(ecx), level = "debug")]
|
2021-02-22 14:09:24 +00:00
|
|
|
fn const_to_valtree_inner<'tcx>(
|
|
|
|
ecx: &CompileTimeEvalContext<'tcx, 'tcx>,
|
|
|
|
place: &MPlaceTy<'tcx>,
|
2021-02-22 14:34:23 +00:00
|
|
|
) -> Option<ty::ValTree<'tcx>> {
|
2021-02-22 14:09:24 +00:00
|
|
|
match place.layout.ty.kind() {
|
|
|
|
ty::FnDef(..) => Some(ty::ValTree::zst()),
|
|
|
|
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => {
|
|
|
|
let val = ecx.read_immediate(&place.into()).unwrap();
|
|
|
|
let val = val.to_scalar().unwrap();
|
|
|
|
Some(ty::ValTree::Leaf(val.assert_int()))
|
|
|
|
}
|
|
|
|
|
2021-03-16 17:26:09 +00:00
|
|
|
// Raw pointers are not allowed in type level constants, as we cannot properly test them for
|
|
|
|
// equality at compile-time (see `ptr_guaranteed_eq`/`_ne`).
|
|
|
|
// Technically we could allow function pointers (represented as `ty::Instance`), but this is not guaranteed to
|
|
|
|
// agree with runtime equality tests.
|
2021-02-22 14:09:24 +00:00
|
|
|
ty::FnPtr(_) | ty::RawPtr(_) => None,
|
2022-03-29 09:38:08 +00:00
|
|
|
|
2022-04-14 19:56:27 +00:00
|
|
|
ty::Ref(_, _, _) => {
|
|
|
|
let derefd_place = ecx.deref_operand(&place.into()).unwrap_or_else(|e| bug!("couldn't deref {:?}, error: {:?}", place, e));
|
|
|
|
debug!(?derefd_place);
|
2022-04-08 11:09:24 +00:00
|
|
|
|
2022-04-14 19:56:27 +00:00
|
|
|
const_to_valtree_inner(ecx, &derefd_place)
|
2022-03-29 09:38:08 +00:00
|
|
|
}
|
2021-02-22 14:09:24 +00:00
|
|
|
|
2022-04-14 19:56:27 +00:00
|
|
|
ty::Str | ty::Slice(_) | ty::Array(_, _) => {
|
|
|
|
let valtree = slice_branches(ecx, place);
|
|
|
|
debug!(?valtree);
|
|
|
|
|
|
|
|
valtree
|
|
|
|
}
|
2021-03-12 13:00:16 +00:00
|
|
|
// Trait objects are not allowed in type level constants, as we have no concept for
|
2021-03-15 11:53:37 +00:00
|
|
|
// resolving their backing type, even if we can do that at const eval time. We may
|
|
|
|
// hypothetically be able to allow `dyn StructuralEq` trait objects in the future,
|
|
|
|
// but it is unclear if this is useful.
|
2021-03-12 13:00:16 +00:00
|
|
|
ty::Dynamic(..) => None,
|
2021-02-22 14:09:24 +00:00
|
|
|
|
2022-03-29 09:38:08 +00:00
|
|
|
ty::Tuple(substs) => branches(ecx, place, substs.len(), None),
|
2021-02-22 14:09:24 +00:00
|
|
|
|
|
|
|
ty::Adt(def, _) => {
|
2022-03-04 20:28:41 +00:00
|
|
|
if def.variants().is_empty() {
|
2021-03-15 11:53:37 +00:00
|
|
|
bug!("uninhabited types should have errored and never gotten converted to valtree")
|
2021-02-22 14:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let variant = ecx.read_discriminant(&place.into()).unwrap().1;
|
|
|
|
|
2022-03-29 09:38:08 +00:00
|
|
|
branches(ecx, place, def.variant(variant).fields.len(), def.is_enum().then_some(variant))
|
2021-02-22 14:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::Never
|
|
|
|
| ty::Error(_)
|
|
|
|
| ty::Foreign(..)
|
|
|
|
| ty::Infer(ty::FreshIntTy(_))
|
|
|
|
| ty::Infer(ty::FreshFloatTy(_))
|
|
|
|
| ty::Projection(..)
|
|
|
|
| ty::Param(_)
|
|
|
|
| ty::Bound(..)
|
|
|
|
| ty::Placeholder(..)
|
|
|
|
// FIXME(oli-obk): we could look behind opaque types
|
|
|
|
| ty::Opaque(..)
|
|
|
|
| ty::Infer(_)
|
|
|
|
// FIXME(oli-obk): we can probably encode closures just like structs
|
|
|
|
| ty::Closure(..)
|
|
|
|
| ty::Generator(..)
|
|
|
|
| ty::GeneratorWitness(..) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:58:34 +00:00
|
|
|
/// This function should never fail for validated constants. However, it is also invoked from the
|
|
|
|
/// pretty printer which might attempt to format invalid constants and in that case it might fail.
|
|
|
|
pub(crate) fn try_destructure_const<'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>,
|
2022-02-02 03:24:45 +00:00
|
|
|
val: ty::Const<'tcx>,
|
2022-02-15 12:58:34 +00:00
|
|
|
) -> InterpResult<'tcx, mir::DestructuredConst<'tcx>> {
|
2020-01-05 15:46:44 +00:00
|
|
|
trace!("destructure_const: {:?}", val);
|
2019-12-16 14:23:42 +00:00
|
|
|
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
|
2022-02-15 12:58:34 +00:00
|
|
|
let op = ecx.const_to_op(val, None)?;
|
2020-01-05 15:46:44 +00:00
|
|
|
|
2020-03-21 16:17:01 +00:00
|
|
|
// We go to `usize` as we cannot allocate anything bigger anyway.
|
2022-02-02 03:24:45 +00:00
|
|
|
let (field_count, variant, down) = match val.ty().kind() {
|
2020-06-18 09:37:59 +00:00
|
|
|
ty::Array(_, len) => (usize::try_from(len.eval_usize(tcx, param_env)).unwrap(), None, op),
|
2022-02-17 00:00:00 +00:00
|
|
|
// Checks if we have any variants, to avoid downcasting to a non-existing variant (when
|
|
|
|
// there are no variants `read_discriminant` successfully returns a non-existing variant
|
|
|
|
// index).
|
2022-03-04 20:28:41 +00:00
|
|
|
ty::Adt(def, _) if def.variants().is_empty() => throw_ub!(Unreachable),
|
2020-06-18 09:37:59 +00:00
|
|
|
ty::Adt(def, _) => {
|
2022-02-15 12:58:34 +00:00
|
|
|
let variant = ecx.read_discriminant(&op)?.1;
|
|
|
|
let down = ecx.operand_downcast(&op, variant)?;
|
2022-03-04 20:28:41 +00:00
|
|
|
(def.variant(variant).fields.len(), Some(variant), down)
|
2020-06-18 09:37:59 +00:00
|
|
|
}
|
|
|
|
ty::Tuple(substs) => (substs.len(), None, op),
|
2020-01-05 15:46:44 +00:00
|
|
|
_ => bug!("cannot destructure constant {:?}", val),
|
|
|
|
};
|
|
|
|
|
2022-02-15 12:58:34 +00:00
|
|
|
let fields = (0..field_count)
|
|
|
|
.map(|i| {
|
|
|
|
let field_op = ecx.operand_field(&down, i)?;
|
|
|
|
let val = op_to_const(&ecx, &field_op);
|
|
|
|
Ok(ty::Const::from_value(tcx, val, field_op.layout.ty))
|
|
|
|
})
|
|
|
|
.collect::<InterpResult<'tcx, Vec<_>>>()?;
|
|
|
|
let fields = tcx.arena.alloc_from_iter(fields);
|
2020-01-05 15:46:44 +00:00
|
|
|
|
2022-02-15 12:58:34 +00:00
|
|
|
Ok(mir::DestructuredConst { variant, fields })
|
2018-01-16 08:24:38 +00:00
|
|
|
}
|
2020-07-01 09:41:38 +00:00
|
|
|
|
|
|
|
pub(crate) fn deref_const<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2022-02-02 03:24:45 +00:00
|
|
|
val: ty::Const<'tcx>,
|
|
|
|
) -> ty::Const<'tcx> {
|
2020-07-01 09:41:38 +00:00
|
|
|
trace!("deref_const: {:?}", val);
|
|
|
|
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
|
|
|
|
let op = ecx.const_to_op(val, None).unwrap();
|
2021-02-15 00:00:00 +00:00
|
|
|
let mplace = ecx.deref_operand(&op).unwrap();
|
2021-07-12 16:22:15 +00:00
|
|
|
if let Some(alloc_id) = mplace.ptr.provenance {
|
2020-07-01 09:41:38 +00:00
|
|
|
assert_eq!(
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-01 20:15:04 +00:00
|
|
|
tcx.get_global_alloc(alloc_id).unwrap().unwrap_memory().inner().mutability,
|
2020-07-01 09:41:38 +00:00
|
|
|
Mutability::Not,
|
|
|
|
"deref_const cannot be used with mutable allocations as \
|
|
|
|
that could allow pattern matching to observe mutable statics",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let ty = match mplace.meta {
|
|
|
|
MemPlaceMeta::None => mplace.layout.ty,
|
|
|
|
MemPlaceMeta::Poison => bug!("poison metadata in `deref_const`: {:#?}", mplace),
|
|
|
|
// In case of unsized types, figure out the real type behind.
|
2020-07-01 13:10:51 +00:00
|
|
|
MemPlaceMeta::Meta(scalar) => match mplace.layout.ty.kind() {
|
2020-07-01 09:41:38 +00:00
|
|
|
ty::Str => bug!("there's no sized equivalent of a `str`"),
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 03:13:38 +00:00
|
|
|
ty::Slice(elem_ty) => tcx.mk_array(*elem_ty, scalar.to_machine_usize(&tcx).unwrap()),
|
2020-07-01 09:41:38 +00:00
|
|
|
_ => bug!(
|
|
|
|
"type {} should not have metadata, but had {:?}",
|
|
|
|
mplace.layout.ty,
|
|
|
|
mplace.meta
|
|
|
|
),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-02-02 03:24:45 +00:00
|
|
|
tcx.mk_const(ty::ConstS { val: ty::ConstKind::Value(op_to_const(&ecx, &mplace.into())), ty })
|
2020-07-01 09:41:38 +00:00
|
|
|
}
|