Skip to content

Commit a460b4b

Browse files
estebankpietroalbini
authored andcommitted
Collect unclosed delimiters in parent parser
1 parent d21b852 commit a460b4b

File tree

4 files changed

+45
-35
lines changed

4 files changed

+45
-35
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,9 +1510,13 @@ impl<'a> Parser<'a> {
15101510
pub fn parse_trait_item(&mut self, at_end: &mut bool) -> PResult<'a, TraitItem> {
15111511
maybe_whole!(self, NtTraitItem, |x| x);
15121512
let attrs = self.parse_outer_attributes()?;
1513+
let mut unclosed_delims = vec![];
15131514
let (mut item, tokens) = self.collect_tokens(|this| {
1514-
this.parse_trait_item_(at_end, attrs)
1515+
let item = this.parse_trait_item_(at_end, attrs);
1516+
unclosed_delims.append(&mut this.unclosed_delims);
1517+
item
15151518
})?;
1519+
self.unclosed_delims.append(&mut unclosed_delims);
15161520
// See `parse_item` for why this clause is here.
15171521
if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
15181522
item.tokens = Some(tokens);
@@ -6462,9 +6466,13 @@ impl<'a> Parser<'a> {
64626466
pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, ImplItem> {
64636467
maybe_whole!(self, NtImplItem, |x| x);
64646468
let attrs = self.parse_outer_attributes()?;
6469+
let mut unclosed_delims = vec![];
64656470
let (mut item, tokens) = self.collect_tokens(|this| {
6466-
this.parse_impl_item_(at_end, attrs)
6471+
let item = this.parse_impl_item_(at_end, attrs);
6472+
unclosed_delims.append(&mut this.unclosed_delims);
6473+
item
64676474
})?;
6475+
self.unclosed_delims.append(&mut unclosed_delims);
64686476

64696477
// See `parse_item` for why this clause is here.
64706478
if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
@@ -7784,12 +7792,13 @@ impl<'a> Parser<'a> {
77847792
macros_allowed: bool,
77857793
attributes_allowed: bool,
77867794
) -> PResult<'a, Option<P<Item>>> {
7795+
let mut unclosed_delims = vec![];
77877796
let (ret, tokens) = self.collect_tokens(|this| {
77887797
let item = this.parse_item_implementation(attrs, macros_allowed, attributes_allowed);
7789-
let diag = this.diagnostic();
7790-
emit_unclosed_delims(&mut this.unclosed_delims, diag);
7798+
unclosed_delims.append(&mut this.unclosed_delims);
77917799
item
77927800
})?;
7801+
self.unclosed_delims.append(&mut unclosed_delims);
77937802

77947803
// Once we've parsed an item and recorded the tokens we got while
77957804
// parsing we may want to store `tokens` into the item we're about to

src/test/ui/parser-recovery-2.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error: unexpected token: `;`
2+
--> $DIR/parser-recovery-2.rs:12:15
3+
|
4+
LL | let x = y.; //~ ERROR unexpected token
5+
| ^
6+
17
error: incorrect close delimiter: `)`
28
--> $DIR/parser-recovery-2.rs:8:5
39
|
@@ -7,12 +13,6 @@ LL | let x = foo(); //~ ERROR cannot find function `foo` in this scope
713
LL | ) //~ ERROR incorrect close delimiter: `)`
814
| ^ incorrect close delimiter
915

10-
error: unexpected token: `;`
11-
--> $DIR/parser-recovery-2.rs:12:15
12-
|
13-
LL | let x = y.; //~ ERROR unexpected token
14-
| ^
15-
1616
error[E0425]: cannot find function `foo` in this scope
1717
--> $DIR/parser-recovery-2.rs:7:17
1818
|

src/test/ui/resolve/token-error-correct-3.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ pub mod raw {
1414
//~^ ERROR cannot find function `is_directory`
1515
callback(path.as_ref();
1616
//~^ ERROR expected one of
17-
//~| ERROR this function takes 1 parameter but 2 parameters were supplied
1817
fs::create_dir_all(path.as_ref()).map(|()| true)
18+
//~^ ERROR mismatched types
1919
} else {
20-
//~^ ERROR incorrect close delimiter: `}`
20+
//~^ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `)`
2121
Ok(false);
2222
}
2323

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1-
error: incorrect close delimiter: `}`
2-
--> $DIR/token-error-correct-3.rs:19:9
3-
|
4-
LL | if !is_directory(path.as_ref()) {
5-
| - close delimiter possibly meant for this
6-
LL | //~^ ERROR cannot find function `is_directory`
7-
LL | callback(path.as_ref();
8-
| - un-closed delimiter
9-
...
10-
LL | } else {
11-
| ^ incorrect close delimiter
12-
131
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `;`
142
--> $DIR/token-error-correct-3.rs:15:35
153
|
164
LL | callback(path.as_ref();
17-
| ^ expected one of `)`, `,`, `.`, `?`, or an operator here
5+
| - ^
6+
| | |
7+
| | help: `)` may belong here
8+
| unclosed delimiter
9+
10+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `)`
11+
--> $DIR/token-error-correct-3.rs:19:9
12+
|
13+
LL | fs::create_dir_all(path.as_ref()).map(|()| true)
14+
| - expected one of `.`, `;`, `?`, `}`, or an operator here
15+
LL | //~^ ERROR mismatched types
16+
LL | } else {
17+
| ^ unexpected token
1818

1919
error[E0425]: cannot find function `is_directory` in this scope
2020
--> $DIR/token-error-correct-3.rs:13:13
2121
|
2222
LL | if !is_directory(path.as_ref()) {
2323
| ^^^^^^^^^^^^ not found in this scope
2424

25-
error[E0057]: this function takes 1 parameter but 2 parameters were supplied
26-
--> $DIR/token-error-correct-3.rs:15:13
25+
error[E0308]: mismatched types
26+
--> $DIR/token-error-correct-3.rs:17:13
27+
|
28+
LL | fs::create_dir_all(path.as_ref()).map(|()| true)
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try adding a semicolon: `;`
30+
| |
31+
| expected (), found enum `std::result::Result`
2732
|
28-
LL | / callback(path.as_ref();
29-
LL | | //~^ ERROR expected one of
30-
LL | | //~| ERROR this function takes 1 parameter but 2 parameters were supplied
31-
LL | | fs::create_dir_all(path.as_ref()).map(|()| true)
32-
LL | | } else {
33-
| |_________^ expected 1 parameter
33+
= note: expected type `()`
34+
found type `std::result::Result<bool, std::io::Error>`
3435

3536
error: aborting due to 4 previous errors
3637

37-
Some errors occurred: E0057, E0425.
38-
For more information about an error, try `rustc --explain E0057`.
38+
Some errors occurred: E0308, E0425.
39+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)