about summary refs log tree commit diff
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2025-02-03 14:29:13 +1100
committerZalathar <Zalathar@users.noreply.github.com>2025-02-03 14:53:43 +1100
commit2fb1261c2156087d003359db25b13b282934669c (patch)
treebb3660e2819c3b6de5fa12615bc1ff7f264d835b
parent85f4cdc62692f27e897ba0da2cd5fae30a19f740 (diff)
downloadrust-2fb1261c2156087d003359db25b13b282934669c.tar.gz
rust-2fb1261c2156087d003359db25b13b282934669c.zip
Simplify the pattern unpeeling in `lower_pattern_range_endpoint`
-rw-r--r--compiler/rustc_mir_build/src/thir/pattern/mod.rs51
1 files changed, 25 insertions, 26 deletions
diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs
index 3aa2b343b55..0aa61152330 100644
--- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs
+++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs
@@ -161,35 +161,34 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
     ) -> Result<Option<PatRangeBoundary<'tcx>>, ErrorGuaranteed> {
         let Some(expr) = expr else { return Ok(None) };
 
-        let (kind, ascr, inline_const) = match self.lower_lit(expr) {
-            PatKind::ExpandedConstant { subpattern, def_id, is_inline: true } => {
-                (subpattern.kind, None, def_id.as_local())
-            }
-            PatKind::ExpandedConstant { subpattern, is_inline: false, .. } => {
-                (subpattern.kind, None, None)
-            }
-            PatKind::AscribeUserType { ascription, subpattern: box Pat { kind, .. } } => {
-                (kind, Some(ascription), None)
-            }
-            kind => (kind, None, None),
-        };
-        let value = match kind {
-            PatKind::Constant { value } => value,
-            PatKind::ExpandedConstant { subpattern, .. }
-                if let PatKind::Constant { value } = subpattern.kind =>
-            {
-                value
-            }
-            _ => {
-                let msg = format!(
-                    "found bad range pattern endpoint `{expr:?}` outside of error recovery"
-                );
-                return Err(self.tcx.dcx().span_delayed_bug(expr.span, msg));
+        // Lower the endpoint into a temporary `PatKind` that will then be
+        // deconstructed to obtain the constant value and other data.
+        let mut kind: PatKind<'tcx> = self.lower_lit(expr);
+
+        // Unpeel any ascription or inline-const wrapper nodes.
+        loop {
+            match kind {
+                PatKind::AscribeUserType { ascription, subpattern } => {
+                    ascriptions.push(ascription);
+                    kind = subpattern.kind;
+                }
+                PatKind::ExpandedConstant { is_inline, def_id, subpattern } => {
+                    if is_inline {
+                        inline_consts.extend(def_id.as_local());
+                    }
+                    kind = subpattern.kind;
+                }
+                _ => break,
             }
+        }
+
+        // The unpeeled kind should now be a constant, giving us the endpoint value.
+        let PatKind::Constant { value } = kind else {
+            let msg =
+                format!("found bad range pattern endpoint `{expr:?}` outside of error recovery");
+            return Err(self.tcx.dcx().span_delayed_bug(expr.span, msg));
         };
 
-        ascriptions.extend(ascr);
-        inline_consts.extend(inline_const);
         Ok(Some(PatRangeBoundary::Finite(value)))
     }