Skip to content

Commit c51bfaf

Browse files
authored
Rollup merge of #135308 - compiler-errors:scope-visit, r=oli-obk
Make sure to walk into nested const blocks in `RegionResolutionVisitor` Fixes#135306 I tried auditing the rest of the visitors that called `.visit_body`, and it seems like this is the only one that was missing it. I wonder if we should modify intravisit (specifcially, that `NestedBodyFilter` stuff) to make this less likely to happen, tho... r? oli-obk
2 parents 6b88aa0 + 9585f36 commit c51bfaf

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

‎compiler/rustc_hir_analysis/src/check/region.rs‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct Context {
3131
parent:Option<(Scope,ScopeDepth)>,
3232
}
3333

34-
structRegionResolutionVisitor<'tcx>{
34+
structScopeResolutionVisitor<'tcx>{
3535
tcx:TyCtxt<'tcx>,
3636

3737
// The number of expressions and patterns visited in the current body.
@@ -71,7 +71,7 @@ struct RegionResolutionVisitor<'tcx> {
7171
}
7272

7373
/// Records the lifetime of a local variable as `cx.var_parent`
74-
fnrecord_var_lifetime(visitor:&mutRegionResolutionVisitor<'_>,var_id: hir::ItemLocalId){
74+
fnrecord_var_lifetime(visitor:&mutScopeResolutionVisitor<'_>,var_id: hir::ItemLocalId){
7575
match visitor.cx.var_parent{
7676
None => {
7777
// this can happen in extern fn declarations like
@@ -82,7 +82,7 @@ fn record_var_lifetime(visitor: &mut RegionResolutionVisitor<'_>, var_id: hir::I
8282
}
8383
}
8484

85-
fnresolve_block<'tcx>(visitor:&mutRegionResolutionVisitor<'tcx>,blk:&'tcx hir::Block<'tcx>){
85+
fnresolve_block<'tcx>(visitor:&mutScopeResolutionVisitor<'tcx>,blk:&'tcx hir::Block<'tcx>){
8686
debug!("resolve_block(blk.hir_id={:?})", blk.hir_id);
8787

8888
let prev_cx = visitor.cx;
@@ -193,7 +193,7 @@ fn resolve_block<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, blk: &'tcx h
193193
visitor.cx = prev_cx;
194194
}
195195

196-
fnresolve_arm<'tcx>(visitor:&mutRegionResolutionVisitor<'tcx>,arm:&'tcx hir::Arm<'tcx>){
196+
fnresolve_arm<'tcx>(visitor:&mutScopeResolutionVisitor<'tcx>,arm:&'tcx hir::Arm<'tcx>){
197197
fnhas_let_expr(expr:&Expr<'_>) -> bool{
198198
match&expr.kind{
199199
hir::ExprKind::Binary(_, lhs, rhs) => has_let_expr(lhs) || has_let_expr(rhs),
@@ -220,7 +220,7 @@ fn resolve_arm<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, arm: &'tcx hir
220220
visitor.cx = prev_cx;
221221
}
222222

223-
fnresolve_pat<'tcx>(visitor:&mutRegionResolutionVisitor<'tcx>,pat:&'tcx hir::Pat<'tcx>){
223+
fnresolve_pat<'tcx>(visitor:&mutScopeResolutionVisitor<'tcx>,pat:&'tcx hir::Pat<'tcx>){
224224
visitor.record_child_scope(Scope{local_id: pat.hir_id.local_id,data:ScopeData::Node});
225225

226226
// If this is a binding then record the lifetime of that binding.
@@ -237,7 +237,7 @@ fn resolve_pat<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, pat: &'tcx hir
237237
debug!("resolve_pat - post-increment {} pat = {:?}", visitor.expr_and_pat_count, pat);
238238
}
239239

240-
fnresolve_stmt<'tcx>(visitor:&mutRegionResolutionVisitor<'tcx>,stmt:&'tcx hir::Stmt<'tcx>){
240+
fnresolve_stmt<'tcx>(visitor:&mutScopeResolutionVisitor<'tcx>,stmt:&'tcx hir::Stmt<'tcx>){
241241
let stmt_id = stmt.hir_id.local_id;
242242
debug!("resolve_stmt(stmt.id={:?})", stmt_id);
243243

@@ -256,7 +256,7 @@ fn resolve_stmt<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, stmt: &'tcx h
256256
visitor.cx.parent = prev_parent;
257257
}
258258

259-
fnresolve_expr<'tcx>(visitor:&mutRegionResolutionVisitor<'tcx>,expr:&'tcx hir::Expr<'tcx>){
259+
fnresolve_expr<'tcx>(visitor:&mutScopeResolutionVisitor<'tcx>,expr:&'tcx hir::Expr<'tcx>){
260260
debug!("resolve_expr - pre-increment {} expr = {:?}", visitor.expr_and_pat_count, expr);
261261

262262
let prev_cx = visitor.cx;
@@ -420,10 +420,10 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
420420
// properly, we can't miss any types.
421421

422422
match expr.kind{
423-
// Manually recurse over closures and inline consts, because they are the only
424-
// case of nested bodies that share the parent environment.
425-
hir::ExprKind::Closure(&hir::Closure{ body, .. })
426-
| hir::ExprKind::ConstBlock(hir::ConstBlock{ body, .. }) => {
423+
// Manually recurse over closures, because they are nested bodies
424+
// that share the parent environment. We handle const blocks in
425+
// `visit_inline_const`.
426+
hir::ExprKind::Closure(&hir::Closure{ body, .. }) => {
427427
let body = visitor.tcx.hir().body(body);
428428
visitor.visit_body(body);
429429
}
@@ -554,7 +554,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
554554
}
555555

556556
fnresolve_local<'tcx>(
557-
visitor:&mutRegionResolutionVisitor<'tcx>,
557+
visitor:&mutScopeResolutionVisitor<'tcx>,
558558
pat:Option<&'tcx hir::Pat<'tcx>>,
559559
init:Option<&'tcx hir::Expr<'tcx>>,
560560
){
@@ -725,7 +725,7 @@ fn resolve_local<'tcx>(
725725
/// | ( E& )
726726
/// ```
727727
fnrecord_rvalue_scope_if_borrow_expr<'tcx>(
728-
visitor:&mutRegionResolutionVisitor<'tcx>,
728+
visitor:&mutScopeResolutionVisitor<'tcx>,
729729
expr:&hir::Expr<'_>,
730730
blk_id:Option<Scope>,
731731
){
@@ -782,7 +782,7 @@ fn resolve_local<'tcx>(
782782
}
783783
}
784784

785-
impl<'tcx>RegionResolutionVisitor<'tcx>{
785+
impl<'tcx>ScopeResolutionVisitor<'tcx>{
786786
/// Records the current parent (if any) as the parent of `child_scope`.
787787
/// Returns the depth of `child_scope`.
788788
fnrecord_child_scope(&mutself,child_scope:Scope) -> ScopeDepth{
@@ -838,7 +838,7 @@ impl<'tcx> RegionResolutionVisitor<'tcx> {
838838
}
839839
}
840840

841-
impl<'tcx>Visitor<'tcx>forRegionResolutionVisitor<'tcx>{
841+
impl<'tcx>Visitor<'tcx>forScopeResolutionVisitor<'tcx>{
842842
fnvisit_block(&mutself,b:&'tcxBlock<'tcx>){
843843
resolve_block(self, b);
844844
}
@@ -906,6 +906,10 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> {
906906
fnvisit_local(&mutself,l:&'tcxLetStmt<'tcx>){
907907
resolve_local(self,Some(l.pat), l.init)
908908
}
909+
fnvisit_inline_const(&mutself,c:&'tcx hir::ConstBlock){
910+
let body = self.tcx.hir().body(c.body);
911+
self.visit_body(body);
912+
}
909913
}
910914

911915
/// Per-body `region::ScopeTree`. The `DefId` should be the owner `DefId` for the body;
@@ -922,7 +926,7 @@ pub(crate) fn region_scope_tree(tcx: TyCtxt<'_>, def_id: DefId) -> &ScopeTree {
922926
}
923927

924928
let scope_tree = ifletSome(body) = tcx.hir().maybe_body_owned_by(def_id.expect_local()){
925-
letmut visitor = RegionResolutionVisitor{
929+
letmut visitor = ScopeResolutionVisitor{
926930
tcx,
927931
scope_tree:ScopeTree::default(),
928932
expr_and_pat_count:0,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @compile-flags: -Zlint-mir
2+
//@ check-pass
3+
4+
#![feature(inline_const_pat)]
5+
6+
fnmain(){
7+
match1{
8+
const{
9+
|| match0{
10+
x => 0,
11+
};
12+
0
13+
} => (),
14+
_ => (),
15+
}
16+
}

0 commit comments

Comments
 (0)