Skip to content

Commit 3ebe9ab

Browse files
committed
rustc: use lowering helpers
This commit changes the lowering to stop creating HIR statements, expressions and patterns directly and instead uses the pre-existing helper functions.
1 parent 5e3b41e commit 3ebe9ab

File tree

1 file changed

+28
-55
lines changed

1 file changed

+28
-55
lines changed

src/librustc/hir/lowering.rs

+28-55
Original file line numberDiff line numberDiff line change
@@ -3039,12 +3039,15 @@ impl<'a> LoweringContext<'a> {
30393039
// Async function arguments are lowered into the closure body so that they are
30403040
// captured and so that the drop order matches the equivalent non-async functions.
30413041
//
3042+
// from:
3043+
//
30423044
// async fn foo(<pattern>: <ty>, <pattern>: <ty>, <pattern>: <ty>) {
30433045
// async move {
30443046
// }
30453047
// }
30463048
//
3047-
// // ...becomes...
3049+
// into:
3050+
//
30483051
// fn foo(__arg0: <ty>, __arg1: <ty>, __arg2: <ty>) {
30493052
// async move {
30503053
// let __arg2 = __arg2;
@@ -3076,61 +3079,29 @@ impl<'a> LoweringContext<'a> {
30763079
},
30773080
};
30783081

3082+
let desugared_span =
3083+
this.mark_span_with_reason(CompilerDesugaringKind::Async, span, None);
3084+
30793085
// Construct an argument representing `__argN: <ty>` to replace the argument of the
30803086
// async function.
30813087
//
30823088
// If this is the simple case, this argument will end up being the same as the
30833089
// original argument, but with a different pattern id.
3084-
let new_argument_id = this.next_id();
3085-
let desugared_span =
3086-
this.mark_span_with_reason(CompilerDesugaringKind::Async, span, None);
3090+
let (new_argument_pat, new_argument_id) = this.pat_ident(desugared_span, ident);
30873091
let new_argument = hir::Arg {
30883092
hir_id: argument.hir_id,
3089-
pat: P(hir::Pat {
3090-
hir_id: new_argument_id,
3091-
node: hir::PatKind::Binding(hir::BindingAnnotation::Unannotated,
3092-
new_argument_id, ident, None),
3093-
span: desugared_span,
3094-
}),
3095-
source: hir::ArgSource::AsyncFn,
3096-
};
3097-
3098-
let construct_stmt = |this: &mut LoweringContext<'_>, pat: P<hir::Pat>,
3099-
init_pat_id: hir::HirId| {
3100-
hir::Stmt {
3101-
hir_id: this.next_id(),
3102-
node: hir::StmtKind::Local(P(hir::Local {
3103-
pat,
3104-
// We explicitly do not specify the type for any statements. When the
3105-
// user's argument type is `impl Trait` then this would require the
3106-
// `impl_trait_in_bindings` feature to also be present for that same
3107-
// type to be valid in this binding. At the time of writing (13 Mar 19),
3108-
// `impl_trait_in_bindings` is not stable.
3109-
ty: None,
3110-
init: Some(P(hir::Expr {
3111-
span,
3112-
node: hir::ExprKind::Path(hir::QPath::Resolved(None, P(hir::Path {
3113-
span,
3114-
res: Res::Local(init_pat_id),
3115-
segments: hir_vec![ hir::PathSegment::from_ident(ident) ],
3116-
}))),
3117-
attrs: ThinVec::new(),
3118-
hir_id: this.next_id(),
3119-
})),
3120-
hir_id: this.next_id(),
3121-
span: desugared_span,
3122-
attrs: ThinVec::new(),
3123-
source: hir::LocalSource::AsyncFn,
3124-
})),
3125-
span: desugared_span,
3126-
}
3093+
pat: new_argument_pat,
3094+
source: hir::ArgSource::AsyncFn
31273095
};
31283096

31293097
let new_statements = if is_simple_argument {
31303098
// If this is the simple case, then we only insert one statement that is
31313099
// `let <pat> = <pat>;`. We re-use the original argument's pattern so that
31323100
// `HirId`s are densely assigned.
3133-
(construct_stmt(this, argument.pat, new_argument_id), None)
3101+
let expr = this.expr_ident(desugared_span, ident, new_argument_id);
3102+
let stmt = this.stmt_let_pat(
3103+
desugared_span, Some(P(expr)), argument.pat, hir::LocalSource::AsyncFn);
3104+
(stmt, None)
31343105
} else {
31353106
// If this is not the simple case, then we construct two statements:
31363107
//
@@ -3147,21 +3118,19 @@ impl<'a> LoweringContext<'a> {
31473118
// Construct the `let mut __argN = __argN;` statement. It must be a mut binding
31483119
// because the user may have specified a `ref mut` binding in the next
31493120
// statement.
3150-
let hir_id = this.next_id();
3151-
let move_stmt = construct_stmt(
3152-
this,
3153-
P(hir::Pat {
3154-
hir_id,
3155-
node: hir::PatKind::Binding(hir::BindingAnnotation::Mutable,
3156-
hir_id, ident, None),
3157-
span: desugared_span,
3158-
}),
3159-
new_argument_id,
3160-
);
3121+
let (move_pat, move_id) = this.pat_ident_binding_mode(
3122+
desugared_span, ident, hir::BindingAnnotation::Mutable);
3123+
let move_expr = this.expr_ident(desugared_span, ident, new_argument_id);
3124+
let move_stmt = this.stmt_let_pat(
3125+
desugared_span, Some(P(move_expr)), move_pat, hir::LocalSource::AsyncFn);
31613126

31623127
// Construct the `let <pat> = __argN;` statement. We re-use the original
31633128
// argument's pattern so that `HirId`s are densely assigned.
3164-
let pattern_stmt = construct_stmt(this, argument.pat, hir_id);
3129+
let pattern_expr = this.expr_ident(desugared_span, ident, move_id);
3130+
let pattern_stmt = this.stmt_let_pat(
3131+
desugared_span, Some(P(pattern_expr)), argument.pat,
3132+
hir::LocalSource::AsyncFn);
3133+
31653134
(move_stmt, Some(pattern_stmt))
31663135
};
31673136

@@ -5251,6 +5220,10 @@ impl<'a> LoweringContext<'a> {
52515220
}
52525221
}
52535222

5223+
fn arg(&mut self, hir_id: hir::HirId, pat: P<hir::Pat>, source: hir::ArgSource) -> hir::Arg {
5224+
hir::Arg { hir_id, pat, source }
5225+
}
5226+
52545227
fn stmt(&mut self, span: Span, node: hir::StmtKind) -> hir::Stmt {
52555228
hir::Stmt { span, node, hir_id: self.next_id() }
52565229
}

0 commit comments

Comments
 (0)