Skip to content

Commit 93a3409

Browse files
committed
Add note for identifier following by array literal
1 parent 3eb5c45 commit 93a3409

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

compiler/rustc_parse/src/parser/expr.rs

+32
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,38 @@ impl<'a> Parser<'a> {
12361236

12371237
/// Parse an indexing expression `expr[...]`.
12381238
fn parse_index_expr(&mut self, lo: Span, base: P<Expr>) -> PResult<'a, P<Expr>> {
1239+
if self.prev_token.is_ident()
1240+
&& matches!(self.token.kind, token::OpenDelim(Delimiter::Bracket))
1241+
&& self.look_ahead(1, |t| t.is_lit())
1242+
&& self.look_ahead(2, |t| matches!(t.kind, token::Semi))
1243+
&& self.look_ahead(3, |t| t.is_lit())
1244+
&& self.look_ahead(4, |t| matches!(t.kind, token::CloseDelim(Delimiter::Bracket)))
1245+
&& let token::Ident(symbol, _) = self.prev_token.kind
1246+
&& self.may_recover()
1247+
{
1248+
let ident = symbol.as_str().to_owned();
1249+
self.bump(); // [
1250+
if let token::Literal(lit) = self.token.kind {
1251+
let lit1 = lit.symbol.as_str().to_owned();
1252+
self.bump(); // lit
1253+
let span = self.token.span;
1254+
self.bump(); // ;
1255+
if let token::Literal(lit) = self.token.kind {
1256+
let lit2 = lit.symbol.as_str().to_owned();
1257+
let mut err = self.struct_span_err(
1258+
span,
1259+
"expected one of `.`, `?`, `]`, or an operator, found `;`",
1260+
);
1261+
err.span_label(
1262+
span,
1263+
"expected one of `.`, `?`, `]`, or an operator",
1264+
);
1265+
err.note(format!("`[{}; {}]` would construct an array literal, but not immediately following the identifier `{}`", lit1, lit2, ident));
1266+
return Err(err);
1267+
}
1268+
}
1269+
}
1270+
12391271
let prev_span = self.prev_token.span;
12401272
let open_delim_span = self.token.span;
12411273
self.bump(); // `[`

tests/ui/issues/issue-107518.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
(a_function_that_takes_an_array[0; 10]);
3+
//~^ ERROR expected one of `.`, `?`, `]`, or an operator, found `;`
4+
}
5+
6+
fn a_function_that_takes_an_array(arg: [u8; 10]) {
7+
let _ = arg;
8+
}

tests/ui/issues/issue-107518.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected one of `.`, `?`, `]`, or an operator, found `;`
2+
--> $DIR/issue-107518.rs:2:38
3+
|
4+
LL | (a_function_that_takes_an_array[0; 10]);
5+
| ^ expected one of `.`, `?`, `]`, or an operator
6+
|
7+
= note: `[0; 10]` would construct an array literal, but not immediately following the identifier `a_function_that_takes_an_array`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)