diff options
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/errors.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/stmt.rs | 17 |
2 files changed, 23 insertions, 2 deletions
diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index a39398950a5..2b17cea9794 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -1206,6 +1206,14 @@ pub(crate) struct SelfParamNotFirst { } #[derive(Diagnostic)] +#[diag(parser_invalid_identifier_with_leading_number)] +pub(crate) struct InvalidIdentiferStartsWithNumber { + #[primary_span] + #[label] + pub span: Span, +} + +#[derive(Diagnostic)] #[diag(parser_const_generic_without_braces)] pub(crate) struct ConstGenericWithoutBraces { #[primary_span] diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 9684145ad99..c2c3ff64ec7 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -10,8 +10,8 @@ use super::{ use crate::errors::{ AssignmentElseNotAllowed, CompoundAssignmentExpressionInLet, ConstLetMutuallyExclusive, DocCommentDoesNotDocumentAnything, ExpectedStatementAfterOuterAttr, InvalidCurlyInLetElse, - InvalidExpressionInLetElse, InvalidVariableDeclaration, InvalidVariableDeclarationSub, - WrapExpressionInParentheses, + InvalidExpressionInLetElse, InvalidIdentiferStartsWithNumber, InvalidVariableDeclaration, + InvalidVariableDeclarationSub, WrapExpressionInParentheses, }; use crate::maybe_whole; @@ -264,6 +264,7 @@ impl<'a> Parser<'a> { self.bump(); } + self.report_invalid_identifier_error()?; let (pat, colon) = self.parse_pat_before_ty(None, RecoverComma::Yes, "`let` bindings")?; let (err, ty) = if colon { @@ -355,6 +356,18 @@ impl<'a> Parser<'a> { Ok(P(ast::Local { ty, pat, kind, id: DUMMY_NODE_ID, span: lo.to(hi), attrs, tokens: None })) } + /// report error for `let 1x = 123` + pub fn report_invalid_identifier_error(&mut self) -> PResult<'a, ()> { + if let token::Literal(lit) = self.token.uninterpolate().kind && + let Err(_) = rustc_ast::Lit::from_token(&self.token) && + (lit.kind == token::LitKind::Integer || lit.kind == token::LitKind::Float) && + self.look_ahead(1, |t| matches!(t.kind, token::Eq) || matches!(t.kind, token::Colon ) ) { + let err = self.sess.create_err(InvalidIdentiferStartsWithNumber { span: self.token.span }); + return Err(err); + } + Ok(()) + } + fn check_let_else_init_bool_expr(&self, init: &ast::Expr) { if let ast::ExprKind::Binary(op, ..) = init.kind { if op.node.lazy() { |
