@@ -97,6 +97,10 @@ pub struct LoweringContext<'a> {
97
97
is_generator : bool ,
98
98
is_async_body : bool ,
99
99
100
+ /// Used to get the current `fn`'s def span to point to when using `await`
101
+ /// outside of an `async fn`.
102
+ current_item : Option < Span > ,
103
+
100
104
catch_scopes : Vec < NodeId > ,
101
105
loop_scopes : Vec < NodeId > ,
102
106
is_in_loop_condition : bool ,
@@ -250,6 +254,7 @@ pub fn lower_crate(
250
254
node_id_to_hir_id : IndexVec :: new ( ) ,
251
255
is_generator : false ,
252
256
is_async_body : false ,
257
+ current_item : None ,
253
258
is_in_trait_impl : false ,
254
259
lifetimes_to_define : Vec :: new ( ) ,
255
260
is_collecting_in_band_lifetimes : false ,
@@ -3116,6 +3121,7 @@ impl<'a> LoweringContext<'a> {
3116
3121
ItemKind :: Fn ( ref decl, ref header, ref generics, ref body) => {
3117
3122
let fn_def_id = self . resolver . definitions ( ) . local_def_id ( id) ;
3118
3123
self . with_new_scopes ( |this| {
3124
+ this. current_item = Some ( ident. span ) ;
3119
3125
let mut lower_fn = |decl : & FnDecl | {
3120
3126
// Note: we don't need to change the return type from `T` to
3121
3127
// `impl Future<Output = T>` here because lower_body
@@ -3654,6 +3660,7 @@ impl<'a> LoweringContext<'a> {
3654
3660
} else {
3655
3661
lower_method ( sig)
3656
3662
} ;
3663
+ self . current_item = Some ( i. span ) ;
3657
3664
3658
3665
( generics, hir:: ImplItemKind :: Method ( sig, body_id) )
3659
3666
}
@@ -4270,6 +4277,7 @@ impl<'a> LoweringContext<'a> {
4270
4277
let fn_decl = self . lower_fn_decl ( decl, None , false , None ) ;
4271
4278
4272
4279
self . with_new_scopes ( |this| {
4280
+ this. current_item = Some ( fn_decl_span) ;
4273
4281
let mut is_generator = false ;
4274
4282
let body_id = this. lower_body ( Some ( decl) , |this| {
4275
4283
let e = this. lower_expr ( body) ;
@@ -5551,13 +5559,18 @@ impl<'a> LoweringContext<'a> {
5551
5559
// }
5552
5560
// }
5553
5561
if !self . is_async_body {
5554
- span_err ! (
5562
+ let mut err = struct_span_err ! (
5555
5563
self . sess,
5556
5564
await_span,
5557
5565
E0728 ,
5558
5566
"`await` is only allowed inside `async` functions and blocks"
5559
5567
) ;
5560
- self . sess . abort_if_errors ( ) ;
5568
+ err. span_label ( await_span, "only allowed inside `async` functions and blocks" ) ;
5569
+ if let Some ( item_sp) = self . current_item {
5570
+ err. span_label ( item_sp, "this is not `async`" ) ;
5571
+ }
5572
+ err. emit ( ) ;
5573
+ return hir:: ExprKind :: Err ;
5561
5574
}
5562
5575
let span = self . sess . source_map ( ) . mark_span_with_reason (
5563
5576
CompilerDesugaringKind :: Await ,
0 commit comments