Skip to content

Commit 4205077

Browse files
Rollup merge of #87436 - ebobrow:suggest-semicolon, r=oli-obk
Suggest `;` on parse error where applicable fixes #87197
2 parents cfc856a + e0995a5 commit 4205077

6 files changed

+118
-64
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+57-57
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,63 @@ impl<'a> Parser<'a> {
242242
expected.sort_by_cached_key(|x| x.to_string());
243243
expected.dedup();
244244

245+
let sm = self.sess.source_map();
246+
let msg = format!("expected `;`, found {}", super::token_descr(&self.token));
247+
let appl = Applicability::MachineApplicable;
248+
if expected.contains(&TokenType::Token(token::Semi)) {
249+
if self.token.span == DUMMY_SP || self.prev_token.span == DUMMY_SP {
250+
// Likely inside a macro, can't provide meaningful suggestions.
251+
} else if !sm.is_multiline(self.prev_token.span.until(self.token.span)) {
252+
// The current token is in the same line as the prior token, not recoverable.
253+
} else if [token::Comma, token::Colon].contains(&self.token.kind)
254+
&& self.prev_token.kind == token::CloseDelim(token::Paren)
255+
{
256+
// Likely typo: The current token is on a new line and is expected to be
257+
// `.`, `;`, `?`, or an operator after a close delimiter token.
258+
//
259+
// let a = std::process::Command::new("echo")
260+
// .arg("1")
261+
// ,arg("2")
262+
// ^
263+
// https://github.com/rust-lang/rust/issues/72253
264+
} else if self.look_ahead(1, |t| {
265+
t == &token::CloseDelim(token::Brace)
266+
|| t.can_begin_expr() && t.kind != token::Colon
267+
}) && [token::Comma, token::Colon].contains(&self.token.kind)
268+
{
269+
// Likely typo: `,` → `;` or `:` → `;`. This is triggered if the current token is
270+
// either `,` or `:`, and the next token could either start a new statement or is a
271+
// block close. For example:
272+
//
273+
// let x = 32:
274+
// let y = 42;
275+
self.bump();
276+
let sp = self.prev_token.span;
277+
self.struct_span_err(sp, &msg)
278+
.span_suggestion_short(sp, "change this to `;`", ";".to_string(), appl)
279+
.emit();
280+
return Ok(false);
281+
} else if self.look_ahead(0, |t| {
282+
t == &token::CloseDelim(token::Brace)
283+
|| (
284+
t.can_begin_expr() && t != &token::Semi && t != &token::Pound
285+
// Avoid triggering with too many trailing `#` in raw string.
286+
)
287+
}) {
288+
// Missing semicolon typo. This is triggered if the next token could either start a
289+
// new statement or is a block close. For example:
290+
//
291+
// let x = 32
292+
// let y = 42;
293+
let sp = self.prev_token.span.shrink_to_hi();
294+
self.struct_span_err(sp, &msg)
295+
.span_label(self.token.span, "unexpected token")
296+
.span_suggestion_short(sp, "add `;` here", ";".to_string(), appl)
297+
.emit();
298+
return Ok(false);
299+
}
300+
}
301+
245302
let expect = tokens_to_string(&expected[..]);
246303
let actual = super::token_descr(&self.token);
247304
let (msg_exp, (label_sp, label_exp)) = if expected.len() > 1 {
@@ -303,7 +360,6 @@ impl<'a> Parser<'a> {
303360
return Err(err);
304361
}
305362

306-
let sm = self.sess.source_map();
307363
if self.prev_token.span == DUMMY_SP {
308364
// Account for macro context where the previous span might not be
309365
// available to avoid incorrect output (#54841).
@@ -1144,62 +1200,6 @@ impl<'a> Parser<'a> {
11441200
if self.eat(&token::Semi) {
11451201
return Ok(());
11461202
}
1147-
let sm = self.sess.source_map();
1148-
let msg = format!("expected `;`, found {}", super::token_descr(&self.token));
1149-
let appl = Applicability::MachineApplicable;
1150-
if self.token.span == DUMMY_SP || self.prev_token.span == DUMMY_SP {
1151-
// Likely inside a macro, can't provide meaningful suggestions.
1152-
return self.expect(&token::Semi).map(drop);
1153-
} else if !sm.is_multiline(self.prev_token.span.until(self.token.span)) {
1154-
// The current token is in the same line as the prior token, not recoverable.
1155-
} else if [token::Comma, token::Colon].contains(&self.token.kind)
1156-
&& self.prev_token.kind == token::CloseDelim(token::Paren)
1157-
{
1158-
// Likely typo: The current token is on a new line and is expected to be
1159-
// `.`, `;`, `?`, or an operator after a close delimiter token.
1160-
//
1161-
// let a = std::process::Command::new("echo")
1162-
// .arg("1")
1163-
// ,arg("2")
1164-
// ^
1165-
// https://github.com/rust-lang/rust/issues/72253
1166-
self.expect(&token::Semi)?;
1167-
return Ok(());
1168-
} else if self.look_ahead(1, |t| {
1169-
t == &token::CloseDelim(token::Brace) || t.can_begin_expr() && t.kind != token::Colon
1170-
}) && [token::Comma, token::Colon].contains(&self.token.kind)
1171-
{
1172-
// Likely typo: `,` → `;` or `:` → `;`. This is triggered if the current token is
1173-
// either `,` or `:`, and the next token could either start a new statement or is a
1174-
// block close. For example:
1175-
//
1176-
// let x = 32:
1177-
// let y = 42;
1178-
self.bump();
1179-
let sp = self.prev_token.span;
1180-
self.struct_span_err(sp, &msg)
1181-
.span_suggestion_short(sp, "change this to `;`", ";".to_string(), appl)
1182-
.emit();
1183-
return Ok(());
1184-
} else if self.look_ahead(0, |t| {
1185-
t == &token::CloseDelim(token::Brace)
1186-
|| (
1187-
t.can_begin_expr() && t != &token::Semi && t != &token::Pound
1188-
// Avoid triggering with too many trailing `#` in raw string.
1189-
)
1190-
}) {
1191-
// Missing semicolon typo. This is triggered if the next token could either start a
1192-
// new statement or is a block close. For example:
1193-
//
1194-
// let x = 32
1195-
// let y = 42;
1196-
let sp = self.prev_token.span.shrink_to_hi();
1197-
self.struct_span_err(sp, &msg)
1198-
.span_label(self.token.span, "unexpected token")
1199-
.span_suggestion_short(sp, "add `;` here", ";".to_string(), appl)
1200-
.emit();
1201-
return Ok(());
1202-
}
12031203
self.expect(&token::Semi).map(drop) // Error unconditionally
12041204
}
12051205

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-rustfix
2+
// Parser should know when a semicolon is missing.
3+
// https://github.com/rust-lang/rust/issues/87197
4+
5+
fn main() {
6+
let x = 100; //~ ERROR: expected `;`
7+
println!("{}", x); //~ ERROR: expected `;`
8+
let y = 200; //~ ERROR: expected `;`
9+
println!("{}", y);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-rustfix
2+
// Parser should know when a semicolon is missing.
3+
// https://github.com/rust-lang/rust/issues/87197
4+
5+
fn main() {
6+
let x = 100 //~ ERROR: expected `;`
7+
println!("{}", x) //~ ERROR: expected `;`
8+
let y = 200 //~ ERROR: expected `;`
9+
println!("{}", y);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: expected `;`, found `println`
2+
--> $DIR/issue-87197-missing-semicolon.rs:6:16
3+
|
4+
LL | let x = 100
5+
| ^ help: add `;` here
6+
LL | println!("{}", x)
7+
| ------- unexpected token
8+
9+
error: expected `;`, found keyword `let`
10+
--> $DIR/issue-87197-missing-semicolon.rs:7:22
11+
|
12+
LL | println!("{}", x)
13+
| ^ help: add `;` here
14+
LL | let y = 200
15+
| --- unexpected token
16+
17+
error: expected `;`, found `println`
18+
--> $DIR/issue-87197-missing-semicolon.rs:8:16
19+
|
20+
LL | let y = 200
21+
| ^ help: add `;` here
22+
LL | println!("{}", y);
23+
| ------- unexpected token
24+
25+
error: aborting due to 3 previous errors
26+
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
2-
assert_eq!(1, 2)
3-
assert_eq!(3, 4) //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `assert_eq`
2+
assert_eq!(1, 2) //~ ERROR: expected `;`
3+
assert_eq!(3, 4) //~ ERROR: expected `;`
44
println!("hello");
55
}
+13-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `assert_eq`
2-
--> $DIR/macros-no-semicolon.rs:3:5
1+
error: expected `;`, found `assert_eq`
2+
--> $DIR/macros-no-semicolon.rs:2:21
33
|
44
LL | assert_eq!(1, 2)
5-
| - expected one of `.`, `;`, `?`, `}`, or an operator
5+
| ^ help: add `;` here
66
LL | assert_eq!(3, 4)
7-
| ^^^^^^^^^ unexpected token
7+
| --------- unexpected token
88

9-
error: aborting due to previous error
9+
error: expected `;`, found `println`
10+
--> $DIR/macros-no-semicolon.rs:3:21
11+
|
12+
LL | assert_eq!(3, 4)
13+
| ^ help: add `;` here
14+
LL | println!("hello");
15+
| ------- unexpected token
16+
17+
error: aborting due to 2 previous errors
1018

0 commit comments

Comments
 (0)