Skip to content

Commit b2bada2

Browse files
committed
Bail when encountering a second unexpected token in the same span
1 parent e33c280 commit b2bada2

File tree

7 files changed

+25
-78
lines changed

7 files changed

+25
-78
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use crate::ThinVec;
4646
use crate::tokenstream::{self, DelimSpan, TokenTree, TokenStream, TreeAndJoint};
4747
use crate::symbol::{Symbol, keywords};
4848

49-
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
49+
use errors::{Applicability, DiagnosticBuilder, DiagnosticId, FatalError};
5050
use rustc_target::spec::abi::{self, Abi};
5151
use syntax_pos::{Span, MultiSpan, BytePos, FileName};
5252
use log::{debug, trace};
@@ -256,6 +256,7 @@ pub struct Parser<'a> {
256256
/// it gets removed from here. Every entry left at the end gets emitted as an independent
257257
/// error.
258258
crate unclosed_delims: Vec<UnmatchedBrace>,
259+
last_unexpected_token_span: Option<Span>,
259260
}
260261

261262

@@ -582,6 +583,7 @@ impl<'a> Parser<'a> {
582583
unmatched_angle_bracket_count: 0,
583584
max_angle_bracket_count: 0,
584585
unclosed_delims: Vec::new(),
586+
last_unexpected_token_span: None,
585587
};
586588

