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-05 14:33:42 +00:00
|
|
|
use rustc_middle::mir;
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2020-01-01 18:25:28 +00:00
|
|
|
use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
|
2017-12-12 16:14:49 +00:00
|
|
|
|
2020-07-01 09:41:38 +00:00
|
|
|
use crate::interpret::{
|
2022-04-05 14:33:42 +00:00
|
|
|
intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, InterpResult, 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;
|
2022-04-05 14:33:42 +00:00
|
|
|
mod valtrees;
|
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::*;
|
2022-04-05 14:33:42 +00:00
|
|
|
pub(crate) use valtrees::{const_to_valtree, valtree_to_const_value};
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-04-05 14:33:42 +00:00
|
|
|
#[instrument(skip(tcx), level = "debug")]
|
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
|
|
|
}
|