Skip to content

Commit c25e6cb

Browse files
Get rid of const eval_* and try_eval_* helpers
1 parent f559d61 commit c25e6cb

File tree

24 files changed

+84
-109
lines changed

24 files changed

+84
-109
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11281128
}
11291129
let projected_ty = curr_projected_ty.projection_ty_core(
11301130
tcx,
1131-
self.param_env,
11321131
proj,
11331132
|this, field, ()| {
11341133
let ty = this.field_ty(tcx, field);
@@ -1919,7 +1918,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19191918
// than 1.
19201919
// If the length is larger than 1, the repeat expression will need to copy the
19211920
// element, so we require the `Copy` trait.
1922-
if len.try_eval_target_usize(tcx, self.param_env).map_or(true, |len| len > 1) {
1921+
if len.try_to_target_usize(tcx).is_none_or(|len| len > 1) {
19231922
match operand {
19241923
Operand::Copy(..) | Operand::Constant(..) => {
19251924
// These are always okay: direct use of a const, or a value that can evidently be copied.

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,8 +1172,10 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
11721172
ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => args[0].immediate(),
11731173
ty::Array(elem, len)
11741174
if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
1175-
&& len.try_eval_target_usize(bx.tcx, ty::ParamEnv::reveal_all())
1176-
== Some(expected_bytes) =>
1175+
&& len
1176+
.try_to_target_usize(bx.tcx)
1177+
.expect("expected monomorphic const in codegen")
1178+
== expected_bytes =>
11771179
{
11781180
let place = PlaceRef::alloca(bx, args[0].layout);
11791181
args[0].val.store(bx, place);
@@ -1462,8 +1464,10 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
14621464
}
14631465
ty::Array(elem, len)
14641466
if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
1465-
&& len.try_eval_target_usize(bx.tcx, ty::ParamEnv::reveal_all())
1466-
== Some(expected_bytes) =>
1467+
&& len
1468+
.try_to_target_usize(bx.tcx)
1469+
.expect("expected monomorphic const in codegen")
1470+
== expected_bytes =>
14671471
{
14681472
// Zero-extend iN to the array length:
14691473
let ze = bx.zext(i_, bx.type_ix(expected_bytes * 8));

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -690,16 +690,20 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: ty::Const<'tcx>, output: &mut S
690690
ty::Int(ity) => {
691691
// FIXME: directly extract the bits from a valtree instead of evaluating an
692692
// already evaluated `Const` in order to get the bits.
693-
let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all());
693+
let bits = ct
694+
.try_to_bits(tcx, ty::ParamEnv::reveal_all())
695+
.expect("expected monomorphic const in codegen");
694696
let val = Integer::from_int_ty(&tcx, *ity).size().sign_extend(bits) as i128;
695697
write!(output, "{val}")
696698
}
697699
ty::Uint(_) => {
698-
let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all());
700+
let val = ct
701+
.try_to_bits(tcx, ty::ParamEnv::reveal_all())
702+
.expect("expected monomorphic const in codegen");
699703
write!(output, "{val}")
700704
}
701705
ty::Bool => {
702-
let val = ct.try_eval_bool(tcx, ty::ParamEnv::reveal_all()).unwrap();
706+
let val = ct.try_to_bool().expect("expected monomorphic const in codegen");
703707
write!(output, "{val}")
704708
}
705709
_ => {

compiler/rustc_const_eval/src/const_eval/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(crate) fn try_destructure_mir_constant_for_user_output<'tcx>(
4343

4444
// We go to `usize` as we cannot allocate anything bigger anyway.
4545
let (field_count, variant, down) = match ty.kind() {
46-
ty::Array(_, len) => (len.eval_target_usize(tcx.tcx, param_env) as usize, None, op),
46+
ty::Array(_, len) => (len.try_to_target_usize(tcx.tcx)? as usize, None, op),
4747
ty::Adt(def, _) if def.variants().is_empty() => {
4848
return None;
4949
}

compiler/rustc_const_eval/src/interpret/cast.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
398398
let ptr = self.read_pointer(src)?;
399399
let val = Immediate::new_slice(
400400
ptr,
401-
length.eval_target_usize(*self.tcx, self.param_env),
401+
length
402+
.try_to_target_usize(*self.tcx)
403+
.expect("expected monomorphic const in const eval"),
402404
self,
403405
);
404406
self.write_immediate(val, dest)

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,8 @@ fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
10821082
return;
10831083
}
10841084

1085-
if let Some(len) = len_const.try_eval_target_usize(tcx, tcx.param_env(def.did())) {
1085+
// TODO:
1086+
if let Some(len) = len_const.try_to_target_usize(tcx) {
10861087
if len == 0 {
10871088
struct_span_code_err!(tcx.dcx(), sp, E0075, "SIMD vector cannot be empty").emit();
10881089
return;

compiler/rustc_hir_analysis/src/check/intrinsicck.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
7777
let (size, ty) = match elem_ty.kind() {
7878
ty::Array(ty, len) => {
7979
if let Some(len) =
80-
len.try_eval_target_usize(self.tcx, self.tcx.param_env(adt.did()))
80+
// TODO:
81+
len.try_to_target_usize(self.tcx)
8182
{
8283
(len, *ty)
8384
} else {

compiler/rustc_lint/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2601,7 +2601,7 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
26012601
ty.tuple_fields().iter().find_map(|field| ty_find_init_error(cx, field, init))
26022602
}
26032603
Array(ty, len) => {
2604-
if matches!(len.try_eval_target_usize(cx.tcx, cx.param_env), Some(v) if v > 0) {
2604+
if matches!(len.try_to_target_usize(cx.tcx), Some(v) if v > 0) {
26052605
// Array length known at array non-empty -- recurse.
26062606
ty_find_init_error(cx, *ty, init)
26072607
} else {

compiler/rustc_lint/src/unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
346346
None
347347
}
348348
}
349-
ty::Array(ty, len) => match len.try_eval_target_usize(cx.tcx, cx.param_env) {
349+
ty::Array(ty, len) => match len.try_to_target_usize(cx.tcx) {
350350
// If the array is empty we don't lint, to avoid false positives
351351
Some(0) | None => None,
352352
// If the array is definitely non-empty, we can do `#[must_use]` checking.

compiler/rustc_middle/src/mir/tcx.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'tcx> PlaceTy<'tcx> {
5555
/// `PlaceElem`, where we can just use the `Ty` that is already
5656
/// stored inline on field projection elems.
5757
pub fn projection_ty(self, tcx: TyCtxt<'tcx>, elem: PlaceElem<'tcx>) -> PlaceTy<'tcx> {
58-
self.projection_ty_core(tcx, ty::ParamEnv::empty(), &elem, |_, _, ty| ty, |_, ty| ty)
58+
self.projection_ty_core(tcx, &elem, |_, _, ty| ty, |_, ty| ty)
5959
}
6060

6161
/// `place_ty.projection_ty_core(tcx, elem, |...| { ... })`
@@ -66,7 +66,6 @@ impl<'tcx> PlaceTy<'tcx> {
6666
pub fn projection_ty_core<V, T>(
6767
self,
6868
tcx: TyCtxt<'tcx>,
69-
param_env: ty::ParamEnv<'tcx>,
7069
elem: &ProjectionElem<V, T>,
7170
mut handle_field: impl FnMut(&Self, FieldIdx, T) -> Ty<'tcx>,
7271
mut handle_opaque_cast_and_subtype: impl FnMut(&Self, T) -> Ty<'tcx>,
@@ -93,7 +92,9 @@ impl<'tcx> PlaceTy<'tcx> {
9392
ty::Slice(..) => self.ty,
9493
ty::Array(inner, _) if !from_end => Ty::new_array(tcx, *inner, to - from),
9594
ty::Array(inner, size) if from_end => {
96-
let size = size.eval_target_usize(tcx, param_env);
95+
let size = size
96+
.try_to_target_usize(tcx)
97+
.expect("expected subslice projection on fixed-size array");
9798
let len = size - from - to;
9899
Ty::new_array(tcx, *inner, len)
99100
}

0 commit comments

Comments
 (0)