Skip to content

Add note for identifier following by array literal error #108222

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

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 32 additions & 0 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,38 @@ impl<'a> Parser<'a> {

/// Parse an indexing expression `expr[...]`.
fn parse_index_expr(&mut self, lo: Span, base: P<Expr>) -> PResult<'a, P<Expr>> {
if self.prev_token.is_ident()
&& matches!(self.token.kind, token::OpenDelim(Delimiter::Bracket))
&& self.look_ahead(1, |t| t.is_lit())
&& self.look_ahead(2, |t| matches!(t.kind, token::Semi))
&& self.look_ahead(3, |t| t.is_lit())
&& self.look_ahead(4, |t| matches!(t.kind, token::CloseDelim(Delimiter::Bracket)))
&& let token::Ident(symbol, _) = self.prev_token.kind
&& self.may_recover()
{
let ident = symbol.as_str().to_owned();
self.bump(); // [
if let token::Literal(lit) = self.token.kind {
let lit1 = lit.symbol.as_str().to_owned();
self.bump(); // lit
let span = self.token.span;
self.bump(); // ;
if let token::Literal(lit) = self.token.kind {
let lit2 = lit.symbol.as_str().to_owned();
let mut err = self.struct_span_err(
span,
"expected one of `.`, `?`, `]`, or an operator, found `;`",
);
err.span_label(
span,
"expected one of `.`, `?`, `]`, or an operator",
);
err.note(format!("`[{}; {}]` would construct an array literal, but not immediately following the identifier `{}`", lit1, lit2, ident));
return Err(err);
}
}
}

let prev_span = self.prev_token.span;
let open_delim_span = self.token.span;
self.bump(); // `[`
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/issues/issue-107518.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
(a_function_that_takes_an_array[0; 10]);
//~^ ERROR expected one of `.`, `?`, `]`, or an operator, found `;`
}

fn a_function_that_takes_an_array(arg: [u8; 10]) {
let _ = arg;
}
10 changes: 10 additions & 0 deletions tests/ui/issues/issue-107518.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: expected one of `.`, `?`, `]`, or an operator, found `;`
--> $DIR/issue-107518.rs:2:38
|
LL | (a_function_that_takes_an_array[0; 10]);
| ^ expected one of `.`, `?`, `]`, or an operator
|
= note: `[0; 10]` would construct an array literal, but not immediately following the identifier `a_function_that_takes_an_array`

error: aborting due to previous error