Skip to content

Commit 75b7f78

Browse files
committed
Extract for_each_immediate_subpat from THIR pattern visitors
1 parent 493c38b commit 75b7f78

File tree

2 files changed

+38
-47
lines changed

2 files changed

+38
-47
lines changed

compiler/rustc_middle/src/thir.rs

+2-21
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use tracing::instrument;
2828
use crate::middle::region;
2929
use crate::mir::interpret::AllocId;
3030
use crate::mir::{self, BinOp, BorrowKind, FakeReadCause, UnOp};
31+
use crate::thir::visit::for_each_immediate_subpat;
3132
use crate::ty::adjustment::PointerCoercion;
3233
use crate::ty::layout::IntegerExt;
3334
use crate::ty::{
@@ -672,27 +673,7 @@ impl<'tcx> Pat<'tcx> {
672673
return;
673674
}
674675

675-
use PatKind::*;
676-
match &self.kind {
677-
Wild
678-
| Never
679-
| Range(..)
680-
| Binding { subpattern: None, .. }
681-
| Constant { .. }
682-
| Error(_) => {}
683-
AscribeUserType { subpattern, .. }
684-
| Binding { subpattern: Some(subpattern), .. }
685-
| Deref { subpattern }
686-
| DerefPattern { subpattern, .. }
687-
| ExpandedConstant { subpattern, .. } => subpattern.walk_(it),
688-
Leaf { subpatterns } | Variant { subpatterns, .. } => {
689-
subpatterns.iter().for_each(|field| field.pattern.walk_(it))
690-
}
691-
Or { pats } => pats.iter().for_each(|p| p.walk_(it)),
692-
Array { box prefix, slice, box suffix } | Slice { box prefix, slice, box suffix } => {
693-
prefix.iter().chain(slice.as_deref()).chain(suffix.iter()).for_each(|p| p.walk_(it))
694-
}
695-
}
676+
for_each_immediate_subpat(self, |p| p.walk_(it));
696677
}
697678

698679
/// Whether the pattern has a `PatKind::Error` nested within.

compiler/rustc_middle/src/thir/visit.rs

+36-26
Original file line numberDiff line numberDiff line change
@@ -240,36 +240,46 @@ pub fn walk_pat<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
240240
visitor: &mut V,
241241
pat: &'thir Pat<'tcx>,
242242
) {
243-
use PatKind::*;
244-
match &pat.kind {
245-
AscribeUserType { subpattern, ascription: _ }
246-
| Deref { subpattern }
247-
| DerefPattern { subpattern, .. }
248-
| Binding { subpattern: Some(subpattern), .. } => visitor.visit_pat(subpattern),
249-
Binding { .. } | Wild | Never | Error(_) => {}
250-
Variant { subpatterns, adt_def: _, args: _, variant_index: _ } | Leaf { subpatterns } => {
251-
for subpattern in subpatterns {
252-
visitor.visit_pat(&subpattern.pattern);
243+
for_each_immediate_subpat(pat, |p| visitor.visit_pat(p));
244+
}
245+
246+
/// Invokes `callback` on each immediate subpattern of `pat`, if any.
247+
/// A building block for assembling THIR pattern visitors.
248+
pub(crate) fn for_each_immediate_subpat<'a, 'tcx>(
249+
pat: &'a Pat<'tcx>,
250+
mut callback: impl FnMut(&'a Pat<'tcx>),
251+
) {
252+
match pat.kind {
253+
PatKind::Wild
254+
| PatKind::Binding { subpattern: None, .. }
255+
| PatKind::Constant { value: _ }
256+
| PatKind::Range(_)
257+
| PatKind::Never
258+
| PatKind::Error(_) => {}
259+
260+
PatKind::AscribeUserType { ref subpattern, .. }
261+
| PatKind::Binding { subpattern: Some(ref subpattern), .. }
262+
| PatKind::Deref { ref subpattern }
263+
| PatKind::DerefPattern { ref subpattern, .. }
264+
| PatKind::ExpandedConstant { ref subpattern, .. } => callback(subpattern),
265+
266+
PatKind::Variant { ref subpatterns, .. } | PatKind::Leaf { ref subpatterns } => {
267+
for field_pat in subpatterns {
268+
callback(&field_pat.pattern);
253269
}
254270
}
255-
Constant { value: _ } => {}
256-
ExpandedConstant { def_id: _, is_inline: _, subpattern } => visitor.visit_pat(subpattern),
257-
Range(_) => {}
258-
Slice { prefix, slice, suffix } | Array { prefix, slice, suffix } => {
259-
for subpattern in prefix.iter() {
260-
visitor.visit_pat(subpattern);
261-
}
262-
if let Some(pat) = slice {
263-
visitor.visit_pat(pat);
264-
}
265-
for subpattern in suffix.iter() {
266-
visitor.visit_pat(subpattern);
271+
272+
PatKind::Slice { ref prefix, ref slice, ref suffix }
273+
| PatKind::Array { ref prefix, ref slice, ref suffix } => {
274+
for pat in prefix.iter().chain(slice.as_deref()).chain(suffix) {
275+
callback(pat);
267276
}
268277
}
269-
Or { pats } => {
270-
for pat in pats.iter() {
271-
visitor.visit_pat(pat);
278+
279+
PatKind::Or { ref pats } => {
280+
for pat in pats {
281+
callback(pat);
272282
}
273283
}
274-
};
284+
}
275285
}

0 commit comments

Comments
 (0)