Skip to content

Commit 9db8e18

Browse files
committed
fix #104088, Slightly improve error message for invalid identifier
1 parent 101e182 commit 9db8e18

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

Diff for: compiler/rustc_error_messages/locales/en-US/parser.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,6 @@ parser_fn_ptr_with_generics = function pointer types may not have generic parame
384384
[true] the
385385
*[false] a
386386
} `for` parameter list
387+
388+
parser_invalid_identifier_with_leading_number = expected identifier, found number literal
389+
.label = identifiers cannot start with a number

Diff for: compiler/rustc_parse/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,14 @@ pub(crate) struct SelfParamNotFirst {
12051205
pub span: Span,
12061206
}
12071207

1208+
#[derive(Diagnostic)]
1209+
#[diag(parser_invalid_identifier_with_leading_number)]
1210+
pub(crate) struct InvalidIdentiferStartsWithNumber {
1211+
#[primary_span]
1212+
#[label]
1213+
pub span: Span,
1214+
}
1215+
12081216
#[derive(Diagnostic)]
12091217
#[diag(parser_const_generic_without_braces)]
12101218
pub(crate) struct ConstGenericWithoutBraces {

Diff for: compiler/rustc_parse/src/parser/stmt.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use super::{
1010
use crate::errors::{
1111
AssignmentElseNotAllowed, CompoundAssignmentExpressionInLet, ConstLetMutuallyExclusive,
1212
DocCommentDoesNotDocumentAnything, ExpectedStatementAfterOuterAttr, InvalidCurlyInLetElse,
13-
InvalidExpressionInLetElse, InvalidVariableDeclaration, InvalidVariableDeclarationSub,
14-
WrapExpressionInParentheses,
13+
InvalidExpressionInLetElse, InvalidIdentiferStartsWithNumber, InvalidVariableDeclaration,
14+
InvalidVariableDeclarationSub, WrapExpressionInParentheses,
1515
};
1616
use crate::maybe_whole;
1717

@@ -264,6 +264,7 @@ impl<'a> Parser<'a> {
264264
self.bump();
265265
}
266266

267+
self.report_invalid_identifier_error()?;
267268
let (pat, colon) = self.parse_pat_before_ty(None, RecoverComma::Yes, "`let` bindings")?;
268269

269270
let (err, ty) = if colon {
@@ -355,6 +356,18 @@ impl<'a> Parser<'a> {
355356
Ok(P(ast::Local { ty, pat, kind, id: DUMMY_NODE_ID, span: lo.to(hi), attrs, tokens: None }))
356357
}
357358

359+
/// report error for `let 1x = 123`
360+
pub fn report_invalid_identifier_error(&mut self) -> PResult<'a, ()> {
361+
if let token::Literal(lit) = self.token.uninterpolate().kind &&
362+
let Err(_) = rustc_ast::Lit::from_token(&self.token) &&
363+
(lit.kind == token::LitKind::Integer || lit.kind == token::LitKind::Float) &&
364+
self.look_ahead(1, |t| matches!(t.kind, token::Eq) || matches!(t.kind, token::Colon ) ) {
365+
let err = self.sess.create_err(InvalidIdentiferStartsWithNumber { span: self.token.span });
366+
return Err(err);
367+
}
368+
Ok(())
369+
}
370+
358371
fn check_let_else_init_bool_expr(&self, init: &ast::Expr) {
359372
if let ast::ExprKind::Binary(op, ..) = init.kind {
360373
if op.node.lazy() {

Diff for: src/test/ui/parser/issues/issue-104088.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
fn test() {
2+
if let 123 = 123 { println!("yes"); }
3+
}
4+
5+
fn test_2() {
6+
let 1x = 123;
7+
//~^ ERROR expected identifier, found number literal
8+
}
9+
10+
fn test_3() {
11+
let 2x: i32 = 123;
12+
//~^ ERROR expected identifier, found number literal
13+
}
14+
15+
fn test_4() {
16+
if let 2e1 = 123 {
17+
//~^ ERROR mismatched types
18+
}
19+
}
20+
21+
fn test_5() {
22+
let 23name = 123;
23+
//~^ ERROR expected identifier, found number literal
24+
}
25+
26+
fn main() {}

Diff for: src/test/ui/parser/issues/issue-104088.stderr

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: expected identifier, found number literal
2+
--> $DIR/issue-104088.rs:6:9
3+
|
4+
LL | let 1x = 123;
5+
| ^^ identifiers cannot start with a number
6+
7+
error: expected identifier, found number literal
8+
--> $DIR/issue-104088.rs:11:9
9+
|
10+
LL | let 2x: i32 = 123;
11+
| ^^ identifiers cannot start with a number
12+
13+
error: expected identifier, found number literal
14+
--> $DIR/issue-104088.rs:22:9
15+
|
16+
LL | let 23name = 123;
17+
| ^^^^^^ identifiers cannot start with a number
18+
19+
error[E0308]: mismatched types
20+
--> $DIR/issue-104088.rs:16:12
21+
|
22+
LL | if let 2e1 = 123 {
23+
| ^^^ --- this expression has type `{integer}`
24+
| |
25+
| expected integer, found floating-point number
26+
27+
error: aborting due to 4 previous errors
28+
29+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)