Skip to content

Commit 7356e28

Browse files
committed
Tweak expr.await desugaring Span
Fix #93074
1 parent 5e57faa commit 7356e28

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -630,18 +630,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
630630
/// }
631631
/// }
632632
/// ```
633-
fn lower_expr_await(&mut self, await_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
634-
let dot_await_span = expr.span.shrink_to_hi().to(await_span);
633+
fn lower_expr_await(&mut self, dot_await_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
634+
let full_span = expr.span.to(dot_await_span);
635635
match self.generator_kind {
636636
Some(hir::GeneratorKind::Async(_)) => {}
637637
Some(hir::GeneratorKind::Gen) | None => {
638638
let mut err = struct_span_err!(
639639
self.sess,
640-
await_span,
640+
dot_await_span,
641641
E0728,
642642
"`await` is only allowed inside `async` functions and blocks"
643643
);
644-
err.span_label(await_span, "only allowed inside `async` functions and blocks");
644+
err.span_label(dot_await_span, "only allowed inside `async` functions and blocks");
645645
if let Some(item_sp) = self.current_item {
646646
err.span_label(item_sp, "this is not `async`");
647647
}
@@ -651,7 +651,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
651651
let span = self.mark_span_with_reason(DesugaringKind::Await, dot_await_span, None);
652652
let gen_future_span = self.mark_span_with_reason(
653653
DesugaringKind::Await,
654-
await_span,
654+
full_span,
655655
self.allow_gen_future.clone(),
656656
);
657657
let expr = self.lower_expr_mut(expr);
@@ -704,9 +704,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
704704
let loop_hir_id = self.lower_node_id(loop_node_id);
705705
let ready_arm = {
706706
let x_ident = Ident::with_dummy_span(sym::result);
707-
let (x_pat, x_pat_hid) = self.pat_ident(span, x_ident);
708-
let x_expr = self.expr_ident(span, x_ident, x_pat_hid);
709-
let ready_field = self.single_pat_field(span, x_pat);
707+
let (x_pat, x_pat_hid) = self.pat_ident(gen_future_span, x_ident);
708+
let x_expr = self.expr_ident(gen_future_span, x_ident, x_pat_hid);
709+
let ready_field = self.single_pat_field(gen_future_span, x_pat);
710710
let ready_pat = self.pat_lang_item_variant(
711711
span,
712712
hir::LangItem::PollReady,
@@ -716,7 +716,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
716716
let break_x = self.with_loop_scope(loop_node_id, move |this| {
717717
let expr_break =
718718
hir::ExprKind::Break(this.lower_loop_destination(None), Some(x_expr));
719-
this.arena.alloc(this.expr(span, expr_break, ThinVec::new()))
719+
this.arena.alloc(this.expr(gen_future_span, expr_break, ThinVec::new()))
720720
});
721721
self.arm(ready_pat, break_x)
722722
};
@@ -788,7 +788,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
788788
// `match ::std::future::IntoFuture::into_future(<expr>) { ... }`
789789
let into_future_span = self.mark_span_with_reason(
790790
DesugaringKind::Await,
791-
await_span,
791+
dot_await_span,
792792
self.allow_into_future.clone(),
793793
);
794794
let into_future_expr = self.expr_call_lang_item_fn(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// edition:2021
2+
// run-rustfix
3+
#![allow(dead_code)]
4+
5+
async fn a() {}
6+
7+
async fn foo() -> Result<(), i32> {
8+
Ok(a().await) //~ ERROR mismatched types
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// edition:2021
2+
// run-rustfix
3+
#![allow(dead_code)]
4+
5+
async fn a() {}
6+
7+
async fn foo() -> Result<(), i32> {
8+
a().await //~ ERROR mismatched types
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/proper-span-for-type-error.rs:8:5
3+
|
4+
LL | a().await
5+
| ^^^^^^^^^ expected enum `Result`, found `()`
6+
|
7+
= note: expected enum `Result<(), i32>`
8+
found unit type `()`
9+
help: try wrapping the expression in `Ok`
10+
|
11+
LL | Ok(a().await)
12+
| +++ +
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)