Point at enclosing fn/closure when it's not async
This commit is contained in:
parent
91c36c40bd
commit
c616605005
5 changed files with 60 additions and 41 deletions
|
@ -99,7 +99,7 @@ pub struct LoweringContext<'a> {
|
|||
|
||||
/// Used to get the current `fn`'s def span to point to when using `await`
|
||||
/// outside of an `async fn`.
|
||||
current_item_id: Option<hir::HirId>,
|
||||
current_item: Option<Span>,
|
||||
|
||||
catch_scopes: Vec<NodeId>,
|
||||
loop_scopes: Vec<NodeId>,
|
||||
|
@ -254,7 +254,7 @@ pub fn lower_crate(
|
|||
node_id_to_hir_id: IndexVec::new(),
|
||||
is_generator: false,
|
||||
is_async_body: false,
|
||||
current_item_id: None,
|
||||
current_item: None,
|
||||
is_in_trait_impl: false,
|
||||
lifetimes_to_define: Vec::new(),
|
||||
is_collecting_in_band_lifetimes: false,
|
||||
|
@ -3120,8 +3120,8 @@ impl<'a> LoweringContext<'a> {
|
|||
}
|
||||
ItemKind::Fn(ref decl, ref header, ref generics, ref body) => {
|
||||
let fn_def_id = self.resolver.definitions().local_def_id(id);
|
||||
let hir_id = self.lower_node_id(id);
|
||||
self.with_new_scopes(|this| {
|
||||
this.current_item = Some(ident.span);
|
||||
let mut lower_fn = |decl: &FnDecl| {
|
||||
// Note: we don't need to change the return type from `T` to
|
||||
// `impl Future<Output = T>` here because lower_body
|
||||
|
@ -3159,7 +3159,6 @@ impl<'a> LoweringContext<'a> {
|
|||
} else {
|
||||
lower_fn(decl)
|
||||
};
|
||||
this.current_item_id = Some(hir_id);
|
||||
|
||||
hir::ItemKind::Fn(
|
||||
fn_decl,
|
||||
|
@ -3661,6 +3660,7 @@ impl<'a> LoweringContext<'a> {
|
|||
} else {
|
||||
lower_method(sig)
|
||||
};
|
||||
self.current_item = Some(i.span);
|
||||
|
||||
(generics, hir::ImplItemKind::Method(sig, body_id))
|
||||
}
|
||||
|
@ -4277,6 +4277,7 @@ impl<'a> LoweringContext<'a> {
|
|||
let fn_decl = self.lower_fn_decl(decl, None, false, None);
|
||||
|
||||
self.with_new_scopes(|this| {
|
||||
this.current_item = Some(fn_decl_span);
|
||||
let mut is_generator = false;
|
||||
let body_id = this.lower_body(Some(decl), |this| {
|
||||
let e = this.lower_expr(body);
|
||||
|
@ -5565,11 +5566,8 @@ impl<'a> LoweringContext<'a> {
|
|||
"`await` is only allowed inside `async` functions and blocks"
|
||||
);
|
||||
err.span_label(await_span, "only allowed inside `async` functions and blocks");
|
||||
if let Some(item_id) = self.current_item_id {
|
||||
err.span_label(
|
||||
self.sess.source_map().def_span(self.items[&item_id].span),
|
||||
"this function is not `async`",
|
||||
);
|
||||
if let Some(item_sp) = self.current_item {
|
||||
err.span_label(item_sp, "this is not `async`");
|
||||
}
|
||||
err.emit();
|
||||
return hir::ExprKind::Err;
|
||||
|
|
|
@ -89,5 +89,19 @@ fn foo16() -> Result<(), ()> {
|
|||
let _ = bar().await?; //~ ERROR `await` is only allowed inside `async` functions and blocks
|
||||
Ok(())
|
||||
}
|
||||
fn foo24() -> Result<(), ()> {
|
||||
fn foo() -> Result<(), ()> {
|
||||
let _ = bar().await?; //~ ERROR `await` is only allowed inside `async` functions and blocks
|
||||
Ok(())
|
||||
}
|
||||
foo()
|
||||
}
|
||||
fn foo25() -> Result<(), ()> {
|
||||
let foo = || {
|
||||
let _ = bar().await?; //~ ERROR `await` is only allowed inside `async` functions and blocks
|
||||
Ok(())
|
||||
};
|
||||
foo()
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -91,75 +91,83 @@ LL | let _ = bar().await()?;
|
|||
error[E0728]: `await` is only allowed inside `async` functions and blocks
|
||||
--> $DIR/incorrect-syntax-suggestions.rs:55:13
|
||||
|
|
||||
LL | async fn foo8() -> Result<(), ()> {
|
||||
| --------------------------------- this function is not `async`
|
||||
...
|
||||
LL | fn foo9() -> Result<(), ()> {
|
||||
| ---- this is not `async`
|
||||
LL | let _ = await bar();
|
||||
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
|
||||
|
||||
error[E0728]: `await` is only allowed inside `async` functions and blocks
|
||||
--> $DIR/incorrect-syntax-suggestions.rs:60:13
|
||||
|
|
||||
LL | fn foo9() -> Result<(), ()> {
|
||||
| --------------------------- this function is not `async`
|
||||
...
|
||||
LL | fn foo10() -> Result<(), ()> {
|
||||
| ----- this is not `async`
|
||||
LL | let _ = await? bar();
|
||||
| ^^^^^^^^^^^^ only allowed inside `async` functions and blocks
|
||||
|
||||
error[E0728]: `await` is only allowed inside `async` functions and blocks
|
||||
--> $DIR/incorrect-syntax-suggestions.rs:65:13
|
||||
|
|
||||
LL | fn foo10() -> Result<(), ()> {
|
||||
| ---------------------------- this function is not `async`
|
||||
...
|
||||
LL | fn foo11() -> Result<(), ()> {
|
||||
| ----- this is not `async`
|
||||
LL | let _ = await bar()?;
|
||||
| ^^^^^^^^^^^^ only allowed inside `async` functions and blocks
|
||||
|
||||
error[E0728]: `await` is only allowed inside `async` functions and blocks
|
||||
--> $DIR/incorrect-syntax-suggestions.rs:70:14
|
||||
|
|
||||
LL | fn foo11() -> Result<(), ()> {
|
||||
| ---------------------------- this function is not `async`
|
||||
...
|
||||
LL | fn foo12() -> Result<(), ()> {
|
||||
| ----- this is not `async`
|
||||
LL | let _ = (await bar())?;
|
||||
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
|
||||
|
||||
error[E0728]: `await` is only allowed inside `async` functions and blocks
|
||||
--> $DIR/incorrect-syntax-suggestions.rs:75:13
|
||||
|
|
||||
LL | fn foo12() -> Result<(), ()> {
|
||||
| ---------------------------- this function is not `async`
|
||||
...
|
||||
LL | fn foo13() -> Result<(), ()> {
|
||||
| ----- this is not `async`
|
||||
LL | let _ = bar().await();
|
||||
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
|
||||
|
||||
error[E0728]: `await` is only allowed inside `async` functions and blocks
|
||||
--> $DIR/incorrect-syntax-suggestions.rs:80:13
|
||||
|
|
||||
LL | fn foo13() -> Result<(), ()> {
|
||||
| ---------------------------- this function is not `async`
|
||||
...
|
||||
LL | fn foo14() -> Result<(), ()> {
|
||||
| ----- this is not `async`
|
||||
LL | let _ = bar().await()?;
|
||||
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
|
||||
|
||||
error[E0728]: `await` is only allowed inside `async` functions and blocks
|
||||
--> $DIR/incorrect-syntax-suggestions.rs:85:13
|
||||
|
|
||||
LL | fn foo14() -> Result<(), ()> {
|
||||
| ---------------------------- this function is not `async`
|
||||
...
|
||||
LL | fn foo15() -> Result<(), ()> {
|
||||
| ----- this is not `async`
|
||||
LL | let _ = bar().await;
|
||||
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
|
||||
|
||||
error[E0728]: `await` is only allowed inside `async` functions and blocks
|
||||
--> $DIR/incorrect-syntax-suggestions.rs:89:13
|
||||
|
|
||||
LL | fn foo15() -> Result<(), ()> {
|
||||
| ---------------------------- this function is not `async`
|
||||
...
|
||||
LL | fn foo16() -> Result<(), ()> {
|
||||
| ----- this is not `async`
|
||||
LL | let _ = bar().await?;
|
||||
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
|
||||
|
||||
error[E0728]: `await` is only allowed inside `async` functions and blocks
|
||||
--> $DIR/incorrect-syntax-suggestions.rs:94:17
|
||||
|
|
||||
LL | fn foo() -> Result<(), ()> {
|
||||
| --- this is not `async`
|
||||
LL | let _ = bar().await?;
|
||||
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
|
||||
|
||||
error[E0728]: `await` is only allowed inside `async` functions and blocks
|
||||
--> $DIR/incorrect-syntax-suggestions.rs:101:17
|
||||
|
|
||||
LL | let foo = || {
|
||||
| -- this is not `async`
|
||||
LL | let _ = bar().await?;
|
||||
| ^^^^^^^^^^^ only allowed inside `async` functions and blocks
|
||||
|
||||
error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
|
||||
--> $DIR/incorrect-syntax-suggestions.rs:18:19
|
||||
|
|
||||
|
@ -169,6 +177,6 @@ LL | let _ = await bar()?;
|
|||
= help: the trait `std::ops::Try` is not implemented for `impl std::future::Future`
|
||||
= note: required by `std::ops::Try::into_result`
|
||||
|
||||
error: aborting due to 24 previous errors
|
||||
error: aborting due to 26 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0277`.
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
error[E0728]: `await` is only allowed inside `async` functions and blocks
|
||||
--> $DIR/issue-51719.rs:10:19
|
||||
|
|
||||
LL | async fn foo() {}
|
||||
| -------------- this function is not `async`
|
||||
...
|
||||
LL | let _gen = || foo.await;
|
||||
| ^^^^^^^^^ only allowed inside `async` functions and blocks
|
||||
| -- ^^^^^^^^^ only allowed inside `async` functions and blocks
|
||||
| |
|
||||
| this is not `async`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
error[E0728]: `await` is only allowed inside `async` functions and blocks
|
||||
--> $DIR/issue-51751.rs:11:20
|
||||
|
|
||||
LL | async fn inc(limit: i64) -> i64 {
|
||||
| ------------------------------- this function is not `async`
|
||||
...
|
||||
LL | fn main() {
|
||||
| ---- this is not `async`
|
||||
LL | let result = inc(10000);
|
||||
LL | let finished = result.await;
|
||||
| ^^^^^^^^^^^^ only allowed inside `async` functions and blocks
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue