Skip to content

Commit 9cf05f3

Browse files
committed
Auto merge of #86378 - Smittyvb:thir-walker-pat, r=LeSeulArtichaut
Add pattern walking support to THIR walker Suggested in #85263 (comment), this splits off the support for pattern walking in THIR from #85263. This has no observable effect on THIR unsafety checking, since it is not currently possible to trigger unsafety from the THIR checker using the additional patterns or constants that are now walked. THIR patterns are walked in source code order. r? `@LeSeulArtichaut`
2 parents 9839f9c + 281dd6d commit 9cf05f3

File tree

1 file changed

+53
-2
lines changed
  • compiler/rustc_mir_build/src/thir

1 file changed

+53
-2
lines changed

compiler/rustc_mir_build/src/thir/visit.rs

+53-2
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

@@ -155,12 +159,13 @@ pub fn walk_stmt<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, stmt: &Stm
155159
initializer,
156160
remainder_scope: _,
157161
init_scope: _,
158-
pattern: _,
162+
ref pattern,
159163
lint_level: _,
160164
} => {
161165
if let Some(init) = initializer {
162166
visitor.visit_expr(&visitor.thir()[init]);
163167
}
168+
visitor.visit_pat(pattern);
164169
}
165170
}
166171
}
@@ -177,10 +182,56 @@ pub fn walk_block<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, block: &B
177182
pub fn walk_arm<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, arm: &Arm<'tcx>) {
178183
match arm.guard {
179184
Some(Guard::If(expr)) => visitor.visit_expr(&visitor.thir()[expr]),
180-
Some(Guard::IfLet(ref _pat, expr)) => {
185+
Some(Guard::IfLet(ref pat, expr)) => {
186+
visitor.visit_pat(pat);
181187
visitor.visit_expr(&visitor.thir()[expr]);
182188
}
183189
None => {}
184190
}
191+
visitor.visit_pat(&arm.pattern);
185192
visitor.visit_expr(&visitor.thir()[arm.body]);
186193
}
194+
195+
pub fn walk_pat<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, pat: &Pat<'tcx>) {
196+
use PatKind::*;
197+
match pat.kind.as_ref() {
198+
AscribeUserType { subpattern, ascription: _ }
199+
| Deref { subpattern }
200+
| Binding {
201+
subpattern: Some(subpattern),
202+
mutability: _,
203+
mode: _,
204+
var: _,
205+
ty: _,
206+
is_primary: _,
207+
name: _,
208+
} => visitor.visit_pat(&subpattern),
209+
Binding { .. } | Wild => {}
210+
Variant { subpatterns, adt_def: _, substs: _, variant_index: _ } | Leaf { subpatterns } => {
211+
for subpattern in subpatterns {
212+
visitor.visit_pat(&subpattern.pattern);
213+
}
214+
}
215+
Constant { value } => visitor.visit_const(value),
216+
Range(range) => {
217+
visitor.visit_const(range.lo);
218+
visitor.visit_const(range.hi);
219+
}
220+
Slice { prefix, slice, suffix } | Array { prefix, slice, suffix } => {
221+
for subpattern in prefix {
222+
visitor.visit_pat(&subpattern);
223+
}
224+
if let Some(pat) = slice {
225+
visitor.visit_pat(pat);
226+
}
227+
for subpattern in suffix {
228+
visitor.visit_pat(&subpattern);
229+
}
230+
}
231+
Or { pats } => {
232+
for pat in pats {
233+
visitor.visit_pat(&pat);
234+
}
235+
}
236+
};
237+
}

0 commit comments

Comments
 (0)