1
Fork 0

Give a better error message on async use in edition 2015

This commit is contained in:
Santiago Pastorino 2025-02-21 17:24:46 -03:00
parent 2f48fcec63
commit 4e6407ab94
No known key found for this signature in database
GPG key ID: 8131A24E0C79EFAF
5 changed files with 38 additions and 8 deletions

View file

@ -26,6 +26,8 @@ parse_async_move_block_in_2015 = `async move` blocks are only allowed in Rust 20
parse_async_move_order_incorrect = the order of `move` and `async` is incorrect
.suggestion = try switching the order
parse_async_use_block_in_2015 = `async use` blocks are only allowed in Rust 2018 or later
parse_async_use_order_incorrect = the order of `use` and `async` is incorrect
.suggestion = try switching the order

View file

@ -1688,6 +1688,13 @@ pub(crate) struct AsyncMoveBlockIn2015 {
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(parse_async_use_block_in_2015)]
pub(crate) struct AsyncUseBlockIn2015 {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(parse_async_bound_modifier_in_2015)]
pub(crate) struct AsyncBoundModifierIn2015 {

View file

@ -31,11 +31,11 @@ use super::{
SeqSep, TokenType,
};
use crate::errors::{
AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AttributeOnParamType, AwaitSuggestion,
BadQPathStage2, BadTypePlus, BadTypePlusSub, ColonAsSemi, ComparisonOperatorsCannotBeChained,
ComparisonOperatorsCannotBeChainedSugg, ConstGenericWithoutBraces,
ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything, DocCommentOnParamType,
DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AsyncUseBlockIn2015, AttributeOnParamType,
AwaitSuggestion, BadQPathStage2, BadTypePlus, BadTypePlusSub, ColonAsSemi,
ComparisonOperatorsCannotBeChained, ComparisonOperatorsCannotBeChainedSugg,
ConstGenericWithoutBraces, ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything,
DocCommentOnParamType, DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
GenericParamsWithoutAngleBrackets, GenericParamsWithoutAngleBracketsSugg,
HelpIdentifierStartsWithNumber, HelpUseLatestEdition, InInTypo, IncorrectAwait,
IncorrectSemicolon, IncorrectUseOfAwait, IncorrectUseOfUse, PatternMethodParamWithoutBody,
@ -572,10 +572,17 @@ impl<'a> Parser<'a> {
return Err(self.dcx().create_err(UseEqInstead { span: self.token.span }));
}
if self.token.is_keyword(kw::Move) && self.prev_token.is_keyword(kw::Async) {
// The 2015 edition is in use because parsing of `async move` has failed.
if (self.token.is_keyword(kw::Move) || self.token.is_keyword(kw::Use))
&& self.prev_token.is_keyword(kw::Async)
{
// The 2015 edition is in use because parsing of `async move` or `async use` has failed.
let span = self.prev_token.span.to(self.token.span);
if self.token.is_keyword(kw::Move) {
return Err(self.dcx().create_err(AsyncMoveBlockIn2015 { span }));
} else {
// kw::Use
return Err(self.dcx().create_err(AsyncUseBlockIn2015 { span }));
}
}
let expect = tokens_to_string(&expected);

View file

@ -0,0 +1,6 @@
#![feature(ergonomic_clones)]
fn main() {
async use {};
//~^ ERROR `async use` blocks are only allowed in Rust 2018 or later
}

View file

@ -0,0 +1,8 @@
error: `async use` blocks are only allowed in Rust 2018 or later
--> $DIR/edition-2015.rs:4:5
|
LL | async use {};
| ^^^^^^^^^
error: aborting due to 1 previous error