Get rid of const eval_* and try_eval_* helpers

This commit is contained in:
Michael Goulet 2024-09-27 12:56:51 -04:00
parent a2a1206811
commit e83e4e8112
31 changed files with 119 additions and 172 deletions

View File

@ -187,10 +187,10 @@ fn do_mir_borrowck<'tcx>(
let location_table = LocationTable::new(body);
let move_data = MoveData::gather_moves(body, tcx, param_env, |_| true);
let move_data = MoveData::gather_moves(body, tcx, |_| true);
let promoted_move_data = promoted
.iter_enumerated()
.map(|(idx, body)| (idx, MoveData::gather_moves(body, tcx, param_env, |_| true)));
.map(|(idx, body)| (idx, MoveData::gather_moves(body, tcx, |_| true)));
let mut flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
.into_engine(tcx, body)

View File

@ -1128,7 +1128,6 @@ fn relate_type_and_user_type(
}
let projected_ty = curr_projected_ty.projection_ty_core(
tcx,
self.param_env,
proj,
|this, field, ()| {
let ty = this.field_ty(tcx, field);
@ -1919,7 +1918,7 @@ fn check_rvalue(&mut self, body: &Body<'tcx>, rvalue: &Rvalue<'tcx>, location: L
// than 1.
// If the length is larger than 1, the repeat expression will need to copy the
// element, so we require the `Copy` trait.
if len.try_eval_target_usize(tcx, self.param_env).map_or(true, |len| len > 1) {
if len.try_to_target_usize(tcx).is_none_or(|len| len > 1) {
match operand {
Operand::Copy(..) | Operand::Constant(..) => {
// These are always okay: direct use of a const, or a value that can evidently be copied.

View File

@ -1177,8 +1177,10 @@ macro_rules! require_simd {
ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => args[0].immediate(),
ty::Array(elem, len)
if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
&& len.try_eval_target_usize(bx.tcx, ty::ParamEnv::reveal_all())
== Some(expected_bytes) =>
&& len
.try_to_target_usize(bx.tcx)
.expect("expected monomorphic const in codegen")
== expected_bytes =>
{
let place = PlaceRef::alloca(bx, args[0].layout);
args[0].val.store(bx, place);
@ -1243,12 +1245,7 @@ macro_rules! require_simd {
}
if name == sym::simd_shuffle_generic {
let idx = fn_args[2]
.expect_const()
.eval(tcx, ty::ParamEnv::reveal_all(), span)
.unwrap()
.1
.unwrap_branch();
let idx = fn_args[2].expect_const().try_to_valtree().unwrap().0.unwrap_branch();
let n = idx.len() as u64;
let (out_len, out_ty) = require_simd!(ret_ty, SimdReturn);
@ -1467,8 +1464,10 @@ macro_rules! require_simd {
}
ty::Array(elem, len)
if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
&& len.try_eval_target_usize(bx.tcx, ty::ParamEnv::reveal_all())
== Some(expected_bytes) =>
&& len
.try_to_target_usize(bx.tcx)
.expect("expected monomorphic const in codegen")
== expected_bytes =>
{
// Zero-extend iN to the array length:
let ze = bx.zext(i_, bx.type_ix(expected_bytes * 8));

View File

@ -23,7 +23,6 @@
use rustc_middle::ty::{
self, ExistentialProjection, GenericArgKind, GenericArgsRef, ParamEnv, Ty, TyCtxt,
};
use rustc_span::DUMMY_SP;
use rustc_target::abi::Integer;
use smallvec::SmallVec;
@ -685,21 +684,25 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: ty::Const<'tcx>, output: &mut S
ty::ConstKind::Param(param) => {
write!(output, "{}", param.name)
}
ty::ConstKind::Value(ty, _) => {
ty::ConstKind::Value(ty, valtree) => {
match ty.kind() {
ty::Int(ity) => {
// FIXME: directly extract the bits from a valtree instead of evaluating an
// already evaluated `Const` in order to get the bits.
let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all());
let bits = ct
.try_to_bits(tcx, ty::ParamEnv::reveal_all())
.expect("expected monomorphic const in codegen");
let val = Integer::from_int_ty(&tcx, *ity).size().sign_extend(bits) as i128;
write!(output, "{val}")
}
ty::Uint(_) => {
let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all());
let val = ct
.try_to_bits(tcx, ty::ParamEnv::reveal_all())
.expect("expected monomorphic const in codegen");
write!(output, "{val}")
}
ty::Bool => {
let val = ct.try_eval_bool(tcx, ty::ParamEnv::reveal_all()).unwrap();
let val = ct.try_to_bool().expect("expected monomorphic const in codegen");
write!(output, "{val}")
}
_ => {
@ -711,8 +714,9 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: ty::Const<'tcx>, output: &mut S
// avoiding collisions and will make the emitted type names shorter.
let hash_short = tcx.with_stable_hashing_context(|mut hcx| {
let mut hasher = StableHasher::new();
let ct = ct.eval(tcx, ty::ParamEnv::reveal_all(), DUMMY_SP).unwrap();
hcx.while_hashing_spans(false, |hcx| ct.hash_stable(hcx, &mut hasher));
hcx.while_hashing_spans(false, |hcx| {
(ty, valtree).hash_stable(hcx, &mut hasher)
});
hasher.finish::<Hash64>()
});

View File

@ -43,7 +43,7 @@ pub(crate) fn try_destructure_mir_constant_for_user_output<'tcx>(
// We go to `usize` as we cannot allocate anything bigger anyway.
let (field_count, variant, down) = match ty.kind() {
ty::Array(_, len) => (len.eval_target_usize(tcx.tcx, param_env) as usize, None, op),
ty::Array(_, len) => (len.try_to_target_usize(tcx.tcx)? as usize, None, op),
ty::Adt(def, _) if def.variants().is_empty() => {
return None;
}

View File

@ -391,7 +391,9 @@ fn unsize_into_ptr(
let ptr = self.read_pointer(src)?;
let val = Immediate::new_slice(
ptr,
length.eval_target_usize(*self.tcx, self.param_env),
length
.try_to_target_usize(*self.tcx)
.expect("expected monomorphic const in const eval"),
self,
);
self.write_immediate(val, dest)

View File

@ -1037,7 +1037,11 @@ fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
return;
}
if let Some(len) = len_const.try_eval_target_usize(tcx, tcx.param_env(def.did())) {
// FIXME(repr_simd): This check is nice, but perhaps unnecessary due to the fact
// we do not expect users to implement their own `repr(simd)` types. If they could,
// this check is easily side-steppable by hiding the const behind normalization.
// The consequence is that the error is, in general, only observable post-mono.
if let Some(len) = len_const.try_to_target_usize(tcx) {
if len == 0 {
struct_span_code_err!(tcx.dcx(), sp, E0075, "SIMD vector cannot be empty").emit();
return;

View File

@ -76,9 +76,7 @@ fn get_asm_ty(&self, ty: Ty<'tcx>) -> Option<InlineAsmType> {
let (size, ty) = match elem_ty.kind() {
ty::Array(ty, len) => {
if let Some(len) =
len.try_eval_target_usize(self.tcx, self.tcx.param_env(adt.did()))
{
if let Some(len) = len.try_to_target_usize(self.tcx) {
(len, *ty)
} else {
return None;

View File

@ -2601,7 +2601,7 @@ fn ty_find_init_error<'tcx>(
ty.tuple_fields().iter().find_map(|field| ty_find_init_error(cx, field, init))
}
Array(ty, len) => {
if matches!(len.try_eval_target_usize(cx.tcx, cx.param_env), Some(v) if v > 0) {
if matches!(len.try_to_target_usize(cx.tcx), Some(v) if v > 0) {
// Array length known at array non-empty -- recurse.
ty_find_init_error(cx, *ty, init)
} else {

View File

@ -346,7 +346,7 @@ fn is_ty_must_use<'tcx>(
None
}
}
ty::Array(ty, len) => match len.try_eval_target_usize(cx.tcx, cx.param_env) {
ty::Array(ty, len) => match len.try_to_target_usize(cx.tcx) {
// If the array is empty we don't lint, to avoid false positives
Some(0) | None => None,
// If the array is definitely non-empty, we can do `#[must_use]` checking.

View File

@ -6,6 +6,7 @@
use rustc_session::config::RemapPathScopeComponents;
use rustc_span::{DUMMY_SP, Span};
use rustc_target::abi::{HasDataLayout, Size};
use either::Either;
use crate::mir::interpret::{AllocId, ConstAllocation, ErrorHandled, Scalar, alloc_range};
use crate::mir::{Promoted, pretty_print_const_value};
@ -320,8 +321,14 @@ pub fn eval(
Const::Ty(_, c) => {
// We want to consistently have a "clean" value for type system constants (i.e., no
// data hidden in the padding), so we always go through a valtree here.
let (ty, val) = c.eval(tcx, param_env, span)?;
Ok(tcx.valtree_to_const_val((ty, val)))
match c.eval_valtree(tcx, param_env, span) {
Ok((ty, val)) => Ok(tcx.valtree_to_const_val((ty, val))),
Err(Either::Left(_bad_ty)) => Err(tcx
.dcx()
.delayed_bug("`mir::Const::eval` called on a non-valtree-compatible type")
.into()),
Err(Either::Right(e)) => Err(e),
}
}
Const::Unevaluated(uneval, _) => {
// FIXME: We might want to have a `try_eval`-like function on `Unevaluated`

View File

@ -55,7 +55,7 @@ pub fn field_ty(self, tcx: TyCtxt<'tcx>, f: FieldIdx) -> Ty<'tcx> {
/// `PlaceElem`, where we can just use the `Ty` that is already
/// stored inline on field projection elems.
pub fn projection_ty(self, tcx: TyCtxt<'tcx>, elem: PlaceElem<'tcx>) -> PlaceTy<'tcx> {
self.projection_ty_core(tcx, ty::ParamEnv::empty(), &elem, |_, _, ty| ty, |_, ty| ty)
self.projection_ty_core(tcx, &elem, |_, _, ty| ty, |_, ty| ty)
}
/// `place_ty.projection_ty_core(tcx, elem, |...| { ... })`
@ -66,7 +66,6 @@ pub fn projection_ty(self, tcx: TyCtxt<'tcx>, elem: PlaceElem<'tcx>) -> PlaceTy<
pub fn projection_ty_core<V, T>(
self,
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
elem: &ProjectionElem<V, T>,
mut handle_field: impl FnMut(&Self, FieldIdx, T) -> Ty<'tcx>,
mut handle_opaque_cast_and_subtype: impl FnMut(&Self, T) -> Ty<'tcx>,
@ -93,7 +92,9 @@ pub fn projection_ty_core<V, T>(
ty::Slice(..) => self.ty,
ty::Array(inner, _) if !from_end => Ty::new_array(tcx, *inner, to - from),
ty::Array(inner, size) if from_end => {
let size = size.eval_target_usize(tcx, param_env);
let size = size
.try_to_target_usize(tcx)
.expect("expected subslice projection on fixed-size array");
let len = size - from - to;
Ty::new_array(tcx, *inner, len)
}

View File

@ -398,133 +398,65 @@ pub fn eval_valtree(
}
}
/// Returns the evaluated constant
#[inline]
pub fn eval(
self,
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
span: Span,
) -> Result<(Ty<'tcx>, ValTree<'tcx>), ErrorHandled> {
self.eval_valtree(tcx, param_env, span).map_err(|err| {
match err {
Either::Right(err) => err,
Either::Left(_bad_ty) => {
// This can happen when we run on ill-typed code.
let e = tcx.dcx().span_delayed_bug(
span,
"`ty::Const::eval` called on a non-valtree-compatible type",
);
e.into()
}
}
})
}
/// Normalizes the constant to a value or an error if possible.
#[inline]
pub fn normalize(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Self {
match self.eval(tcx, param_env, DUMMY_SP) {
match self.eval_valtree(tcx, param_env, DUMMY_SP) {
Ok((ty, val)) => Self::new_value(tcx, val, ty),
Err(ErrorHandled::Reported(r, _span)) => Self::new_error(tcx, r.into()),
Err(ErrorHandled::TooGeneric(_span)) => self,
Err(Either::Left(_bad_ty)) => {
// This can happen when we run on ill-typed code.
Self::new_error(
tcx,
tcx.dcx()
.delayed_bug("`ty::Const::eval` called on a non-valtree-compatible type"),
)
}
Err(Either::Right(ErrorHandled::Reported(r, _span))) => Self::new_error(tcx, r.into()),
Err(Either::Right(ErrorHandled::TooGeneric(_span))) => self,
}
#[inline]
pub fn try_eval_scalar(
self,
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> Option<(Ty<'tcx>, Scalar)> {
let (ty, val) = self.eval(tcx, param_env, DUMMY_SP).ok()?;
let val = val.try_to_scalar()?;
Some((ty, val))
}
#[inline]
/// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
/// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
/// contains const generic parameters or pointers).
pub fn try_eval_scalar_int(
self,
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
) -> Option<(Ty<'tcx>, ScalarInt)> {
let (ty, scalar) = self.try_eval_scalar(tcx, param_env)?;
let val = scalar.try_to_scalar_int().ok()?;
Some((ty, val))
}
#[inline]
/// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
/// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
/// contains const generic parameters or pointers).
pub fn try_eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Option<u128> {
let (ty, scalar) = self.try_eval_scalar_int(tcx, param_env)?;
let size = tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
// if `ty` does not depend on generic parameters, use an empty param_env
Some(scalar.to_bits(size))
}
#[inline]
/// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
pub fn eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> u128 {
self.try_eval_bits(tcx, param_env)
.unwrap_or_else(|| bug!("failed to evalate {:#?} to bits", self))
}
#[inline]
pub fn try_eval_target_usize(
self,
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
) -> Option<u64> {
let (_, scalar) = self.try_eval_scalar_int(tcx, param_env)?;
Some(scalar.to_target_usize(tcx))
}
#[inline]
pub fn try_eval_bool(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Option<bool> {
let (_, scalar) = self.try_eval_scalar_int(tcx, param_env)?;
scalar.try_into().ok()
}
#[inline]
/// Panics if the value cannot be evaluated or doesn't contain a valid `usize`.
pub fn eval_target_usize(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> u64 {
self.try_eval_target_usize(tcx, param_env)
.unwrap_or_else(|| bug!("expected usize, got {:#?}", self))
}
/// Panics if self.kind != ty::ConstKind::Value
pub fn to_valtree(self) -> ty::ValTree<'tcx> {
pub fn to_valtree(self) -> (ty::ValTree<'tcx>, Ty<'tcx>) {
match self.kind() {
ty::ConstKind::Value(_, valtree) => valtree,
ty::ConstKind::Value(ty, valtree) => (valtree, ty),
_ => bug!("expected ConstKind::Value, got {:?}", self.kind()),
}
}
/// Attempts to convert to a `ValTree`
pub fn try_to_valtree(self) -> Option<ty::ValTree<'tcx>> {
pub fn try_to_valtree(self) -> Option<(ty::ValTree<'tcx>, Ty<'tcx>)> {
match self.kind() {
ty::ConstKind::Value(_, valtree) => Some(valtree),
ty::ConstKind::Value(ty, valtree) => Some((valtree, ty)),
_ => None,
}
}
#[inline]
pub fn try_to_scalar(self) -> Option<Scalar> {
self.try_to_valtree()?.try_to_scalar()
pub fn try_to_scalar(self) -> Option<(Scalar, Ty<'tcx>)> {
let (valtree, ty) = self.try_to_valtree()?;
Some((valtree.try_to_scalar()?, ty))
}
pub fn try_to_bool(self) -> Option<bool> {
self.try_to_valtree()?.try_to_scalar_int()?.try_to_bool().ok()
self.try_to_valtree()?.0.try_to_scalar_int()?.try_to_bool().ok()
}
#[inline]
pub fn try_to_target_usize(self, tcx: TyCtxt<'tcx>) -> Option<u64> {
self.try_to_valtree()?.try_to_target_usize(tcx)
self.try_to_valtree()?.0.try_to_target_usize(tcx)
}
#[inline]
/// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
/// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
/// contains const generic parameters or pointers).
pub fn try_to_bits(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Option<u128> {
let (scalar, ty) = self.try_to_scalar()?;
let scalar = scalar.try_to_scalar_int().ok()?;
let size = tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
// if `ty` does not depend on generic parameters, use an empty param_env
Some(scalar.to_bits(size))
}
pub fn is_ct_infer(self) -> bool {

View File

@ -85,7 +85,7 @@ fn apply_inner<E: std::fmt::Debug>(
match self {
Self::False => Ok(false),
Self::True => Ok(true),
Self::ConstIsZero(const_) => match const_.try_eval_target_usize(tcx, param_env) {
Self::ConstIsZero(const_) => match const_.try_to_target_usize(tcx) {
None | Some(0) => Ok(true),
Some(1..) => Ok(false),
},

View File

@ -397,7 +397,7 @@ pub fn compute(
}
}
ty::Array(inner, len) if tcx.features().transmute_generic_consts => {
let len_eval = len.try_eval_target_usize(tcx, param_env);
let len_eval = len.try_to_target_usize(tcx);
if len_eval == Some(0) {
return Ok(SizeSkeleton::Known(Size::from_bytes(0), None));
}

View File

@ -1117,7 +1117,12 @@ pub fn simd_size_and_type(self, tcx: TyCtxt<'tcx>) -> (u64, Ty<'tcx>) {
// The way we evaluate the `N` in `[T; N]` here only works since we use
// `simd_size_and_type` post-monomorphization. It will probably start to ICE
// if we use it in generic code. See the `simd-array-trait` ui test.
(f0_len.eval_target_usize(tcx, ParamEnv::empty()), *f0_elem_ty)
(
f0_len
.try_to_target_usize(tcx)
.expect("expected SIMD field to have definite array size"),
*f0_elem_ty,
)
}
#[inline]

View File

@ -863,7 +863,7 @@ fn open_drop(&mut self) -> BasicBlock {
ty::Adt(def, args) => self.open_drop_for_adt(*def, args),
ty::Dynamic(..) => self.complete_drop(self.succ, self.unwind),
ty::Array(ety, size) => {
let size = size.try_eval_target_usize(self.tcx(), self.elaborator.param_env());
let size = size.try_to_target_usize(self.tcx());
self.open_drop_for_array(*ety, size)
}
ty::Slice(ety) => self.drop_loop_pair(*ety),

View File

@ -18,18 +18,12 @@ struct MoveDataBuilder<'a, 'tcx, F> {
body: &'a Body<'tcx>,
loc: Location,
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
data: MoveData<'tcx>,
filter: F,
}
impl<'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> MoveDataBuilder<'a, 'tcx, F> {
fn new(
body: &'a Body<'tcx>,
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
filter: F,
) -> Self {
fn new(body: &'a Body<'tcx>, tcx: TyCtxt<'tcx>, filter: F) -> Self {
let mut move_paths = IndexVec::new();
let mut path_map = IndexVec::new();
let mut init_path_map = IndexVec::new();
@ -59,7 +53,6 @@ fn new(
body,
loc: Location::START,
tcx,
param_env,
data: MoveData {
moves: IndexVec::new(),
loc_map: LocationMap::new(body),
@ -308,10 +301,9 @@ fn finalize(self) -> MoveData<'tcx> {
pub(super) fn gather_moves<'tcx>(
body: &Body<'tcx>,
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
filter: impl Fn(Ty<'tcx>) -> bool,
) -> MoveData<'tcx> {
let mut builder = MoveDataBuilder::new(body, tcx, param_env, filter);
let mut builder = MoveDataBuilder::new(body, tcx, filter);
builder.gather_args();
@ -550,7 +542,9 @@ fn gather_move(&mut self, place: Place<'tcx>) {
};
let base_ty = base_place.ty(self.body, self.tcx).ty;
let len: u64 = match base_ty.kind() {
ty::Array(_, size) => size.eval_target_usize(self.tcx, self.param_env),
ty::Array(_, size) => size
.try_to_target_usize(self.tcx)
.expect("expected subslice projection on fixed-size array"),
_ => bug!("from_end: false slice pattern of non-array type"),
};
for offset in from..to {

View File

@ -4,7 +4,7 @@
use rustc_data_structures::fx::FxHashMap;
use rustc_index::{IndexSlice, IndexVec};
use rustc_middle::mir::*;
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_span::Span;
use smallvec::SmallVec;
@ -352,10 +352,9 @@ impl<'tcx> MoveData<'tcx> {
pub fn gather_moves(
body: &Body<'tcx>,
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
filter: impl Fn(Ty<'tcx>) -> bool,
) -> MoveData<'tcx> {
builder::gather_moves(body, tcx, param_env, filter)
builder::gather_moves(body, tcx, filter)
}
/// For the move path `mpi`, returns the root local variable that starts the path.

View File

@ -40,8 +40,7 @@ pub fn sanity_check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
debug!("running rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
}
let param_env = tcx.param_env(def_id);
let move_data = MoveData::gather_moves(body, tcx, param_env, |_| true);
let move_data = MoveData::gather_moves(body, tcx, |_| true);
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_init).is_some() {
let flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)

View File

@ -1957,7 +1957,8 @@ fn check_must_not_suspend_ty<'tcx>(
let descr_pre = &format!("{}array{} of ", data.descr_pre, plural_suffix);
check_must_not_suspend_ty(tcx, ty, hir_id, param_env, SuspendCheckData {
descr_pre,
plural_len: len.try_eval_target_usize(tcx, param_env).unwrap_or(0) as usize + 1,
// FIXME(must_not_suspend): This is wrong. We should handle printing unevaluated consts.
plural_len: len.try_to_target_usize(tcx).unwrap_or(0) as usize + 1,
..data
})
}

View File

@ -58,8 +58,7 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let param_env = tcx.param_env_reveal_all_normalized(def_id);
// For types that do not need dropping, the behaviour is trivial. So we only need to track
// init/uninit for types that do need dropping.
let move_data =
MoveData::gather_moves(body, tcx, param_env, |ty| ty.needs_drop(tcx, param_env));
let move_data = MoveData::gather_moves(body, tcx, |ty| ty.needs_drop(tcx, param_env));
let elaborate_patch = {
let env = MoveDataParamEnv { move_data, param_env };

View File

@ -602,7 +602,7 @@ fn eval_rvalue(&mut self, rvalue: &Rvalue<'tcx>, dest: &Place<'tcx>) -> Option<(
Len(place) => {
let len = if let ty::Array(_, n) = place.ty(self.local_decls(), self.tcx).ty.kind()
{
n.try_eval_target_usize(self.tcx, self.param_env)?
n.try_to_target_usize(self.tcx)?
} else {
match self.get_const(place)? {
Value::Immediate(src) => src.len(&self.ecx).discard_err()?,

View File

@ -329,7 +329,7 @@ fn validate_place(&mut self, place: PlaceRef<'tcx>) -> Result<(), Unpromotable>
// Determine the type of the thing we are indexing.
&& let ty::Array(_, len) = place_base.ty(self.body, self.tcx).ty.kind()
// It's an array; determine its length.
&& let Some(len) = len.try_eval_target_usize(self.tcx, self.param_env)
&& let Some(len) = len.try_to_target_usize(self.tcx)
// If the index is in-bounds, go ahead.
&& idx < len
{
@ -407,7 +407,7 @@ fn validate_ref(&mut self, kind: BorrowKind, place: &Place<'tcx>) -> Result<(),
// mutably without consequences. However, only &mut []
// is allowed right now.
if let ty::Array(_, len) = ty.kind() {
match len.try_eval_target_usize(self.tcx, self.param_env) {
match len.try_to_target_usize(self.tcx) {
Some(0) => {}
_ => return Err(Unpromotable),
}

View File

@ -19,8 +19,7 @@
impl<'tcx> crate::MirPass<'tcx> for RemoveUninitDrops {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let param_env = tcx.param_env(body.source.def_id());
let move_data =
MoveData::gather_moves(body, tcx, param_env, |ty| ty.needs_drop(tcx, param_env));
let move_data = MoveData::gather_moves(body, tcx, |ty| ty.needs_drop(tcx, param_env));
let mut maybe_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
.into_engine(tcx, body)

View File

@ -133,7 +133,9 @@ fn encode_const<'tcx>(
// bool value false is encoded as 0 and true as 1.
match ct_ty.kind() {
ty::Int(ity) => {
let bits = c.eval_bits(tcx, ty::ParamEnv::reveal_all());
let bits = c
.try_to_bits(tcx, ty::ParamEnv::reveal_all())
.expect("expected monomorphic const in cfi");
let val = Integer::from_int_ty(&tcx, *ity).size().sign_extend(bits) as i128;
if val < 0 {
s.push('n');
@ -141,7 +143,9 @@ fn encode_const<'tcx>(
let _ = write!(s, "{val}");
}
ty::Uint(_) => {
let val = c.eval_bits(tcx, ty::ParamEnv::reveal_all());
let val = c
.try_to_bits(tcx, ty::ParamEnv::reveal_all())
.expect("expected monomorphic const in cfi");
let _ = write!(s, "{val}");
}
ty::Bool => {

View File

@ -406,7 +406,7 @@ fn eval_target_usize_ty(&self, cnst: &TyConst) -> Result<u64, Error> {
let tcx = tables.tcx;
let mir_const = cnst.internal(&mut *tables, tcx);
mir_const
.try_eval_target_usize(tables.tcx, ParamEnv::empty())
.try_to_target_usize(tables.tcx)
.ok_or_else(|| Error::new(format!("Const `{cnst:?}` cannot be encoded as u64")))
}

View File

@ -592,7 +592,9 @@ fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => {
ct_ty.print(self)?;
let mut bits = ct.eval_bits(self.tcx, ty::ParamEnv::reveal_all());
let mut bits = ct
.try_to_bits(self.tcx, ty::ParamEnv::reveal_all())
.expect("expected const to be monomorphic");
// Negative integer values are mangled using `n` as a "sign prefix".
if let ty::Int(ity) = ct_ty.kind() {

View File

@ -231,7 +231,7 @@ pub fn on_unimplemented_note(
// Arrays give us `[]`, `[{ty}; _]` and `[{ty}; N]`
if let ty::Array(aty, len) = self_ty.kind() {
flags.push((sym::_Self, Some("[]".to_string())));
let len = len.try_to_valtree().and_then(|v| v.try_to_target_usize(self.tcx));
let len = len.try_to_target_usize(self.tcx);
flags.push((sym::_Self, Some(format!("[{aty}; _]"))));
if let Some(n) = len {
flags.push((sym::_Self, Some(format!("[{aty}; {n}]"))));

View File

@ -85,7 +85,6 @@ mod rustc {
use rustc_macros::TypeVisitable;
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::{Const, ParamEnv, Ty, TyCtxt, ValTree};
use rustc_span::DUMMY_SP;
use super::*;
@ -134,7 +133,7 @@ pub fn from_const<'tcx>(
use rustc_middle::ty::ScalarInt;
use rustc_span::symbol::sym;
let Ok((ty, cv)) = c.eval(tcx, param_env, DUMMY_SP) else {
let Some((cv, ty)) = c.try_to_valtree() else {
return Some(Self {
alignment: true,
lifetimes: true,

View File

@ -170,12 +170,12 @@ fn layout_of_uncached<'tcx>(
if let Abi::Scalar(scalar) | Abi::ScalarPair(scalar, _) = &mut layout.abi {
if let Some(start) = start {
scalar.valid_range_mut().start = start
.try_eval_bits(tcx, param_env)
.try_to_bits(tcx, param_env)
.ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?;
}
if let Some(end) = end {
let mut end = end
.try_eval_bits(tcx, param_env)
.try_to_bits(tcx, param_env)
.ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?;
if !include_end {
end = end.wrapping_sub(1);
@ -315,7 +315,7 @@ fn layout_of_uncached<'tcx>(
}
let count = count
.try_eval_target_usize(tcx, param_env)
.try_to_target_usize(tcx)
.ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?;
let element = cx.layout_of(element)?;
let size = element