Skip to content

Commit f384cdc

Browse files
committed
improve error message for unexpected comma token in multiline block
confusing diagnostics, issue #72253 add test for confusing error message, issue-72253 remove is_multiline check, refactor to self.expect(&token:Semi) update issue-72253 tests return Ok
1 parent 914adf0 commit f384cdc

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/librustc_parse/parser/diagnostics.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,19 @@ impl<'a> Parser<'a> {
934934
return self.expect(&token::Semi).map(drop);
935935
} else if !sm.is_multiline(self.prev_token.span.until(self.token.span)) {
936936
// The current token is in the same line as the prior token, not recoverable.
937+
} else if [token::Comma, token::Colon].contains(&self.token.kind)
938+
&& &self.prev_token.kind == &token::CloseDelim(token::Paren)
939+
{
940+
// Likely typo: The current token is on a new line and is expected to be
941+
// `.`, `;`, `?`, or an operator after a close delimiter token.
942+
//
943+
// let a = std::process::Command::new("echo")
944+
// .arg("1")
945+
// ,arg("2")
946+
// ^
947+
// https://github.com/rust-lang/rust/issues/72253
948+
self.expect(&token::Semi)?;
949+
return Ok(());
937950
} else if self.look_ahead(1, |t| {
938951
t == &token::CloseDelim(token::Brace) || t.can_begin_expr() && t.kind != token::Colon
939952
}) && [token::Comma, token::Colon].contains(&self.token.kind)

src/test/ui/issues/issue-72253.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
let a = std::process::Command::new("echo")
3+
.arg("1")
4+
,arg("2") //~ ERROR expected one of `.`, `;`, `?`, or an operator, found `,`
5+
.output();
6+
}

src/test/ui/issues/issue-72253.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected one of `.`, `;`, `?`, or an operator, found `,`
2+
--> $DIR/issue-72253.rs:4:9
3+
|
4+
LL | .arg("1")
5+
| - expected one of `.`, `;`, `?`, or an operator
6+
LL | ,arg("2")
7+
| ^ unexpected token
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)