From 34f0c4502f6d64724478a5bf73c9ec616c3bde23 Mon Sep 17 00:00:00 2001 From: Ding Xiang Fei Date: Mon, 5 Sep 2022 14:24:13 +0800 Subject: [PATCH] supplement for the missing or incomplete comments --- compiler/rustc_mir_build/src/build/block.rs | 5 ++++- compiler/rustc_typeck/src/check/region.rs | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_mir_build/src/build/block.rs b/compiler/rustc_mir_build/src/build/block.rs index e2cd27d0339..d611f02333f 100644 --- a/compiler/rustc_mir_build/src/build/block.rs +++ b/compiler/rustc_mir_build/src/build/block.rs @@ -119,10 +119,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } => { let ignores_expr_result = matches!(pattern.kind, PatKind::Wild); this.block_context.push(BlockFrame::Statement { ignores_expr_result }); + + // Lower the `else` block first because its parent scope is actually + // enclosing the rest of the `let .. else ..` parts. + let else_block_span = this.thir[*else_block].span; // This place is not really used because this destination place // should never be used to take values at the end of the failure // block. - let else_block_span = this.thir[*else_block].span; let dummy_place = this.temp(this.tcx.types.never, else_block_span); let failure_entry = this.cfg.start_new_block(); let failure_block; diff --git a/compiler/rustc_typeck/src/check/region.rs b/compiler/rustc_typeck/src/check/region.rs index 594a7e450b8..46ffafc5a61 100644 --- a/compiler/rustc_typeck/src/check/region.rs +++ b/compiler/rustc_typeck/src/check/region.rs @@ -128,16 +128,24 @@ fn resolve_block<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, blk: &'tcx h match statement.kind { hir::StmtKind::Local(hir::Local { els: Some(els), .. }) => { // Let-else has a special lexical structure for variables. + // First we take a checkpoint of the current scope context here. let mut prev_cx = visitor.cx; + visitor.enter_scope(Scope { id: blk.hir_id.local_id, data: ScopeData::Remainder(FirstStatementIndex::new(i)), }); visitor.cx.var_parent = visitor.cx.parent; visitor.visit_stmt(statement); + // We need to back out temporarily to the last enclosing scope + // for the `else` block, so that even the temporaries receiving + // extended lifetime will be dropped inside this block. + // We are visiting the `else` block in this order so that + // the sequence of visits agree with the order in the default + // `hir::intravisit` visitor. mem::swap(&mut prev_cx, &mut visitor.cx); - // We need to back out temporarily and visitor.visit_block(els); + // From now on, we continue normally. visitor.cx = prev_cx; } hir::StmtKind::Local(..) | hir::StmtKind::Item(..) => {