Better error message in ed 2015

This commit is contained in:
Michael Goulet 2024-01-26 17:15:43 +00:00
parent cd2fd34ca6
commit 54db272cc9
6 changed files with 76 additions and 24 deletions

View File

@ -22,6 +22,8 @@ parse_associated_static_item_not_allowed = associated `static` items are not all
parse_async_block_in_2015 = `async` blocks are only allowed in Rust 2018 or later
parse_async_bound_modifier_in_2015 = `async` trait bounds are only allowed in Rust 2018 or later
parse_async_fn_in_2015 = `async fn` is not permitted in Rust 2015
.label = to use `async fn`, switch to Rust 2018 or later

View File

@ -1588,6 +1588,15 @@ pub(crate) struct AsyncMoveBlockIn2015 {
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(parse_async_bound_modifier_in_2015)]
pub(crate) struct AsyncBoundModifierIn2015 {
#[primary_span]
pub span: Span,
#[subdiagnostic]
pub help: HelpUseLatestEdition,
}
#[derive(Diagnostic)]
#[diag(parse_self_argument_pointer)]
pub(crate) struct SelfArgumentPointer {

View File

@ -3,8 +3,8 @@ use super::{Parser, PathStyle, TokenType};
use crate::errors::{
self, DynAfterMut, ExpectedFnPathFoundFnKeyword, ExpectedMutOrConstInRawPointerType,
FnPointerCannotBeAsync, FnPointerCannotBeConst, FnPtrWithGenerics, FnPtrWithGenericsSugg,
InvalidDynKeyword, LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime, NestedCVariadicType,
ReturnTypesUseThinArrow,
HelpUseLatestEdition, InvalidDynKeyword, LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime,
NestedCVariadicType, ReturnTypesUseThinArrow,
};
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
@ -882,6 +882,17 @@ impl<'a> Parser<'a> {
let asyncness = if self.token.span.at_least_rust_2018() && self.eat_keyword(kw::Async) {
self.sess.gated_spans.gate(sym::async_closure, self.prev_token.span);
BoundAsyncness::Async(self.prev_token.span)
} else if self.may_recover()
&& self.token.span.is_rust_2015()
&& self.is_kw_followed_by_ident(kw::Async)
{
self.bump(); // eat `async`
self.dcx().emit_err(errors::AsyncBoundModifierIn2015 {
span: self.prev_token.span,
help: HelpUseLatestEdition::new(),
});
self.sess.gated_spans.gate(sym::async_closure, self.prev_token.span);
BoundAsyncness::Async(self.prev_token.span)
} else {
BoundAsyncness::Normal
};

View File

@ -0,0 +1,10 @@
// check-pass
// Make sure that we don't eagerly recover `async ::Bound` in edition 2015.
mod async {
pub trait Foo {}
}
fn test(x: impl async ::Foo) {}
fn main() {}

View File

@ -1,8 +1,7 @@
// FIXME(async_closures): This error message could be made better.
fn foo(x: impl async Fn()) -> impl async Fn() {}
//~^ ERROR expected
//~| ERROR expected
//~| ERROR expected
fn foo(x: impl async Fn()) -> impl async Fn() { x }
//~^ ERROR `async` trait bounds are only allowed in Rust 2018 or later
//~| ERROR `async` trait bounds are only allowed in Rust 2018 or later
//~| ERROR async closures are unstable
//~| ERROR async closures are unstable
fn main() {}

View File

@ -1,22 +1,43 @@
error: expected one of `:` or `|`, found `)`
--> $DIR/edition-2015.rs:3:26
error: `async` trait bounds are only allowed in Rust 2018 or later
--> $DIR/edition-2015.rs:1:16
|
LL | fn foo(x: impl async Fn()) -> impl async Fn() {}
| ^ expected one of `:` or `|`
error: expected one of `(`, `)`, `+`, `,`, `::`, or `<`, found `Fn`
--> $DIR/edition-2015.rs:3:22
LL | fn foo(x: impl async Fn()) -> impl async Fn() { x }
| ^^^^^
|
LL | fn foo(x: impl async Fn()) -> impl async Fn() {}
| -^^ expected one of `(`, `)`, `+`, `,`, `::`, or `<`
| |
| help: missing `,`
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
error: expected one of `(`, `+`, `::`, `<`, `where`, or `{`, found `Fn`
--> $DIR/edition-2015.rs:3:42
error: `async` trait bounds are only allowed in Rust 2018 or later
--> $DIR/edition-2015.rs:1:36
|
LL | fn foo(x: impl async Fn()) -> impl async Fn() {}
| ^^ expected one of `(`, `+`, `::`, `<`, `where`, or `{`
LL | fn foo(x: impl async Fn()) -> impl async Fn() { x }
| ^^^^^
|
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
error: aborting due to 3 previous errors
error[E0658]: async closures are unstable
--> $DIR/edition-2015.rs:1:16
|
LL | fn foo(x: impl async Fn()) -> impl async Fn() { x }
| ^^^^^
|
= note: see issue #62290 <https://github.com/rust-lang/rust/issues/62290> for more information
= help: add `#![feature(async_closure)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= help: to use an async block, remove the `||`: `async {`
error[E0658]: async closures are unstable
--> $DIR/edition-2015.rs:1:36
|
LL | fn foo(x: impl async Fn()) -> impl async Fn() { x }
| ^^^^^
|
= note: see issue #62290 <https://github.com/rust-lang/rust/issues/62290> for more information
= help: add `#![feature(async_closure)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= help: to use an async block, remove the `||`: `async {`
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0658`.