Skip to content

Commit 4e6407a

Browse files
committed
Give a better error message on async use in edition 2015
1 parent 2f48fce commit 4e6407a

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

compiler/rustc_parse/messages.ftl

+2
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

+7
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,13 @@ pub(crate) struct AsyncMoveBlockIn2015 {
16881688
pub span: Span,
16891689
}
16901690

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

compiler/rustc_parse/src/parser/diagnostics.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ use super::{
3131
SeqSep, TokenType,
3232
};
3333
use crate::errors::{
34-
AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AttributeOnParamType, AwaitSuggestion,
35-
BadQPathStage2, BadTypePlus, BadTypePlusSub, ColonAsSemi, ComparisonOperatorsCannotBeChained,
36-
ComparisonOperatorsCannotBeChainedSugg, ConstGenericWithoutBraces,
37-
ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything, DocCommentOnParamType,
38-
DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
34+
AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AsyncUseBlockIn2015, AttributeOnParamType,
35+
AwaitSuggestion, BadQPathStage2, BadTypePlus, BadTypePlusSub, ColonAsSemi,
36+
ComparisonOperatorsCannotBeChained, ComparisonOperatorsCannotBeChainedSugg,
37+
ConstGenericWithoutBraces, ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything,
38+
DocCommentOnParamType, DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
3939
GenericParamsWithoutAngleBrackets, GenericParamsWithoutAngleBracketsSugg,
4040
HelpIdentifierStartsWithNumber, HelpUseLatestEdition, InInTypo, IncorrectAwait,
4141
IncorrectSemicolon, IncorrectUseOfAwait, IncorrectUseOfUse, PatternMethodParamWithoutBody,
@@ -572,10 +572,17 @@ impl<'a> Parser<'a> {
572572
return Err(self.dcx().create_err(UseEqInstead { span: self.token.span }));
573573
}
574574

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

581588
let expect = tokens_to_string(&expected);
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+
}
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)