Skip to content

Commit e2ff121

Browse files
committed
Give a better error message on async use in edition 2015
1 parent 9350c5d commit e2ff121

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

compiler/rustc_parse/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ parse_async_move_block_in_2015 = `async move` blocks are only allowed in Rust 20
2626
parse_async_move_order_incorrect = the order of `move` and `async` is incorrect
2727
.suggestion = try switching the order
2828
29+
parse_async_use_block_in_2015 = `async use` blocks are only allowed in Rust 2018 or later
30+
2931
parse_async_use_order_incorrect = the order of `use` and `async` is incorrect
3032
.suggestion = try switching the order
3133

compiler/rustc_parse/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,13 @@ pub(crate) struct AsyncMoveBlockIn2015 {
16891689
pub span: Span,
16901690
}
16911691

1692+
#[derive(Diagnostic)]
1693+
#[diag(parse_async_use_block_in_2015)]
1694+
pub(crate) struct AsyncUseBlockIn2015 {
1695+
#[primary_span]
1696+
pub span: Span,
1697+
}
1698+
16921699
#[derive(Diagnostic)]
16931700
#[diag(parse_async_bound_modifier_in_2015)]
16941701
pub(crate) struct AsyncBoundModifierIn2015 {

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ use super::{
3333
SeqSep, TokenType,
3434
};
3535
use crate::errors::{
36-
AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AttributeOnParamType, AwaitSuggestion,
37-
BadQPathStage2, BadTypePlus, BadTypePlusSub, ColonAsSemi, ComparisonOperatorsCannotBeChained,
38-
ComparisonOperatorsCannotBeChainedSugg, ConstGenericWithoutBraces,
39-
ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything, DocCommentOnParamType,
40-
DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
36+
AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AsyncUseBlockIn2015, AttributeOnParamType,
37+
AwaitSuggestion, BadQPathStage2, BadTypePlus, BadTypePlusSub, ColonAsSemi,
38+
ComparisonOperatorsCannotBeChained, ComparisonOperatorsCannotBeChainedSugg,
39+
ConstGenericWithoutBraces, ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything,
40+
DocCommentOnParamType, DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
4141
GenericParamsWithoutAngleBrackets, GenericParamsWithoutAngleBracketsSugg,
4242
HelpIdentifierStartsWithNumber, HelpUseLatestEdition, InInTypo, IncorrectAwait,
4343
IncorrectSemicolon, IncorrectUseOfAwait, IncorrectUseOfUse, PatternMethodParamWithoutBody,
@@ -574,10 +574,17 @@ impl<'a> Parser<'a> {
574574
return Err(self.dcx().create_err(UseEqInstead { span: self.token.span }));
575575
}
576576

577-
if self.token.is_keyword(kw::Move) && self.prev_token.is_keyword(kw::Async) {
578-
// The 2015 edition is in use because parsing of `async move` has failed.
577+
if (self.token.is_keyword(kw::Move) || self.token.is_keyword(kw::Use))
578+
&& self.prev_token.is_keyword(kw::Async)
579+
{
580+
// The 2015 edition is in use because parsing of `async move` or `async use` has failed.
579581
let span = self.prev_token.span.to(self.token.span);
580-
return Err(self.dcx().create_err(AsyncMoveBlockIn2015 { span }));
582+
if self.token.is_keyword(kw::Move) {
583+
return Err(self.dcx().create_err(AsyncMoveBlockIn2015 { span }));
584+
} else {
585+
// kw::Use
586+
return Err(self.dcx().create_err(AsyncUseBlockIn2015 { span }));
587+
}
581588
}
582589

583590
let expect = tokens_to_string(&expected);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![feature(ergonomic_clones)]
2+
3+
fn main() {
4+
async use {};
5+
//~^ ERROR `async use` blocks are only allowed in Rust 2018 or later
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: `async use` blocks are only allowed in Rust 2018 or later
2+
--> $DIR/edition-2015.rs:4:5
3+
|
4+
LL | async use {};
5+
| ^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)