Skip to content

Commit 1b87ce0

Browse files
reorder nesting scopes and declare bindings without drop schedule
1 parent 750bd1a commit 1b87ce0

File tree

5 files changed

+122
-65
lines changed

5 files changed

+122
-65
lines changed

Diff for: compiler/rustc_mir_build/src/build/block.rs

+95-22
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,99 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
109109
)
110110
);
111111
}
112+
StmtKind::Let {
113+
remainder_scope,
114+
init_scope,
115+
pattern,
116+
initializer: Some(initializer),
117+
lint_level,
118+
else_block: Some(else_block),
119+
} => {
120+
let ignores_expr_result = matches!(pattern.kind, PatKind::Wild);
121+
this.block_context.push(BlockFrame::Statement { ignores_expr_result });
122+
// This place is not really used because this destination place
123+
// should never be used to take values at the end of the failure
124+
// block.
125+
let else_block_span = this.thir[*else_block].span;
126+
let dummy_place = this.temp(this.tcx.types.never, else_block_span);
127+
let failure_entry = this.cfg.start_new_block();
128+
let failure_block;
129+
unpack!(
130+
failure_block = this.ast_block(
131+
dummy_place,
132+
failure_entry,
133+
*else_block,
134+
this.source_info(else_block_span),
135+
)
136+
);
137+
this.cfg.terminate(
138+
failure_block,
139+
this.source_info(else_block_span),
140+
TerminatorKind::Unreachable,
141+
);
142+
143+
// Declare the bindings, which may create a source scope.
144+
let remainder_span = remainder_scope.span(this.tcx, this.region_scope_tree);
145+
this.push_scope((*remainder_scope, source_info));
146+
let_scope_stack.push(remainder_scope);
147+
148+
let visibility_scope =
149+
Some(this.new_source_scope(remainder_span, LintLevel::Inherited, None));
150+
151+
let init = &this.thir[*initializer];
152+
let initializer_span = init.span;
153+
this.declare_bindings(
154+
visibility_scope,
155+
remainder_span,
156+
pattern,
157+
ArmHasGuard(false),
158+
Some((None, initializer_span)),
159+
);
160+
this.visit_primary_bindings(
161+
pattern,
162+
UserTypeProjections::none(),
163+
&mut |this, _, _, _, node, span, _, _| {
164+
this.storage_live_binding(block, node, span, OutsideGuard, false);
165+
},
166+
);
167+
let failure = unpack!(
168+
block = this.in_opt_scope(
169+
opt_destruction_scope.map(|de| (de, source_info)),
170+
|this| {
171+
let scope = (*init_scope, source_info);
172+
this.in_scope(scope, *lint_level, |this| {
173+
this.ast_let_else(
174+
block,
175+
init,
176+
initializer_span,
177+
*else_block,
178+
&last_remainder_scope,
179+
pattern,
180+
)
181+
})
182+
}
183+
)
184+
);
185+
this.cfg.goto(failure, source_info, failure_entry);
186+
187+
if let Some(source_scope) = visibility_scope {
188+
this.source_scope = source_scope;
189+
}
190+
last_remainder_scope = *remainder_scope;
191+
}
192+
StmtKind::Let { init_scope, initializer: None, else_block: Some(_), .. } => {
193+
span_bug!(
194+
init_scope.span(this.tcx, this.region_scope_tree),
195+
"initializer is missing, but else block is present in this let binding",
196+
)
197+
}
112198
StmtKind::Let {
113199
remainder_scope,
114200
init_scope,
115201
ref pattern,
116202
initializer,
117203
lint_level,
118-
else_block,
204+
else_block: None,
119205
} => {
120206
let ignores_expr_result = matches!(pattern.kind, PatKind::Wild);
121207
this.block_context.push(BlockFrame::Statement { ignores_expr_result });
@@ -141,27 +227,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
141227
|this| {
142228
let scope = (*init_scope, source_info);
143229
this.in_scope(scope, *lint_level, |this| {
144-
if let Some(else_block) = else_block {
145-
this.ast_let_else(
146-
block,
147-
init,
148-
initializer_span,
149-
*else_block,
150-
visibility_scope,
151-
last_remainder_scope,
152-
remainder_span,
153-
pattern,
154-
)
155-
} else {
156-
this.declare_bindings(
157-
visibility_scope,
158-
remainder_span,
159-
pattern,
160-
ArmHasGuard(false),
161-
Some((None, initializer_span)),
162-
);
163-
this.expr_into_pattern(block, pattern, init) // irrefutable pattern
164-
}
230+
this.declare_bindings(
231+
visibility_scope,
232+
remainder_span,
233+
pattern,
234+
ArmHasGuard(false),
235+
Some((None, initializer_span)),
236+
);
237+
this.expr_into_pattern(block, &pattern, init) // irrefutable pattern
165238
})
166239
},
167240
)

Diff for: compiler/rustc_mir_build/src/build/matches/mod.rs

+6-34
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
699699
self.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(local_id) });
700700
// Although there is almost always scope for given variable in corner cases
701701
// like #92893 we might get variable with no scope.
702-
if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) && schedule_drop{
702+
if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) && schedule_drop {
703703
self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
704704
}
705705
Place::from(local_id)
@@ -2274,23 +2274,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
22742274
init: &Expr<'tcx>,
22752275
initializer_span: Span,
22762276
else_block: BlockId,
2277-
visibility_scope: Option<SourceScope>,
2278-
remainder_scope: region::Scope,
2279-
remainder_span: Span,
2277+
let_else_scope: &region::Scope,
22802278
pattern: &Pat<'tcx>,
2281-
) -> BlockAnd<()> {
2279+
) -> BlockAnd<BasicBlock> {
22822280
let else_block_span = self.thir[else_block].span;
2283-
let (matching, failure) = self.in_if_then_scope(remainder_scope, |this| {
2281+
let (matching, failure) = self.in_if_then_scope(*let_else_scope, |this| {
22842282
let scrutinee = unpack!(block = this.lower_scrutinee(block, init, initializer_span));
22852283
let pat = Pat { ty: init.ty, span: else_block_span, kind: PatKind::Wild };
22862284
let mut wildcard = Candidate::new(scrutinee.clone(), &pat, false);
2287-
this.declare_bindings(
2288-
visibility_scope,
2289-
remainder_span,
2290-
pattern,
2291-
ArmHasGuard(false),
2292-
Some((None, initializer_span)),
2293-
);
22942285
let mut candidate = Candidate::new(scrutinee.clone(), pattern, false);
22952286
let fake_borrow_temps = this.lower_match_tree(
22962287
block,
@@ -2321,28 +2312,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
23212312
None,
23222313
None,
23232314
);
2324-
this.break_for_else(failure, remainder_scope, this.source_info(initializer_span));
2315+
this.break_for_else(failure, *let_else_scope, this.source_info(initializer_span));
23252316
matching.unit()
23262317
});
2327-
2328-
// This place is not really used because this destination place
2329-
// should never be used to take values at the end of the failure
2330-
// block.
2331-
let dummy_place = self.temp(self.tcx.types.never, else_block_span);
2332-
let failure_block;
2333-
unpack!(
2334-
failure_block = self.ast_block(
2335-
dummy_place,
2336-
failure,
2337-
else_block,
2338-
self.source_info(else_block_span),
2339-
)
2340-
);
2341-
self.cfg.terminate(
2342-
failure_block,
2343-
self.source_info(else_block_span),
2344-
TerminatorKind::Unreachable,
2345-
);
2346-
matching.unit()
2318+
matching.and(failure)
23472319
}
23482320
}

Diff for: compiler/rustc_typeck/src/check/region.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ fn resolve_block<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, blk: &'tcx h
126126

127127
for (i, statement) in blk.stmts.iter().enumerate() {
128128
match statement.kind {
129+
hir::StmtKind::Local(hir::Local { els: Some(els), .. }) => {
130+
// Let-else has a special lexical structure for variables.
131+
let mut prev_cx = visitor.cx;
132+
visitor.enter_scope(Scope {
133+
id: blk.hir_id.local_id,
134+
data: ScopeData::Remainder(FirstStatementIndex::new(i)),
135+
});
136+
visitor.cx.var_parent = visitor.cx.parent;
137+
visitor.visit_stmt(statement);
138+
mem::swap(&mut prev_cx, &mut visitor.cx);
139+
// We need to back out temporarily and
140+
visitor.visit_block(els);
141+
visitor.cx = prev_cx;
142+
}
129143
hir::StmtKind::Local(..) | hir::StmtKind::Item(..) => {
130144
// Each declaration introduces a subscope for bindings
131145
// introduced by the declaration; this subscope covers a
@@ -138,10 +152,10 @@ fn resolve_block<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, blk: &'tcx h
138152
data: ScopeData::Remainder(FirstStatementIndex::new(i)),
139153
});
140154
visitor.cx.var_parent = visitor.cx.parent;
155+
visitor.visit_stmt(statement)
141156
}
142-
hir::StmtKind::Expr(..) | hir::StmtKind::Semi(..) => {}
157+
hir::StmtKind::Expr(..) | hir::StmtKind::Semi(..) => visitor.visit_stmt(statement),
143158
}
144-
visitor.visit_stmt(statement)
145159
}
146160
walk_list!(visitor, visit_expr, &blk.expr);
147161
}
@@ -460,7 +474,6 @@ fn resolve_local<'tcx>(
460474
visitor: &mut RegionResolutionVisitor<'tcx>,
461475
pat: Option<&'tcx hir::Pat<'tcx>>,
462476
init: Option<&'tcx hir::Expr<'tcx>>,
463-
els: Option<&'tcx hir::Block<'tcx>>,
464477
) {
465478
debug!("resolve_local(pat={:?}, init={:?})", pat, init);
466479

@@ -547,9 +560,6 @@ fn resolve_local<'tcx>(
547560
if let Some(pat) = pat {
548561
visitor.visit_pat(pat);
549562
}
550-
if let Some(els) = els {
551-
visitor.visit_block(els);
552-
}
553563

554564
/// Returns `true` if `pat` match the `P&` non-terminal.
555565
///
@@ -766,7 +776,7 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> {
766776
// (i.e., `'static`), which means that after `g` returns, it drops,
767777
// and all the associated destruction scope rules apply.
768778
self.cx.var_parent = None;
769-
resolve_local(self, None, Some(&body.value), None);
779+
resolve_local(self, None, Some(&body.value));
770780
}
771781

772782
if body.generator_kind.is_some() {
@@ -793,7 +803,7 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> {
793803
resolve_expr(self, ex);
794804
}
795805
fn visit_local(&mut self, l: &'tcx Local<'tcx>) {
796-
resolve_local(self, Some(&l.pat), l.init, l.els)
806+
resolve_local(self, Some(&l.pat), l.init)
797807
}
798808
}
799809

Diff for: src/test/ui/async-await/async-await-let-else.no-drop-tracking.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ LL | bar2(Rc::new(())).await
3535
| |
3636
| has type `Rc<()>` which is not `Send`
3737
LL | };
38-
| - `Rc::new(())` is later dropped here
38+
LL | }
39+
| - `Rc::new(())` is later dropped here
3940
note: required by a bound in `is_send`
4041
--> $DIR/async-await-let-else.rs:19:15
4142
|

Diff for: src/test/ui/let-else/let-else-temporary-lifetime.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-pass
2+
// compile-flags: -Zvalidate-mir
23
#![feature(let_else)]
34

45
use std::fmt::Display;

0 commit comments

Comments
 (0)