From 2273aeed9decabdbbb0713a8b0749de0e9cd4ea7 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 22 Sep 2024 14:11:44 -0400 Subject: [PATCH] Replace calls to Const::eval in mir build --- compiler/rustc_mir_build/src/build/expr/as_rvalue.rs | 2 +- .../rustc_mir_build/src/build/matches/match_pair.rs | 7 ++++++- compiler/rustc_mir_build/src/thir/pattern/mod.rs | 4 +++- compiler/rustc_pattern_analysis/src/rustc.rs | 11 +++++++---- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs index 0c9571da3cf..5cf5d7d2754 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs @@ -57,7 +57,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { this.in_scope(region_scope, lint_level, |this| this.as_rvalue(block, scope, value)) } ExprKind::Repeat { value, count } => { - if Some(0) == count.try_eval_target_usize(this.tcx, this.param_env) { + if Some(0) == count.try_to_target_usize(this.tcx) { this.build_zero_repeat(block, value, scope, source_info) } else { let value_operand = unpack!( diff --git a/compiler/rustc_mir_build/src/build/matches/match_pair.rs b/compiler/rustc_mir_build/src/build/matches/match_pair.rs index ab2bfcbca3a..54937af0ff2 100644 --- a/compiler/rustc_mir_build/src/build/matches/match_pair.rs +++ b/compiler/rustc_mir_build/src/build/matches/match_pair.rs @@ -42,7 +42,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let tcx = self.tcx; let (min_length, exact_size) = if let Some(place_resolved) = place.try_to_place(self) { match place_resolved.ty(&self.local_decls, tcx).ty.kind() { - ty::Array(_, length) => (length.eval_target_usize(tcx, self.param_env), true), + ty::Array(_, length) => ( + length + .try_to_target_usize(tcx) + .expect("expected len of array pat to be definite"), + true, + ), _ => ((prefix.len() + suffix.len()).try_into().unwrap(), false), } } else { diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index d78e1f5da09..04e921ecc2e 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -441,7 +441,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { ty::Slice(..) => PatKind::Slice { prefix, slice, suffix }, // Fixed-length array, `[T; len]`. ty::Array(_, len) => { - let len = len.eval_target_usize(self.tcx, self.param_env); + let len = len + .try_to_target_usize(self.tcx) + .expect("expected len of array pat to be definite"); assert!(len >= prefix.len() as u64 + suffix.len() as u64); PatKind::Array { prefix, slice, suffix } } diff --git a/compiler/rustc_pattern_analysis/src/rustc.rs b/compiler/rustc_pattern_analysis/src/rustc.rs index 6c09f97bfe7..78195a3177d 100644 --- a/compiler/rustc_pattern_analysis/src/rustc.rs +++ b/compiler/rustc_pattern_analysis/src/rustc.rs @@ -352,7 +352,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> { ty::Array(sub_ty, len) => { // We treat arrays of a constant but unknown length like slices. ConstructorSet::Slice { - array_len: len.try_eval_target_usize(cx.tcx, cx.param_env).map(|l| l as usize), + array_len: len.try_to_target_usize(cx.tcx).map(|l| l as usize), subtype_is_empty: cx.is_uninhabited(*sub_ty), } } @@ -685,9 +685,12 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> { } PatKind::Array { prefix, slice, suffix } | PatKind::Slice { prefix, slice, suffix } => { let array_len = match ty.kind() { - ty::Array(_, length) => { - Some(length.eval_target_usize(cx.tcx, cx.param_env) as usize) - } + ty::Array(_, length) => Some( + length + .try_to_target_usize(cx.tcx) + .expect("expected len of array pat to be definite") + as usize, + ), ty::Slice(_) => None, _ => span_bug!(pat.span, "bad ty {} for slice pattern", ty.inner()), };