Skip to content

Commit 210e46b

Browse files
committed
Add pattern walking support to THIR walker
1 parent 9fef8d9 commit 210e46b

File tree

1 file changed

+48
-5
lines changed
  • compiler/rustc_mir_build/src/thir

1 file changed

+48
-5
lines changed

compiler/rustc_mir_build/src/thir/visit.rs

+48-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ pub trait Visitor<'a, 'tcx: 'a>: Sized {
2020
walk_arm(self, arm);
2121
}
2222

23+
fn visit_pat(&mut self, pat: &Pat<'tcx>) {
24+
walk_pat(self, pat);
25+
}
26+
2327
fn visit_const(&mut self, _cnst: &'tcx Const<'tcx>) {}
2428
}
2529

@@ -142,18 +146,19 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
142146
}
143147

144148
pub fn walk_stmt<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, stmt: &Stmt<'tcx>) {
145-
match stmt.kind {
146-
StmtKind::Expr { expr, scope: _ } => visitor.visit_expr(&visitor.thir()[expr]),
149+
match &stmt.kind {
150+
StmtKind::Expr { expr, scope: _ } => visitor.visit_expr(&visitor.thir()[*expr]),
147151
StmtKind::Let {
148152
initializer,
149153
remainder_scope: _,
150154
init_scope: _,
151-
pattern: _,
155+
pattern,
152156
lint_level: _,
153157
} => {
154158
if let Some(init) = initializer {
155-
visitor.visit_expr(&visitor.thir()[init]);
159+
visitor.visit_expr(&visitor.thir()[*init]);
156160
}
161+
visitor.visit_pat(&pattern);
157162
}
158163
}
159164
}
@@ -170,10 +175,48 @@ pub fn walk_block<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, block: &B
170175
pub fn walk_arm<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, arm: &Arm<'tcx>) {
171176
match arm.guard {
172177
Some(Guard::If(expr)) => visitor.visit_expr(&visitor.thir()[expr]),
173-
Some(Guard::IfLet(ref _pat, expr)) => {
178+
Some(Guard::IfLet(ref pat, expr)) => {
179+
visitor.visit_pat(pat);
174180
visitor.visit_expr(&visitor.thir()[expr]);
175181
}
176182
None => {}
177183
}
184+
visitor.visit_pat(&arm.pattern);
178185
visitor.visit_expr(&visitor.thir()[arm.body]);
179186
}
187+
188+
pub fn walk_pat<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, pat: &Pat<'tcx>) {
189+
use PatKind::*;
190+
match pat.kind.as_ref() {
191+
AscribeUserType { subpattern, .. }
192+
| Deref { subpattern, .. }
193+
| Binding { subpattern: Some(subpattern), .. } => visitor.visit_pat(&subpattern),
194+
Binding { .. } | Wild => {}
195+
Variant { subpatterns, .. } | Leaf { subpatterns } => {
196+
for subpattern in subpatterns {
197+
visitor.visit_pat(&subpattern.pattern);
198+
}
199+
}
200+
Constant { value } => visitor.visit_const(value),
201+
Range(range) => {
202+
visitor.visit_const(range.lo);
203+
visitor.visit_const(range.hi);
204+
}
205+
Slice { prefix, slice, suffix } | Array { prefix, slice, suffix } => {
206+
for subpattern in prefix {
207+
visitor.visit_pat(&subpattern);
208+
}
209+
if let Some(pat) = slice {
210+
visitor.visit_pat(pat);
211+
}
212+
for subpattern in suffix {
213+
visitor.visit_pat(&subpattern);
214+
}
215+
}
216+
Or { pats } => {
217+
for pat in pats {
218+
visitor.visit_pat(&pat);
219+
}
220+
}
221+
};
222+
}

0 commit comments

Comments
 (0)