Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly improve error message for invalid identifier #104309

Merged
merged 3 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.