587589
let tok = parser.next_tok();
@@ -775,6 +777,8 @@ impl<'a> Parser<'a> {
775777
} else if inedible.contains(&self.token) {
776778
// leave it in the input
777779
Ok(false)
780+
} else if self.last_unexpected_token_span == Some(self.span) {
781+
FatalError.raise();
778782
} else {
779783
let mut expected = edible.iter()
780784
.map(|x| TokenType::Token(x.clone()))
@@ -802,6 +806,7 @@ impl<'a> Parser<'a> {
802806
(self.sess.source_map().next_point(self.prev_span),
803807
format!("expected {} here", expect)))
804808
};
809+
self.last_unexpected_token_span = Some(self.span);
805810
let mut err = self.fatal(&msg_exp);
806811
if self.token.is_ident_named("and") {
807812
err.span_suggestion_short(
@@ -6332,10 +6337,11 @@ impl<'a> Parser<'a> {
63326337
&token::CloseDelim(token::Paren), sep, parse_arg_fn)?;
63336338
fn_inputs.append(&mut input);
63346339
(fn_inputs, recovered)
6335-
} else if let Err(err) = self.expect_one_of(&[], &[]) {
6336-
return Err(err);
63376340
} else {
6338-
(vec![self_arg], true)
6341+
match self.expect_one_of(&[], &[]) {
6342+
Err(err) => return Err(err),
6343+
Ok(recovered) => (vec![self_arg], recovered),
6344+
}
63396345
}
63406346
} else {
63416347
self.parse_seq_to_before_end(&token::CloseDelim(token::Paren), sep, parse_arg_fn)?

src/test/ui/issues/issue-58856-1.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
struct A;
2+
13
impl A {
2-
//~^ ERROR cannot find type `A` in this scope
3-
fn b(self>
4+
fn b(self> {}
45
//~^ ERROR expected one of `)`, `,`, or `:`, found `>`
5-
//~| ERROR expected one of `->`, `where`, or `{`, found `>`
6-
//~| ERROR expected one of `->`, `async`, `const`, `crate`, `default`, `existential`,
76
}
87

98
fn main() {}
Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
11
error: expected one of `)`, `,`, or `:`, found `>`
2-
--> $DIR/issue-58856-1.rs:3:14
2+
--> $DIR/issue-58856-1.rs:4:14
33
|
4-
LL | fn b(self>
4+
LL | fn b(self> {}
55
| - ^
66
| | |
77
| | help: `)` may belong here
88
| unclosed delimiter
99

10-
error: expected one of `->`, `where`, or `{`, found `>`
11-
--> $DIR/issue-58856-1.rs:3:14
12-
|
13-
LL | fn b(self>
14-
| ^ expected one of `->`, `where`, or `{` here
15-
16-
error: expected one of `->`, `async`, `const`, `crate`, `default`, `existential`, `extern`, `fn`, `pub`, `type`, `unsafe`, `where`, or `}`, found `>`
17-
--> $DIR/issue-58856-1.rs:3:14
18-
|
19-
LL | fn b(self>
20-
| ^ expected one of 13 possible tokens here
21-
22-
error[E0412]: cannot find type `A` in this scope
23-
--> $DIR/issue-58856-1.rs:1:6
24-
|
25-
LL | impl A {
26-
| ^ not found in this scope
27-
28-
error: aborting due to 4 previous errors
10+
error: aborting due to previous error
2911

30-
For more information about this error, try `rustc --explain E0412`.

src/test/ui/issues/issue-58856-2.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
struct Empty;
2+
13
trait Howness {}
4+
25
impl Howness for () {
36
fn how_are_you(&self -> Empty {
47
//~^ ERROR expected one of `)` or `,`, found `->`
58
//~| ERROR method `how_are_you` is not a member of trait `Howness`
6-
//~| ERROR cannot find type `Empty` in this scope
79
Empty
8-
//~^ ERROR cannot find value `Empty` in this scope
910
}
1011
}
1112
//~^ ERROR expected one of `async`, `const`, `crate`, `default`, `existential`, `extern`, `fn`,
Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: expected one of `)` or `,`, found `->`
2-
--> $DIR/issue-58856-2.rs:3:26
2+
--> $DIR/issue-58856-2.rs:6:26
33
|
44
LL | fn how_are_you(&self -> Empty {
55
| - -^^
@@ -8,48 +8,23 @@ LL | fn how_are_you(&self -> Empty {
88
| unclosed delimiter
99

1010
error: expected one of `async`, `const`, `crate`, `default`, `existential`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `)`
11-
--> $DIR/issue-58856-2.rs:10:1
11+
--> $DIR/issue-58856-2.rs:11:1
1212
|
1313
LL | }
1414
| - expected one of 11 possible tokens here
1515
LL | }
1616
| ^ unexpected token
1717

1818
error[E0407]: method `how_are_you` is not a member of trait `Howness`
19-
--> $DIR/issue-58856-2.rs:3:5
19+
--> $DIR/issue-58856-2.rs:6:5
2020
|
2121
LL | / fn how_are_you(&self -> Empty {
2222
LL | | //~^ ERROR expected one of `)` or `,`, found `->`
2323
LL | | //~| ERROR method `how_are_you` is not a member of trait `Howness`
24-
LL | | //~| ERROR cannot find type `Empty` in this scope
2524
LL | | Empty
26-
LL | | //~^ ERROR cannot find value `Empty` in this scope
2725
LL | | }
2826
| |_____^ not a member of trait `Howness`
2927

30-
error[E0412]: cannot find type `Empty` in this scope
31-
--> $DIR/issue-58856-2.rs:3:29
32-
|
33-
LL | fn how_are_you(&self -> Empty {
34-
| ^^^^^ not found in this scope
35-
help: possible candidates are found in other modules, you can import them into scope
36-
|
37-
LL | use std::io::Empty;
38-
|
39-
LL | use std::iter::Empty;
40-
|
41-
42-
error[E0425]: cannot find value `Empty` in this scope
43-
--> $DIR/issue-58856-2.rs:7:9
44-
|
45-
LL | Empty
46-
| ^^^^^ not found in this scope
47-
help: possible candidate is found in another module, you can import it into scope
48-
|
49-
LL | use std::sync::mpsc::TryRecvError::Empty;
50-
|
51-
52-
error: aborting due to 5 previous errors
28+
error: aborting due to 3 previous errors
5329

54-
Some errors occurred: E0407, E0412, E0425.
55-
For more information about an error, try `rustc --explain E0407`.
30+
For more information about this error, try `rustc --explain E0407`.

src/test/ui/parser/recover-enum2.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ fn main() {
2525
// fail again
2626
enum Test4 {
2727
Nope(i32 {}) //~ ERROR: found `{`
28-
//~^ ERROR: found `{`
2928
}
3029
}
31-
// still recover later
32-
let bad_syntax = _; //~ ERROR: expected expression, found reserved identifier `_`
3330
}

src/test/ui/parser/recover-enum2.stderr

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,5 @@ error: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `{`
1010
LL | Nope(i32 {}) //~ ERROR: found `{`
1111
| ^ expected one of 7 possible tokens here
1212

13-
error: expected one of `!`, `&&`, `&`, `(`, `)`, `*`, `+`, `,`, `...`, `::`, `<`, `?`, `[`, `_`, `crate`, `dyn`, `extern`, `fn`, `for`, `impl`, `pub`, `unsafe`, `}`, or lifetime, found `{`
14-
--> $DIR/recover-enum2.rs:27:22
15-
|
16-
LL | Nope(i32 {}) //~ ERROR: found `{`
17-
| ^ expected one of 24 possible tokens here
18-
19-
error: expected expression, found reserved identifier `_`
20-
--> $DIR/recover-enum2.rs:32:22
21-
|
22-
LL | let bad_syntax = _; //~ ERROR: expected expression, found reserved identifier `_`
23-
| ^ expected expression
24-
25-
error: aborting due to 4 previous errors
13+
error: aborting due to 2 previous errors
2614

0 commit comments

Comments
 (0)