diff --git a/compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs b/compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs index cbe411d78d5..e7f9f894381 100644 --- a/compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs +++ b/compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs @@ -133,6 +133,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( .expect_const() .try_to_valtree() .expect("expected monomorphic const in codegen") + .0 .unwrap_branch(); assert_eq!(x.layout(), y.layout()); @@ -806,8 +807,10 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => m.load_scalar(fx), ty::Array(elem, len) if matches!(elem.kind(), ty::Uint(ty::UintTy::U8)) - && len.try_eval_target_usize(fx.tcx, ty::ParamEnv::reveal_all()) - == Some(expected_bytes) => + && len + .try_to_target_usize(fx.tcx) + .expect("expected monomorphic const in codegen") + == expected_bytes => { m.force_stack(fx).0.load( fx, @@ -907,8 +910,10 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => {} ty::Array(elem, len) if matches!(elem.kind(), ty::Uint(ty::UintTy::U8)) - && len.try_eval_target_usize(fx.tcx, ty::ParamEnv::reveal_all()) - == Some(expected_bytes) => {} + && len + .try_to_target_usize(fx.tcx) + .expect("expected monomorphic const in codegen") + == expected_bytes => {} _ => { fx.tcx.dcx().span_fatal( span, diff --git a/compiler/rustc_codegen_gcc/src/intrinsic/simd.rs b/compiler/rustc_codegen_gcc/src/intrinsic/simd.rs index 4e1b99fdebf..43dbfafa871 100644 --- a/compiler/rustc_codegen_gcc/src/intrinsic/simd.rs +++ b/compiler/rustc_codegen_gcc/src/intrinsic/simd.rs @@ -76,8 +76,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); @@ -696,8 +698,10 @@ macro_rules! arith_binary { } 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(result, bx.type_ix(expected_bytes * 8)); diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 1ddad917b78..7d4d8d8941d 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1817,7 +1817,7 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T // Only anon consts can implicitly capture params. // FIXME: is this correct behavior? let param_env = cx.tcx.param_env(*def_id); - ct.normalize(cx.tcx, param_env) + cx.tcx.normalize_erasing_regions(param_env, ct) } else { ct }; @@ -2033,8 +2033,8 @@ pub(crate) fn clean_middle_ty<'tcx>( Box::new(clean_middle_ty(bound_ty.rebind(ty), cx, None, None)), format!("{pat:?}").into_boxed_str(), ), - ty::Array(ty, mut n) => { - n = n.normalize(cx.tcx, ty::ParamEnv::reveal_all()); + ty::Array(ty, n) => { + let n = cx.tcx.normalize_erasing_regions(cx.param_env, n); let n = print_const(cx, n); Array(Box::new(clean_middle_ty(bound_ty.rebind(ty), cx, None, None)), n.into()) } diff --git a/src/tools/clippy/clippy_lints/src/indexing_slicing.rs b/src/tools/clippy/clippy_lints/src/indexing_slicing.rs index 22e9674714f..ae2c3e0491f 100644 --- a/src/tools/clippy/clippy_lints/src/indexing_slicing.rs +++ b/src/tools/clippy/clippy_lints/src/indexing_slicing.rs @@ -114,7 +114,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { if let Some(range) = higher::Range::hir(index) { // Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..] if let ty::Array(_, s) = ty.kind() { - let size: u128 = if let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env) { + let size: u128 = if let Some(size) = s.try_to_target_usize(cx.tcx) { size.into() } else { return; @@ -183,7 +183,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { && let ty::Uint(utype) = cx.typeck_results().expr_ty(index).kind() && *utype == ty::UintTy::Usize && let ty::Array(_, s) = ty.kind() - && let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env) + && let Some(size) = s.try_to_target_usize(cx.tcx) { // get constant offset and check whether it is in bounds let off = usize::try_from(off).unwrap(); diff --git a/src/tools/clippy/clippy_lints/src/loops/explicit_iter_loop.rs b/src/tools/clippy/clippy_lints/src/loops/explicit_iter_loop.rs index b134af500f5..022c6bcc70b 100644 --- a/src/tools/clippy/clippy_lints/src/loops/explicit_iter_loop.rs +++ b/src/tools/clippy/clippy_lints/src/loops/explicit_iter_loop.rs @@ -30,7 +30,7 @@ pub(super) fn check( return; } } else if count - .try_eval_target_usize(cx.tcx, cx.param_env) + .try_to_target_usize(cx.tcx) .map_or(true, |x| x > 32) && !msrv.meets(msrvs::ARRAY_IMPL_ANY_LEN) { diff --git a/src/tools/clippy/clippy_lints/src/loops/manual_memcpy.rs b/src/tools/clippy/clippy_lints/src/loops/manual_memcpy.rs index 68d063ad5e5..af089451759 100644 --- a/src/tools/clippy/clippy_lints/src/loops/manual_memcpy.rs +++ b/src/tools/clippy/clippy_lints/src/loops/manual_memcpy.rs @@ -472,7 +472,7 @@ fn extract_lit_value(expr: &Expr<'_>) -> Option { let arr_ty = cx.typeck_results().expr_ty(arr).peel_refs(); if let ty::Array(_, s) = arr_ty.kind() { - let size: u128 = if let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env) { + let size: u128 = if let Some(size) = s.try_to_target_usize(cx.tcx) { size.into() } else { return false; diff --git a/src/tools/clippy/clippy_lints/src/loops/needless_range_loop.rs b/src/tools/clippy/clippy_lints/src/loops/needless_range_loop.rs index 214b8b0f379..2e6442156ef 100644 --- a/src/tools/clippy/clippy_lints/src/loops/needless_range_loop.rs +++ b/src/tools/clippy/clippy_lints/src/loops/needless_range_loop.rs @@ -207,7 +207,7 @@ fn is_end_eq_array_len<'tcx>( if let ExprKind::Lit(lit) = end.kind && let ast::LitKind::Int(end_int, _) = lit.node && let ty::Array(_, arr_len_const) = indexed_ty.kind() - && let Some(arr_len) = arr_len_const.try_eval_target_usize(cx.tcx, cx.param_env) + && let Some(arr_len) = arr_len_const.try_to_target_usize(cx.tcx) { return match limits { ast::RangeLimits::Closed => end_int.get() + 1 >= arr_len.into(), diff --git a/src/tools/clippy/clippy_lints/src/matches/single_match.rs b/src/tools/clippy/clippy_lints/src/matches/single_match.rs index 2eda238ae8c..047d070a131 100644 --- a/src/tools/clippy/clippy_lints/src/matches/single_match.rs +++ b/src/tools/clippy/clippy_lints/src/matches/single_match.rs @@ -11,7 +11,7 @@ use rustc_hir::intravisit::{Visitor, walk_pat}; use rustc_hir::{Arm, Expr, ExprKind, HirId, Node, Pat, PatKind, QPath, StmtKind}; use rustc_lint::LateContext; -use rustc_middle::ty::{self, AdtDef, ParamEnv, TyCtxt, TypeckResults, VariantDef}; +use rustc_middle::ty::{self, AdtDef, TyCtxt, TypeckResults, VariantDef}; use rustc_span::{Span, sym}; use super::{MATCH_BOOL, SINGLE_MATCH, SINGLE_MATCH_ELSE}; @@ -67,7 +67,6 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'_>, arms: &'tc if v.has_enum { let cx = PatCtxt { tcx: cx.tcx, - param_env: cx.param_env, typeck, arena: DroplessArena::default(), }; @@ -185,7 +184,6 @@ fn visit_pat(&mut self, pat: &'tcx Pat<'_>) -> Self::Result { /// The context needed to manipulate a `PatState`. struct PatCtxt<'tcx> { tcx: TyCtxt<'tcx>, - param_env: ParamEnv<'tcx>, typeck: &'tcx TypeckResults<'tcx>, arena: DroplessArena, } @@ -334,7 +332,7 @@ fn add_pat<'tcx>(&mut self, cx: &'a PatCtxt<'tcx>, pat: &'tcx Pat<'_>) -> bool { if match *cx.typeck.pat_ty(pat).peel_refs().kind() { ty::Adt(adt, _) => adt.is_enum() || (adt.is_struct() && !adt.non_enum_variant().fields.is_empty()), ty::Tuple(tys) => !tys.is_empty(), - ty::Array(_, len) => len.try_eval_target_usize(cx.tcx, cx.param_env) != Some(1), + ty::Array(_, len) => len.try_to_target_usize(cx.tcx) != Some(1), ty::Slice(..) => true, _ => false, } => @@ -353,7 +351,7 @@ fn add_pat<'tcx>(&mut self, cx: &'a PatCtxt<'tcx>, pat: &'tcx Pat<'_>) -> bool { }, PatKind::Slice([sub_pat], _, []) | PatKind::Slice([], _, [sub_pat]) if let ty::Array(_, len) = *cx.typeck.pat_ty(pat).kind() - && len.try_eval_target_usize(cx.tcx, cx.param_env) == Some(1) => + && len.try_to_target_usize(cx.tcx) == Some(1) => { self.add_pat(cx, sub_pat) }, diff --git a/src/tools/clippy/clippy_lints/src/methods/iter_out_of_bounds.rs b/src/tools/clippy/clippy_lints/src/methods/iter_out_of_bounds.rs index f198849c5c0..9d462bd1845 100644 --- a/src/tools/clippy/clippy_lints/src/methods/iter_out_of_bounds.rs +++ b/src/tools/clippy/clippy_lints/src/methods/iter_out_of_bounds.rs @@ -31,14 +31,14 @@ fn get_iterator_length<'tcx>(cx: &LateContext<'tcx>, iter: &'tcx Expr<'tcx>) -> // parameter. substs .const_at(1) - .try_eval_target_usize(cx.tcx, cx.param_env) + .try_to_target_usize(cx.tcx) .map(u128::from) } else if cx.tcx.is_diagnostic_item(sym::SliceIter, did) && let ExprKind::MethodCall(_, recv, ..) = iter.kind { if let ty::Array(_, len) = cx.typeck_results().expr_ty(recv).peel_refs().kind() { // For slice::Iter<'_, T>, the receiver might be an array literal: [1,2,3].iter().skip(..) - len.try_eval_target_usize(cx.tcx, cx.param_env).map(u128::from) + len.try_to_target_usize(cx.tcx).map(u128::from) } else if let Some(args) = VecArgs::hir(cx, expr_or_init(cx, recv)) { match args { VecArgs::Vec(vec) => vec.len().try_into().ok(), diff --git a/src/tools/clippy/clippy_lints/src/methods/utils.rs b/src/tools/clippy/clippy_lints/src/methods/utils.rs index cf0ee569f13..6e39e7be2c4 100644 --- a/src/tools/clippy/clippy_lints/src/methods/utils.rs +++ b/src/tools/clippy/clippy_lints/src/methods/utils.rs @@ -18,7 +18,7 @@ fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool { ty::Slice(_) => true, ty::Adt(..) if let Some(boxed) = ty.boxed_ty() => may_slice(cx, boxed), ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::Vec), - ty::Array(_, size) => size.try_eval_target_usize(cx.tcx, cx.param_env).is_some(), + ty::Array(_, size) => size.try_to_target_usize(cx.tcx).is_some(), ty::Ref(_, inner, _) => may_slice(cx, *inner), _ => false, } diff --git a/src/tools/clippy/clippy_lints/src/trailing_empty_array.rs b/src/tools/clippy/clippy_lints/src/trailing_empty_array.rs index db8c63892b8..52bb7c4bd68 100644 --- a/src/tools/clippy/clippy_lints/src/trailing_empty_array.rs +++ b/src/tools/clippy/clippy_lints/src/trailing_empty_array.rs @@ -2,7 +2,7 @@ use clippy_utils::has_repr_attr; use rustc_hir::{Item, ItemKind}; use rustc_lint::{LateContext, LateLintPass}; -use rustc_middle::ty::{Const, FeedConstTy}; +use rustc_middle::ty; use rustc_session::declare_lint_pass; declare_clippy_lint! { @@ -55,16 +55,14 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { fn is_struct_with_trailing_zero_sized_array<'tcx>(cx: &LateContext<'tcx>, item: &Item<'tcx>) -> bool { if let ItemKind::Struct(data, _) = &item.kind - // First check if last field is an array && let Some(last_field) = data.fields().last() - && let rustc_hir::TyKind::Array(_, rustc_hir::ArrayLen::Body(length)) = last_field.ty.kind - - // Then check if that array is zero-sized - && let length = Const::from_const_arg(cx.tcx, length, FeedConstTy::No) - && let length = length.try_eval_target_usize(cx.tcx, cx.param_env) - && let Some(length) = length + && let field_ty = cx + .tcx + .normalize_erasing_regions(cx.param_env, cx.tcx.type_of(last_field.def_id).instantiate_identity()) + && let ty::Array(_, array_len) = *field_ty.kind() + && let Some(0) = array_len.try_to_target_usize(cx.tcx) { - length == 0 + true } else { false } diff --git a/src/tools/clippy/clippy_lints/src/tuple_array_conversions.rs b/src/tools/clippy/clippy_lints/src/tuple_array_conversions.rs index 3da8a449a7c..07d0f59b91c 100644 --- a/src/tools/clippy/clippy_lints/src/tuple_array_conversions.rs +++ b/src/tools/clippy/clippy_lints/src/tuple_array_conversions.rs @@ -190,7 +190,7 @@ fn all_bindings_are_for_conv<'tcx>( tys.len() == elements.len() && tys.iter().chain(final_tys.iter().copied()).all_equal() }, (ToType::Tuple, ty::Array(ty, len)) => { - let Some(len) = len.try_eval_target_usize(cx.tcx, cx.param_env) else { return false }; + let Some(len) = len.try_to_target_usize(cx.tcx) else { return false }; len as usize == elements.len() && final_tys.iter().chain(once(ty)).all_equal() }, _ => false, diff --git a/src/tools/clippy/clippy_utils/src/consts.rs b/src/tools/clippy/clippy_utils/src/consts.rs index a1cfb7be647..67c31abbdda 100644 --- a/src/tools/clippy/clippy_utils/src/consts.rs +++ b/src/tools/clippy/clippy_utils/src/consts.rs @@ -472,7 +472,7 @@ fn expr(&self, e: &Expr<'_>) -> Option> { ExprKind::Tup(tup) => self.multi(tup).map(Constant::Tuple), ExprKind::Repeat(value, _) => { let n = match self.typeck.expr_ty(e).kind() { - ty::Array(_, n) => n.try_eval_target_usize(self.tcx, self.param_env)?, + ty::Array(_, n) => n.try_to_target_usize(self.tcx)?, _ => span_bug!(e.span, "typeck error"), }; self.expr(value).map(|v| Constant::Repeat(Box::new(v), n)) @@ -553,7 +553,7 @@ pub fn eval_is_empty(&self, e: &Expr<'_>) -> Option { ExprKind::Array(vec) => self.multi(vec).map(|v| v.is_empty()), ExprKind::Repeat(..) => { if let ty::Array(_, n) = self.typeck.expr_ty(e).kind() { - Some(n.try_eval_target_usize(self.tcx, self.param_env)? == 0) + Some(n.try_to_target_usize(self.tcx)? == 0) } else { span_bug!(e.span, "typeck error"); } diff --git a/src/tools/clippy/clippy_utils/src/ty.rs b/src/tools/clippy/clippy_utils/src/ty.rs index b7a3569ccf0..21793f761d9 100644 --- a/src/tools/clippy/clippy_utils/src/ty.rs +++ b/src/tools/clippy/clippy_utils/src/ty.rs @@ -989,7 +989,7 @@ pub fn approx_ty_size<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> u64 { (Ok(size), _) => size, (Err(_), ty::Tuple(list)) => list.iter().map(|t| approx_ty_size(cx, t)).sum(), (Err(_), ty::Array(t, n)) => { - n.try_eval_target_usize(cx.tcx, cx.param_env).unwrap_or_default() * approx_ty_size(cx, *t) + n.try_to_target_usize(cx.tcx).unwrap_or_default() * approx_ty_size(cx, *t) }, (Err(_), ty::Adt(def, subst)) if def.is_struct() => def .variants() @@ -1207,7 +1207,7 @@ pub fn interior_mut_ty_chain(&mut self, cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> let chain = match *ty.kind() { ty::RawPtr(inner_ty, _) if !self.ignore_pointers => self.interior_mut_ty_chain(cx, inner_ty), ty::Ref(_, inner_ty, _) | ty::Slice(inner_ty) => self.interior_mut_ty_chain(cx, inner_ty), - ty::Array(inner_ty, size) if size.try_eval_target_usize(cx.tcx, cx.param_env) != Some(0) => { + ty::Array(inner_ty, size) if size.try_to_target_usize(cx.tcx) != Some(0) => { self.interior_mut_ty_chain(cx, inner_ty) }, ty::Tuple(fields) => fields.iter().find_map(|ty| self.interior_mut_ty_chain(cx, ty)), diff --git a/src/tools/miri/src/intrinsics/simd.rs b/src/tools/miri/src/intrinsics/simd.rs index de293495e86..6629afeb607 100644 --- a/src/tools/miri/src/intrinsics/simd.rs +++ b/src/tools/miri/src/intrinsics/simd.rs @@ -1,8 +1,7 @@ use either::Either; use rustc_apfloat::{Float, Round}; -use rustc_middle::ty::FloatTy; -use rustc_middle::ty::layout::{HasParamEnv, LayoutOf}; -use rustc_middle::{mir, ty}; +use rustc_middle::ty::layout::LayoutOf; +use rustc_middle::{mir, ty, ty::FloatTy}; use rustc_span::{Symbol, sym}; use rustc_target::abi::{Endian, HasDataLayout}; @@ -633,9 +632,9 @@ enum Op { let index = generic_args[2] .expect_const() - .eval(*this.tcx, this.param_env(), this.tcx.span) + .try_to_valtree() .unwrap() - .1 + .0 .unwrap_branch(); let index_len = index.len();