Skip to content

Commit

Permalink
Rollup merge of #104309 - chenyukang:yukang/fix-104088-identifier-err…
Browse files Browse the repository at this point in the history
…or, r=davidtwco

Slightly improve error message for invalid identifier

fixes #104088
  • Loading branch information
matthiaskrgr authored Nov 15, 2022
2 parents d8b416d + b2b5094 commit 769f231
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 2 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/parser.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,6 @@ parser_fn_ptr_with_generics = function pointer types may not have generic parame
[true] the
*[false] a
} `for` parameter list
parser_invalid_identifier_with_leading_number = expected identifier, found number literal
.label = identifiers cannot start with a number
8 changes: 8 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,14 @@ pub(crate) struct SelfParamNotFirst {
pub span: Span,
}

#[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 {
Expand Down
16 changes: 14 additions & 2 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -355,6 +356,17 @@ 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 ) ) {
return Err(self.sess.create_err(InvalidIdentiferStartsWithNumber { span: self.token.span }));
}
Ok(())
}

fn check_let_else_init_bool_expr(&self, init: &ast::Expr) {
if let ast::ExprKind::Binary(op, ..) = init.kind {
if op.node.lazy() {
Expand Down
26 changes: 26 additions & 0 deletions src/test/ui/parser/issues/issue-104088.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
fn test() {
if let 123 = 123 { println!("yes"); }
}

fn test_2() {
let 1x = 123;
//~^ ERROR expected identifier, found number literal
}

fn test_3() {
let 2x: i32 = 123;
//~^ ERROR expected identifier, found number literal
}

fn test_4() {
if let 2e1 = 123 {
//~^ ERROR mismatched types
}
}

fn test_5() {
let 23name = 123;
//~^ ERROR expected identifier, found number literal
}

fn main() {}
29 changes: 29 additions & 0 deletions src/test/ui/parser/issues/issue-104088.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error: expected identifier, found number literal
--> $DIR/issue-104088.rs:6:9
|
LL | let 1x = 123;
| ^^ identifiers cannot start with a number

error: expected identifier, found number literal
--> $DIR/issue-104088.rs:11:9
|
LL | let 2x: i32 = 123;
| ^^ identifiers cannot start with a number

error: expected identifier, found number literal
--> $DIR/issue-104088.rs:22:9
|
LL | let 23name = 123;
| ^^^^^^ identifiers cannot start with a number

error[E0308]: mismatched types
--> $DIR/issue-104088.rs:16:12
|
LL | if let 2e1 = 123 {
| ^^^ --- this expression has type `{integer}`
| |
| expected integer, found floating-point number

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.
File renamed without changes.
File renamed without changes.

0 comments on commit 769f231

Please sign in to comment.