Skip to content

Commit

Permalink
rustc: construct statement vector directly
Browse files Browse the repository at this point in the history
This commit simplifies the previous logic to construct the statement
vector directly rather than constructing a `Vec` of
`(hir::Stmt, Option<hir::Stmt>)` first.
  • Loading branch information
davidtwco committed Jun 3, 2019
1 parent 3ebe9ab commit 3c7e0eb
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3034,7 +3034,7 @@ impl<'a> LoweringContext<'a> {

self.lower_body(|this| {
let mut arguments: Vec<hir::Arg> = Vec::new();
let mut statements: Vec<(hir::Stmt, Option<hir::Stmt>)> = Vec::new();
let mut statements: Vec<hir::Stmt> = Vec::new();

// Async function arguments are lowered into the closure body so that they are
// captured and so that the drop order matches the equivalent non-async functions.
Expand Down Expand Up @@ -3094,14 +3094,14 @@ impl<'a> LoweringContext<'a> {
source: hir::ArgSource::AsyncFn
};

let new_statements = if is_simple_argument {
if is_simple_argument {
// If this is the simple case, then we only insert one statement that is
// `let <pat> = <pat>;`. We re-use the original argument's pattern so that
// `HirId`s are densely assigned.
let expr = this.expr_ident(desugared_span, ident, new_argument_id);
let stmt = this.stmt_let_pat(
desugared_span, Some(P(expr)), argument.pat, hir::LocalSource::AsyncFn);
(stmt, None)
statements.push(stmt);
} else {
// If this is not the simple case, then we construct two statements:
//
Expand Down Expand Up @@ -3131,26 +3131,17 @@ impl<'a> LoweringContext<'a> {
desugared_span, Some(P(pattern_expr)), argument.pat,
hir::LocalSource::AsyncFn);

(move_stmt, Some(pattern_stmt))
statements.push(move_stmt);
statements.push(pattern_stmt);
};

arguments.push(new_argument);
statements.push(new_statements);
}

let async_expr = this.make_async_expr(
CaptureBy::Value, closure_id, None, body.span,
|this| {
let mut stmts = vec![];
for (move_stmt, pattern_stmt) in statements.drain(..) {
// Insert the `let __argN = __argN` statement first.
stmts.push(move_stmt);
// Then insert the `let <pat> = __argN` statement, if there is one.
if let Some(pattern_stmt) = pattern_stmt {
stmts.push(pattern_stmt);
}
}
let body = this.lower_block_with_stmts(body, false, stmts);
let body = this.lower_block_with_stmts(body, false, statements);
this.expr_block(body, ThinVec::new())
});
(HirVec::from(arguments), this.expr(body.span, async_expr, ThinVec::new()))
Expand Down Expand Up @@ -5220,10 +5211,6 @@ impl<'a> LoweringContext<'a> {
}
}

fn arg(&mut self, hir_id: hir::HirId, pat: P<hir::Pat>, source: hir::ArgSource) -> hir::Arg {
hir::Arg { hir_id, pat, source }
}

fn stmt(&mut self, span: Span, node: hir::StmtKind) -> hir::Stmt {
hir::Stmt { span, node, hir_id: self.next_id() }
}
Expand Down

0 comments on commit 3c7e0eb

Please sign in to comment.