From d72cc93e4ee2900d2b26900c4779461fc8f4821a Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sun, 2 Feb 2025 15:58:43 +1100 Subject: [PATCH] Remove some non-trivial `box` patterns These particular patterns make it harder to experiment with alternate representations for THIR patterns and subpatterns. --- .../src/builder/matches/mod.rs | 20 ++++++++----------- .../src/thir/pattern/check_match.rs | 20 +++++++++---------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_mir_build/src/builder/matches/mod.rs b/compiler/rustc_mir_build/src/builder/matches/mod.rs index dcc9be10dc1..a2995c04727 100644 --- a/compiler/rustc_mir_build/src/builder/matches/mod.rs +++ b/compiler/rustc_mir_build/src/builder/matches/mod.rs @@ -605,19 +605,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // Optimize the case of `let x: T = ...` to write directly // into `x` and then require that `T == typeof(x)`. PatKind::AscribeUserType { - subpattern: - box Pat { - kind: - PatKind::Binding { - mode: BindingMode(ByRef::No, _), - var, - subpattern: None, - .. - }, - .. - }, + ref subpattern, ascription: thir::Ascription { ref annotation, variance: _ }, - } => { + } if let PatKind::Binding { + mode: BindingMode(ByRef::No, _), + var, + subpattern: None, + .. + } = subpattern.kind => + { let place = self.storage_live_binding( block, var, diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 8247a6c6a32..697cb7cf37a 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -676,12 +676,14 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> { let mut interpreted_as_const = None; let mut interpreted_as_const_sugg = None; - if let PatKind::ExpandedConstant { def_id, is_inline: false, .. } - | PatKind::AscribeUserType { - subpattern: - box Pat { kind: PatKind::ExpandedConstant { def_id, is_inline: false, .. }, .. }, - .. - } = pat.kind + // These next few matches want to peek through `AscribeUserType` to see + // the underlying pattern. + let mut unpeeled_pat = pat; + while let PatKind::AscribeUserType { ref subpattern, .. } = unpeeled_pat.kind { + unpeeled_pat = subpattern; + } + + if let PatKind::ExpandedConstant { def_id, is_inline: false, .. } = unpeeled_pat.kind && let DefKind::Const = self.tcx.def_kind(def_id) && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span) // We filter out paths with multiple path::segments. @@ -692,11 +694,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> { // When we encounter a constant as the binding name, point at the `const` definition. interpreted_as_const = Some(span); interpreted_as_const_sugg = Some(InterpretedAsConst { span: pat.span, variable }); - } else if let PatKind::Constant { .. } - | PatKind::AscribeUserType { - subpattern: box Pat { kind: PatKind::Constant { .. }, .. }, - .. - } = pat.kind + } else if let PatKind::Constant { .. } = unpeeled_pat.kind && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span) { // If the pattern to match is an integer literal: