Skip to content

Commit 808f983

Browse files
authored
Rollup merge of rust-lang#63432 - Centril:simplify-lowering, r=eddyb
Cleanup & Simplify stuff in lowering Closes rust-lang#60253 as a byproduct. It turns out that it is in fact necessary to have a `DropTemps(...)` around the `match_expr` and there is a test (https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-13304.rs) which fails without that. r? @eddyb
2 parents 219336a + 8758d7f commit 808f983

File tree

1 file changed

+34
-43
lines changed

1 file changed

+34
-43
lines changed

src/librustc/hir/lowering.rs

+34-43
Original file line numberDiff line numberDiff line change
@@ -4440,23 +4440,23 @@ impl<'a> LoweringContext<'a> {
44404440
})
44414441
}
44424442

4443+
fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> HirVec<hir::Expr> {
4444+
exprs.iter().map(|x| self.lower_expr(x)).collect()
4445+
}
4446+
44434447
fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
44444448
let kind = match e.node {
44454449
ExprKind::Box(ref inner) => hir::ExprKind::Box(P(self.lower_expr(inner))),
4446-
ExprKind::Array(ref exprs) => {
4447-
hir::ExprKind::Array(exprs.iter().map(|x| self.lower_expr(x)).collect())
4448-
}
4450+
ExprKind::Array(ref exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
44494451
ExprKind::Repeat(ref expr, ref count) => {
44504452
let expr = P(self.lower_expr(expr));
44514453
let count = self.lower_anon_const(count);
44524454
hir::ExprKind::Repeat(expr, count)
44534455
}
4454-
ExprKind::Tup(ref elts) => {
4455-
hir::ExprKind::Tup(elts.iter().map(|x| self.lower_expr(x)).collect())
4456-
}
4456+
ExprKind::Tup(ref elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),
44574457
ExprKind::Call(ref f, ref args) => {
44584458
let f = P(self.lower_expr(f));
4459-
hir::ExprKind::Call(f, args.iter().map(|x| self.lower_expr(x)).collect())
4459+
hir::ExprKind::Call(f, self.lower_exprs(args))
44604460
}
44614461
ExprKind::MethodCall(ref seg, ref args) => {
44624462
let hir_seg = P(self.lower_path_segment(
@@ -4468,7 +4468,7 @@ impl<'a> LoweringContext<'a> {
44684468
ImplTraitContext::disallowed(),
44694469
None,
44704470
));
4471-
let args = args.iter().map(|x| self.lower_expr(x)).collect();
4471+
let args = self.lower_exprs(args);
44724472
hir::ExprKind::MethodCall(hir_seg, seg.ident.span, args)
44734473
}
44744474
ExprKind::Binary(binop, ref lhs, ref rhs) => {
@@ -5042,17 +5042,9 @@ impl<'a> LoweringContext<'a> {
50425042
));
50435043
let arms = hir_vec![pat_arm, break_arm];
50445044

5045-
P(self.expr(
5046-
head_sp,
5047-
hir::ExprKind::Match(
5048-
next_expr,
5049-
arms,
5050-
hir::MatchSource::ForLoopDesugar
5051-
),
5052-
ThinVec::new(),
5053-
))
5045+
self.expr_match(head_sp, next_expr, arms, hir::MatchSource::ForLoopDesugar)
50545046
};
5055-
let match_stmt = self.stmt(head_sp, hir::StmtKind::Expr(match_expr));
5047+
let match_stmt = self.stmt_expr(head_sp, match_expr);
50565048

50575049
let next_expr = P(self.expr_ident(head_sp, next_ident, next_pat_hid));
50585050

@@ -5076,8 +5068,8 @@ impl<'a> LoweringContext<'a> {
50765068
);
50775069

50785070
let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
5079-
let body_expr = P(self.expr_block(body_block, ThinVec::new()));
5080-
let body_stmt = self.stmt(body.span, hir::StmtKind::Expr(body_expr));
5071+
let body_expr = self.expr_block(body_block, ThinVec::new());
5072+
let body_stmt = self.stmt_expr(body.span, body_expr);
50815073

50825074
let loop_block = P(self.block_all(
50835075
e.span,
@@ -5120,8 +5112,10 @@ impl<'a> LoweringContext<'a> {
51205112
));
51215113

51225114
// This is effectively `{ let _result = ...; _result }`.
5123-
// The construct was introduced in #21984.
5124-
// FIXME(60253): Is this still necessary?
5115+
// The construct was introduced in #21984 and is necessary to make sure that
5116+
// temporaries in the `head` expression are dropped and do not leak to the
5117+
// surrounding scope of the `match` since the `match` is not a terminating scope.
5118+
//
51255119
// Also, add the attributes to the outer returned expr node.
51265120
return self.expr_drop_temps(head_sp, match_expr, e.attrs.clone())
51275121
}
@@ -5247,7 +5241,7 @@ impl<'a> LoweringContext<'a> {
52475241
}
52485242

52495243
fn lower_stmt(&mut self, s: &Stmt) -> SmallVec<[hir::Stmt; 1]> {
5250-
smallvec![match s.node {
5244+
let node = match s.node {
52515245
StmtKind::Local(ref l) => {
52525246
let (l, item_ids) = self.lower_local(l);
52535247
let mut ids: SmallVec<[hir::Stmt; 1]> = item_ids
@@ -5284,21 +5278,14 @@ impl<'a> LoweringContext<'a> {
52845278
})
52855279
.collect();
52865280
}
5287-
StmtKind::Expr(ref e) => {
5288-
hir::Stmt {
5289-
hir_id: self.lower_node_id(s.id),
5290-
node: hir::StmtKind::Expr(P(self.lower_expr(e))),
5291-
span: s.span,
5292-
}
5293-
},
5294-
StmtKind::Semi(ref e) => {
5295-
hir::Stmt {
5296-
hir_id: self.lower_node_id(s.id),
5297-
node: hir::StmtKind::Semi(P(self.lower_expr(e))),
5298-
span: s.span,
5299-
}
5300-
},
5281+
StmtKind::Expr(ref e) => hir::StmtKind::Expr(P(self.lower_expr(e))),
5282+
StmtKind::Semi(ref e) => hir::StmtKind::Semi(P(self.lower_expr(e))),
53015283
StmtKind::Mac(..) => panic!("Shouldn't exist here"),
5284+
};
5285+
smallvec![hir::Stmt {
5286+
hir_id: self.lower_node_id(s.id),
5287+
node,
5288+
span: s.span,
53025289
}]
53035290
}
53045291

@@ -5560,6 +5547,10 @@ impl<'a> LoweringContext<'a> {
55605547
hir::Stmt { span, node, hir_id: self.next_id() }
55615548
}
55625549

5550+
fn stmt_expr(&mut self, span: Span, expr: hir::Expr) -> hir::Stmt {
5551+
self.stmt(span, hir::StmtKind::Expr(P(expr)))
5552+
}
5553+
55635554
fn stmt_let_pat(
55645555
&mut self,
55655556
attrs: ThinVec<Attribute>,
@@ -6053,23 +6044,23 @@ impl<'a> LoweringContext<'a> {
60536044
};
60546045

60556046
let match_stmt = {
6056-
let match_expr = P(self.expr_match(
6047+
let match_expr = self.expr_match(
60576048
span,
60586049
poll_expr,
60596050
hir_vec![ready_arm, pending_arm],
60606051
hir::MatchSource::AwaitDesugar,
6061-
));
6062-
self.stmt(span, hir::StmtKind::Expr(match_expr))
6052+
);
6053+
self.stmt_expr(span, match_expr)
60636054
};
60646055

60656056
let yield_stmt = {
60666057
let unit = self.expr_unit(span);
6067-
let yield_expr = P(self.expr(
6058+
let yield_expr = self.expr(
60686059
span,
60696060
hir::ExprKind::Yield(P(unit), hir::YieldSource::Await),
60706061
ThinVec::new(),
6071-
));
6072-
self.stmt(span, hir::StmtKind::Expr(yield_expr))
6062+
);
6063+
self.stmt_expr(span, yield_expr)
60736064
};
60746065

60756066
let loop_block = P(self.block_all(

0 commit comments

Comments
 (0)