Skip to content

Commit 62fd815

Browse files
committed
!! (WIP) for_each_immediate_subpat
1 parent 41b3fdf commit 62fd815

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::{
@@ -662,27 +663,7 @@ impl<'tcx> Pat<'tcx> {
662663
return;
663664
}
664665

665-
use PatKind::*;
666-
match &self.kind {
667-
Wild
668-
| Never
669-
| Range(..)
670-
| Binding { subpattern: None, .. }
671-
| Constant { .. }
672-
| Error(_) => {}
673-
AscribeUserType { subpattern, .. }
674-
| Binding { subpattern: Some(subpattern), .. }
675-
| Deref { subpattern }
676-
| DerefPattern { subpattern, .. }
677-
| ExpandedConstant { subpattern, .. } => subpattern.walk_(it),
678-
Leaf { subpatterns } | Variant { subpatterns, .. } => {
679-
subpatterns.iter().for_each(|field| field.pattern.walk_(it))
680-
}
681-
Or { pats } => pats.iter().for_each(|p| p.walk_(it)),
682-
Array { box prefix, slice, box suffix } | Slice { box prefix, slice, box suffix } => {
683-
prefix.iter().chain(slice.as_deref()).chain(suffix.iter()).for_each(|p| p.walk_(it))
684-
}
685-
}
666+
for_each_immediate_subpat(self, |p| p.walk_(it));
686667
}
687668

688669
/// 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
@@ -237,36 +237,46 @@ pub fn walk_pat<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
237237
visitor: &mut V,
238238
pat: &'thir Pat<'tcx>,
239239
) {
240-
use PatKind::*;
241-
match &pat.kind {
242-
AscribeUserType { subpattern, ascription: _ }
243-
| Deref { subpattern }
244-
| DerefPattern { subpattern, .. }
245-
| Binding { subpattern: Some(subpattern), .. } => visitor.visit_pat(subpattern),
246-
Binding { .. } | Wild | Never | Error(_) => {}
247-
Variant { subpatterns, adt_def: _, args: _, variant_index: _ } | Leaf { subpatterns } => {
248-
for subpattern in subpatterns {
249-
visitor.visit_pat(&subpattern.pattern);
240+
for_each_immediate_subpat(pat, |p| visitor.visit_pat(p));
241+
}
242+
243+
/// Invokes `callback` on each immediate subpattern of `pat`, if any.
244+
/// A building block for assembling THIR pattern visitors.
245+
pub(crate) fn for_each_immediate_subpat<'a, 'tcx>(
246+
pat: &'a Pat<'tcx>,
247+
mut callback: impl FnMut(&'a Pat<'tcx>),
248+
) {
249+
match pat.kind {
250+
PatKind::Wild
251+
| PatKind::Binding { subpattern: None, .. }
252+
| PatKind::Constant { value: _ }
253+
| PatKind::Range(_)
254+
| PatKind::Never
255+
| PatKind::Error(_) => {}
256+
257+
PatKind::AscribeUserType { ref subpattern, .. }
258+
| PatKind::Binding { subpattern: Some(ref subpattern), .. }
259+
| PatKind::Deref { ref subpattern }
260+
| PatKind::DerefPattern { ref subpattern, .. }
261+
| PatKind::ExpandedConstant { ref subpattern, .. } => callback(subpattern),
262+
263+
PatKind::Variant { ref subpatterns, .. } | PatKind::Leaf { ref subpatterns } => {
264+
for field_pat in subpatterns {
265+
callback(&field_pat.pattern);
250266
}
251267
}
252-
Constant { value: _ } => {}
253-
ExpandedConstant { def_id: _, is_inline: _, subpattern } => visitor.visit_pat(subpattern),
254-
Range(_) => {}
255-
Slice { prefix, slice, suffix } | Array { prefix, slice, suffix } => {
256-
for subpattern in prefix.iter() {
257-
visitor.visit_pat(subpattern);
258-
}
259-
if let Some(pat) = slice {
260-
visitor.visit_pat(pat);
261-
}
262-
for subpattern in suffix.iter() {
263-
visitor.visit_pat(subpattern);
268+
269+
PatKind::Slice { ref prefix, ref slice, ref suffix }
270+
| PatKind::Array { ref prefix, ref slice, ref suffix } => {
271+
for pat in prefix.iter().chain(slice.as_deref()).chain(suffix) {
272+
callback(pat);
264273
}
265274
}
266-
Or { pats } => {
267-
for pat in pats.iter() {
268-
visitor.visit_pat(pat);
275+
276+
PatKind::Or { ref pats } => {
277+
for pat in pats {
278+
callback(pat);
269279
}
270280
}
271-
};
281+
}
272282
}

0 commit comments

Comments
 (0)