@@ -97,6 +97,10 @@ pub struct LoweringContext<'a> {
9797 is_generator : bool ,
9898 is_async_body : bool ,
9999
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+
100104 catch_scopes : Vec < NodeId > ,
101105 loop_scopes : Vec < NodeId > ,
102106 is_in_loop_condition : bool ,
@@ -250,6 +254,7 @@ pub fn lower_crate(
250254 node_id_to_hir_id : IndexVec :: new ( ) ,
251255 is_generator : false ,
252256 is_async_body : false ,
257+ current_item : None ,
253258 is_in_trait_impl : false ,
254259 lifetimes_to_define : Vec :: new ( ) ,
255260 is_collecting_in_band_lifetimes : false ,
@@ -3116,6 +3121,7 @@ impl<'a> LoweringContext<'a> {
31163121 ItemKind :: Fn ( ref decl, ref header, ref generics, ref body) => {
31173122 let fn_def_id = self . resolver . definitions ( ) . local_def_id ( id) ;
31183123 self . with_new_scopes ( |this| {
3124+ this. current_item = Some ( ident. span ) ;
31193125 let mut lower_fn = |decl : & FnDecl | {
31203126 // Note: we don't need to change the return type from `T` to
31213127 // `impl Future<Output = T>` here because lower_body
@@ -3654,6 +3660,7 @@ impl<'a> LoweringContext<'a> {
36543660 } else {
36553661 lower_method ( sig)
36563662 } ;
3663+ self . current_item = Some ( i. span ) ;
36573664
36583665 ( generics, hir:: ImplItemKind :: Method ( sig, body_id) )
36593666 }
@@ -4270,6 +4277,7 @@ impl<'a> LoweringContext<'a> {
42704277 let fn_decl = self . lower_fn_decl ( decl, None , false , None ) ;
42714278
42724279 self . with_new_scopes ( |this| {
4280+ this. current_item = Some ( fn_decl_span) ;
42734281 let mut is_generator = false ;
42744282 let body_id = this. lower_body ( Some ( decl) , |this| {
42754283 let e = this. lower_expr ( body) ;
@@ -5551,13 +5559,18 @@ impl<'a> LoweringContext<'a> {
55515559 // }
55525560 // }
55535561 if !self . is_async_body {
5554- span_err ! (
5562+ let mut err = struct_span_err ! (
55555563 self . sess,
55565564 await_span,
55575565 E0728 ,
55585566 "`await` is only allowed inside `async` functions and blocks"
55595567 ) ;
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 ;
55615574 }
55625575 let span = self . sess . source_map ( ) . mark_span_with_reason (
55635576 CompilerDesugaringKind :: Await ,
0 commit comments