about summary refs log tree commit diff
path: root/compiler/rustc_ast_lowering/src/expr.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-12-15 03:28:55 +0000
committerbors <bors@rust-lang.org>2021-12-15 03:28:55 +0000
commit195e931b02b69bbce1bcf4632f4e2d5603ef006b (patch)
treef56ab523ec6f2a40e2ed60e80fc27f420bfe24f9 /compiler/rustc_ast_lowering/src/expr.rs
parentd594910a2da12f158477b4c7281716f535cfa3de (diff)
parent22fc403757b4e1bbe91375781b33fe51db8030c2 (diff)
downloadrust-195e931b02b69bbce1bcf4632f4e2d5603ef006b.tar.gz
rust-195e931b02b69bbce1bcf4632f4e2d5603ef006b.zip
Auto merge of #91945 - matthiaskrgr:rollup-jszf9zp, r=matthiaskrgr
Rollup of 7 pull requests

Successful merges:

 - #90939 (Tweak errors coming from `for`-loop, `?` and `.await` desugaring)
 - #91859 (Iterator::cycle() — document empty iterator special case)
 - #91868 (Use `OutputFilenames` to generate output file for `-Zllvm-time-trace`)
 - #91870 (Revert setting a default for the MACOSX_DEPLOYMENT_TARGET env var for linking)
 - #91881 (Stabilize `iter::zip`)
 - #91882 (Remove `in_band_lifetimes` from `rustc_typeck`)
 - #91940 (Update cargo)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_ast_lowering/src/expr.rs')
-rw-r--r--compiler/rustc_ast_lowering/src/expr.rs70
1 files changed, 55 insertions, 15 deletions
diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs
index c9578c2f50f..75a45a437e7 100644
--- a/compiler/rustc_ast_lowering/src/expr.rs
+++ b/compiler/rustc_ast_lowering/src/expr.rs
@@ -130,7 +130,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
                         hir::AsyncGeneratorKind::Block,
                         |this| this.with_new_scopes(|this| this.lower_block_expr(block)),
                     ),
-                ExprKind::Await(ref expr) => self.lower_expr_await(e.span, expr),
+                ExprKind::Await(ref expr) => {
+                    let span = if expr.span.hi() < e.span.hi() {
+                        expr.span.shrink_to_hi().with_hi(e.span.hi())
+                    } else {
+                        // this is a recovered `await expr`
+                        e.span
+                    };
+                    self.lower_expr_await(span, expr)
+                }
                 ExprKind::Closure(
                     capture_clause,
                     asyncness,
@@ -479,8 +487,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
         expr: &'hir hir::Expr<'hir>,
         overall_span: Span,
     ) -> &'hir hir::Expr<'hir> {
-        let constructor =
-            self.arena.alloc(self.expr_lang_item_path(method_span, lang_item, ThinVec::new()));
+        let constructor = self.arena.alloc(self.expr_lang_item_path(
+            method_span,
+            lang_item,
+            ThinVec::new(),
+            None,
+        ));
         self.expr_call(overall_span, constructor, std::slice::from_ref(expr))
     }
 
@@ -584,8 +596,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
         // `future::from_generator`:
         let unstable_span =
             self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone());
-        let gen_future =
-            self.expr_lang_item_path(unstable_span, hir::LangItem::FromGenerator, ThinVec::new());
+        let gen_future = self.expr_lang_item_path(
+            unstable_span,
+            hir::LangItem::FromGenerator,
+            ThinVec::new(),
+            None,
+        );
 
         // `future::from_generator(generator)`:
         hir::ExprKind::Call(self.arena.alloc(gen_future), arena_vec![self; generator])
@@ -607,6 +623,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
     /// }
     /// ```
     fn lower_expr_await(&mut self, await_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
+        let dot_await_span = expr.span.shrink_to_hi().to(await_span);
         match self.generator_kind {
             Some(hir::GeneratorKind::Async(_)) => {}
             Some(hir::GeneratorKind::Gen) | None => {
@@ -623,13 +640,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
                 err.emit();
             }
         }
-        let span = self.mark_span_with_reason(DesugaringKind::Await, await_span, None);
+        let span = self.mark_span_with_reason(DesugaringKind::Await, dot_await_span, None);
         let gen_future_span = self.mark_span_with_reason(
             DesugaringKind::Await,
             await_span,
             self.allow_gen_future.clone(),
         );
         let expr = self.lower_expr_mut(expr);
+        let expr_hir_id = expr.hir_id;
 
         let pinned_ident = Ident::with_dummy_span(sym::pinned);
         let (pinned_pat, pinned_pat_hid) =
@@ -656,16 +674,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
                 span,
                 hir::LangItem::PinNewUnchecked,
                 arena_vec![self; ref_mut_pinned],
+                Some(expr_hir_id),
             );
             let get_context = self.expr_call_lang_item_fn_mut(
                 gen_future_span,
                 hir::LangItem::GetContext,
                 arena_vec![self; task_context],
+                Some(expr_hir_id),
             );
             let call = self.expr_call_lang_item_fn(
                 span,
                 hir::LangItem::FuturePoll,
                 arena_vec![self; new_unchecked, get_context],
+                Some(expr_hir_id),
             );
             self.arena.alloc(self.expr_unsafe(call))
         };
