Skip to content

Commit 44ddbc5

Browse files
committed
Correct lowering order to avoid ICE after rebase.
This commit changes the order that arguments and bodies of async functions are lowered so that when the body attempts to `lower_def` of a upvar then the id has already been assigned by lowering the argument first.
1 parent 9d7da82 commit 44ddbc5

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/librustc/hir/lowering.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -809,12 +809,10 @@ impl<'a> LoweringContext<'a> {
809809
})
810810
}
811811

812-
fn record_body(&mut self, value: hir::Expr, decl: Option<&FnDecl>) -> hir::BodyId {
812+
fn record_body(&mut self, value: hir::Expr, arguments: HirVec<hir::Arg>) -> hir::BodyId {
813813
let body = hir::Body {
814-
arguments: decl.map_or(hir_vec![], |decl| {
815-
decl.inputs.iter().map(|x| self.lower_arg(x)).collect()
816-
}),
817814
is_generator: self.is_generator,
815+
arguments,
818816
value,
819817
};
820818
let id = body.id();
@@ -1137,11 +1135,10 @@ impl<'a> LoweringContext<'a> {
11371135
capture_clause: CaptureBy,
11381136
closure_node_id: NodeId,
11391137
ret_ty: Option<&Ty>,
1138+
span: Span,
11401139
body: impl FnOnce(&mut LoweringContext<'_>) -> hir::Expr,
11411140
) -> hir::ExprKind {
11421141
let prev_is_generator = mem::replace(&mut self.is_generator, true);
1143-
let body_expr = body(self);
1144-
let span = body_expr.span;
11451142
let output = match ret_ty {
11461143
Some(ty) => FunctionRetTy::Ty(P(ty.clone())),
11471144
None => FunctionRetTy::Default(span),
@@ -1151,7 +1148,11 @@ impl<'a> LoweringContext<'a> {
11511148
output,
11521149
c_variadic: false
11531150
};
1154-
let body_id = self.record_body(body_expr, Some(&decl));
1151+
// Lower the arguments before the body otherwise the body will call `lower_def` expecting
1152+
// the argument to have been assigned an id already.
1153+
let arguments = self.lower_args(Some(&decl));
1154+
let body_expr = body(self);
1155+
let body_id = self.record_body(body_expr, arguments);
11551156
self.is_generator = prev_is_generator;
11561157

11571158
let capture_clause = self.lower_capture_clause(capture_clause);
@@ -1182,8 +1183,9 @@ impl<'a> LoweringContext<'a> {
11821183
F: FnOnce(&mut LoweringContext<'_>) -> hir::Expr,
11831184
{
11841185
let prev = mem::replace(&mut self.is_generator, false);
1186+
let arguments = self.lower_args(decl);
11851187
let result = f(self);
1186-
let r = self.record_body(result, decl);
1188+
let r = self.record_body(result, arguments);
11871189
self.is_generator = prev;
11881190
return r;
11891191
}
@@ -2267,6 +2269,10 @@ impl<'a> LoweringContext<'a> {
22672269
}
22682270
}
22692271

2272+
fn lower_args(&mut self, decl: Option<&FnDecl>) -> HirVec<hir::Arg> {
2273+
decl.map_or(hir_vec![], |decl| decl.inputs.iter().map(|x| self.lower_arg(x)).collect())
2274+
}
2275+
22702276
fn lower_arg(&mut self, arg: &Arg) -> hir::Arg {
22712277
let LoweredNodeId { node_id: _, hir_id } = self.lower_node_id(arg.id);
22722278
hir::Arg {
@@ -3045,7 +3051,7 @@ impl<'a> LoweringContext<'a> {
30453051
}
30463052

30473053
let async_expr = this.make_async_expr(
3048-
CaptureBy::Value, *closure_id, None,
3054+
CaptureBy::Value, *closure_id, None, body.span,
30493055
|this| {
30503056
let body = this.lower_block(&body, false);
30513057
this.expr_block(body, ThinVec::new())
@@ -4190,7 +4196,7 @@ impl<'a> LoweringContext<'a> {
41904196
hir::MatchSource::Normal,
41914197
),
41924198
ExprKind::Async(capture_clause, closure_node_id, ref block) => {
4193-
self.make_async_expr(capture_clause, closure_node_id, None, |this| {
4199+
self.make_async_expr(capture_clause, closure_node_id, None, block.span, |this| {
41944200
this.with_new_scopes(|this| {
41954201
let block = this.lower_block(block, false);
41964202
this.expr_block(block, ThinVec::new())
@@ -4236,7 +4242,7 @@ impl<'a> LoweringContext<'a> {
42364242
Some(&**ty)
42374243
} else { None };
42384244
let async_body = this.make_async_expr(
4239-
capture_clause, *closure_id, async_ret_ty,
4245+
capture_clause, *closure_id, async_ret_ty, body.span,
42404246
|this| {
42414247
this.with_new_scopes(|this| this.lower_expr(body))
42424248
});

0 commit comments

Comments
 (0)