@@ -678,18 +699,28 @@ impl<'hir> LoweringContext<'_, 'hir> {
             let (x_pat, x_pat_hid) = self.pat_ident(span, x_ident);
             let x_expr = self.expr_ident(span, x_ident, x_pat_hid);
             let ready_field = self.single_pat_field(span, x_pat);
-            let ready_pat = self.pat_lang_item_variant(span, hir::LangItem::PollReady, ready_field);
+            let ready_pat = self.pat_lang_item_variant(
+                span,
+                hir::LangItem::PollReady,
+                ready_field,
+                Some(expr_hir_id),
+            );
             let break_x = self.with_loop_scope(loop_node_id, move |this| {
                 let expr_break =
                     hir::ExprKind::Break(this.lower_loop_destination(None), Some(x_expr));
-                this.arena.alloc(this.expr(await_span, expr_break, ThinVec::new()))
+                this.arena.alloc(this.expr(span, expr_break, ThinVec::new()))
             });
             self.arm(ready_pat, break_x)
         };
 
         // `::std::task::Poll::Pending => {}`
         let pending_arm = {
-            let pending_pat = self.pat_lang_item_variant(span, hir::LangItem::PollPending, &[]);
+            let pending_pat = self.pat_lang_item_variant(
+                span,
+                hir::LangItem::PollPending,
+                &[],
+                Some(expr_hir_id),
+            );
             let empty_block = self.expr_block_empty(span);
             self.arm(pending_pat, empty_block)
         };
@@ -709,7 +740,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
             let unit = self.expr_unit(span);
             let yield_expr = self.expr(
                 span,
-                hir::ExprKind::Yield(unit, hir::YieldSource::Await { expr: Some(expr.hir_id) }),
+                hir::ExprKind::Yield(unit, hir::YieldSource::Await { expr: Some(expr_hir_id) }),
                 ThinVec::new(),
             );
             let yield_expr = self.arena.alloc(yield_expr);
@@ -756,6 +787,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
             into_future_span,
             hir::LangItem::IntoFutureIntoFuture,
             arena_vec![self; expr],
+            Some(expr_hir_id),
         );
 
         // match <into_future_expr> {
@@ -1160,7 +1192,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
     fn lower_expr_range_closed(&mut self, span: Span, e1: &Expr, e2: &Expr) -> hir::ExprKind<'hir> {
         let e1 = self.lower_expr_mut(e1);
         let e2 = self.lower_expr_mut(e2);
-        let fn_path = hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, self.lower_span(span));
+        let fn_path =
+            hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, self.lower_span(span), None);
         let fn_expr =
             self.arena.alloc(self.expr(span, hir::ExprKind::Path(fn_path), ThinVec::new()));
         hir::ExprKind::Call(fn_expr, arena_vec![self; e1, e2])
@@ -1194,7 +1227,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
         );
 
         hir::ExprKind::Struct(
-            self.arena.alloc(hir::QPath::LangItem(lang_item, self.lower_span(span))),
+            self.arena.alloc(hir::QPath::LangItem(lang_item, self.lower_span(span), None)),
             fields,
             None,
         )
@@ -1389,6 +1422,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
                 head_span,
                 hir::LangItem::IteratorNext,
                 arena_vec![self; ref_mut_iter],
+                None,
             );
             let arms = arena_vec![self; none_arm, some_arm];
 
@@ -1417,6 +1451,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
                 head_span,
                 hir::LangItem::IntoIterIntoIter,
                 arena_vec![self; head],
+                None,
             )
         };
 
@@ -1472,6 +1507,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
                 unstable_span,
                 hir::LangItem::TryTraitBranch,
                 arena_vec![self; sub_expr],
+                None,
             )
         };
 
@@ -1628,8 +1664,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
         span: Span,
         lang_item: hir::LangItem,
         args: &'hir [hir::Expr<'hir>],
+        hir_id: Option<hir::HirId>,
     ) -> hir::Expr<'hir> {
-        let path = self.arena.alloc(self.expr_lang_item_path(span, lang_item, ThinVec::new()));
+        let path =
+            self.arena.alloc(self.expr_lang_item_path(span, lang_item, ThinVec::new(), hir_id));
         self.expr_call_mut(span, path, args)
     }
 
@@ -1638,8 +1676,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
         span: Span,
         lang_item: hir::LangItem,
         args: &'hir [hir::Expr<'hir>],
+        hir_id: Option<hir::HirId>,
     ) -> &'hir hir::Expr<'hir> {
-        self.arena.alloc(self.expr_call_lang_item_fn_mut(span, lang_item, args))
+        self.arena.alloc(self.expr_call_lang_item_fn_mut(span, lang_item, args, hir_id))
     }
 
     fn expr_lang_item_path(
@@ -1647,10 +1686,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
         span: Span,
         lang_item: hir::LangItem,
         attrs: AttrVec,
+        hir_id: Option<hir::HirId>,
     ) -> hir::Expr<'hir> {
         self.expr(
             span,
-            hir::ExprKind::Path(hir::QPath::LangItem(lang_item, self.lower_span(span))),
+            hir::ExprKind::Path(hir::QPath::LangItem(lang_item, self.lower_span(span), hir_id)),
             attrs,
         )
     }