Skip to content

Commit d21b852

Browse files
estebankpietroalbini
authored andcommitted
Emit missing unclosed delimiter errors
1 parent d5b3606 commit d21b852

File tree

7 files changed

+56
-55
lines changed

7 files changed

+56
-55
lines changed

src/librustc_metadata/cstore_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,8 @@ impl cstore::CStore {
439439

440440
let source_file = sess.parse_sess.source_map().new_source_file(source_name, def.body);
441441
let local_span = Span::new(source_file.start_pos, source_file.end_pos, NO_EXPANSION);
442-
let (body, errors) = source_file_to_stream(&sess.parse_sess, source_file, None);
443-
emit_unclosed_delims(&errors, &sess.diagnostic());
442+
let (body, mut errors) = source_file_to_stream(&sess.parse_sess, source_file, None);
443+
emit_unclosed_delims(&mut errors, &sess.diagnostic());
444444

445445
// Mark the attrs as used
446446
let attrs = data.get_item_attrs(id.index, sess);

src/libsyntax/parse/parser.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7785,7 +7785,10 @@ impl<'a> Parser<'a> {
77857785
attributes_allowed: bool,
77867786
) -> PResult<'a, Option<P<Item>>> {
77877787
let (ret, tokens) = self.collect_tokens(|this| {
7788-
this.parse_item_implementation(attrs, macros_allowed, attributes_allowed)
7788+
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);
7791+
item
77897792
})?;
77907793

77917794
// Once we've parsed an item and recorded the tokens we got while
@@ -8532,8 +8535,8 @@ impl<'a> Parser<'a> {
85328535
module: self.parse_mod_items(&token::Eof, lo)?,
85338536
span: lo.to(self.span),
85348537
});
8535-
emit_unclosed_delims(&self.unclosed_delims, self.diagnostic());
8536-
self.unclosed_delims.clear();
8538+
let diag = self.diagnostic();
8539+
emit_unclosed_delims(&mut self.unclosed_delims, diag);
85378540
krate
85388541
}
85398542

@@ -8564,8 +8567,8 @@ impl<'a> Parser<'a> {
85648567
}
85658568
}
85668569

8567-
pub fn emit_unclosed_delims(unclosed_delims: &[UnmatchedBrace], handler: &errors::Handler) {
8568-
for unmatched in unclosed_delims {
8570+
pub fn emit_unclosed_delims(unclosed_delims: &mut Vec<UnmatchedBrace>, handler: &errors::Handler) {
8571+
for unmatched in unclosed_delims.iter() {
85698572
let mut err = handler.struct_span_err(unmatched.found_span, &format!(
85708573
"incorrect close delimiter: `{}`",
85718574
pprust::token_to_string(&token::Token::CloseDelim(unmatched.found_delim)),
@@ -8579,4 +8582,5 @@ pub fn emit_unclosed_delims(unclosed_delims: &[UnmatchedBrace], handler: &errors
85798582
}
85808583
err.emit();
85818584
}
8585+
unclosed_delims.clear();
85828586
}

src/libsyntax/parse/token.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -675,9 +675,9 @@ impl Nonterminal {
675675
// FIXME(#43081): Avoid this pretty-print + reparse hack
676676
let source = pprust::nonterminal_to_string(self);
677677
let filename = FileName::macro_expansion_source_code(&source);
678-
let (tokens_for_real, errors) =
678+
let (tokens_for_real, mut errors) =
679679
parse_stream_from_source_str(filename, source, sess, Some(span));
680-
emit_unclosed_delims(&errors, &sess.span_diagnostic);
680+
emit_unclosed_delims(&mut errors, &sess.span_diagnostic);
681681

682682
// During early phases of the compiler the AST could get modified
683683
// directly (e.g., attributes added or removed) and the internal cache
@@ -740,13 +740,13 @@ fn prepend_attrs(sess: &ParseSess,
740740
let source = pprust::attr_to_string(attr);
741741
let macro_filename = FileName::macro_expansion_source_code(&source);
742742
if attr.is_sugared_doc {
743-
let (stream, errors) = parse_stream_from_source_str(
743+
let (stream, mut errors) = parse_stream_from_source_str(
744744
macro_filename,
745745
source,
746746
sess,
747747
Some(span),
748748
);
749-
emit_unclosed_delims(&errors, &sess.span_diagnostic);
749+
emit_unclosed_delims(&mut errors, &sess.span_diagnostic);
750750
builder.push(stream);
751751
continue
752752
}
@@ -763,13 +763,13 @@ fn prepend_attrs(sess: &ParseSess,
763763
// ... and for more complicated paths, fall back to a reparse hack that
764764
// should eventually be removed.
765765
} else {
766-
let (stream, errors) = parse_stream_from_source_str(
766+
let (stream, mut errors) = parse_stream_from_source_str(
767767
macro_filename,
768768
source,
769769
sess,
770770
Some(span),
771771
);
772-
emit_unclosed_delims(&errors, &sess.span_diagnostic);
772+
emit_unclosed_delims(&mut errors, &sess.span_diagnostic);
773773
brackets.push(stream);
774774
}
775775

src/libsyntax_ext/proc_macro_server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,13 +410,13 @@ impl server::TokenStream for Rustc<'_> {
410410
stream.is_empty()
411411
}
412412
fn from_str(&mut self, src: &str) -> Self::TokenStream {
413-
let (tokens, errors) = parse::parse_stream_from_source_str(
413+
let (tokens, mut errors) = parse::parse_stream_from_source_str(
414414
FileName::proc_macro_source_code(src.clone()),
415415
src.to_string(),
416416
self.sess,
417417
Some(self.call_site),
418418
);
419-
emit_unclosed_delims(&errors, &self.sess.span_diagnostic);
419+
emit_unclosed_delims(&mut errors, &self.sess.span_diagnostic);
420420
tokens
421421
}
422422
fn to_string(&mut self, stream: &Self::TokenStream) -> String {

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

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

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: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@ pub mod raw {
1010
pub fn ensure_dir_exists<P: AsRef<Path>, F: FnOnce(&Path)>(path: P,
1111
callback: F)
1212
-> io::Result<bool> {
13-
if !is_directory(path.as_ref()) { //~ ERROR: cannot find function `is_directory`
14-
callback(path.as_ref(); //~ ERROR expected one of
15-
fs::create_dir_all(path.as_ref()).map(|()| true) //~ ERROR: mismatched types
16-
//~^ expected (), found enum `std::result::Result`
17-
//~| expected type `()`
18-
//~| found type `std::result::Result<bool, std::io::Error>`
19-
//~| expected one of
13+
if !is_directory(path.as_ref()) {
14+
//~^ ERROR cannot find function `is_directory`
15+
callback(path.as_ref();
16+
//~^ ERROR expected one of
17+
//~| ERROR this function takes 1 parameter but 2 parameters were supplied
18+
fs::create_dir_all(path.as_ref()).map(|()| true)
2019
} else {
21-
//~^ ERROR: expected one of
22-
//~| unexpected token
20+
//~^ ERROR incorrect close delimiter: `}`
2321
Ok(false);
2422
}
2523

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
1-
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `;`
2-
--> $DIR/token-error-correct-3.rs:14:35
3-
|
4-
LL | callback(path.as_ref(); //~ ERROR expected one of
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:20:9
1+
error: incorrect close delimiter: `}`
2+
--> $DIR/token-error-correct-3.rs:19:9
123
|
13-
LL | fs::create_dir_all(path.as_ref()).map(|()| true) //~ ERROR: mismatched types
14-
| - expected one of `.`, `;`, `?`, `}`, or an operator here
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
159
...
1610
LL | } else {
17-
| ^ unexpected token
11+
| ^ incorrect close delimiter
12+
13+
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `;`
14+
--> $DIR/token-error-correct-3.rs:15:35
15+
|
16+
LL | callback(path.as_ref();
17+
| ^ expected one of `)`, `,`, `.`, `?`, or an operator here
1818

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

25-
error[E0308]: mismatched types
25+
error[E0057]: this function takes 1 parameter but 2 parameters were supplied
2626
--> $DIR/token-error-correct-3.rs:15:13
2727
|
28-
LL | fs::create_dir_all(path.as_ref()).map(|()| true) //~ ERROR: mismatched types
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try adding a semicolon: `;`
30-
| |
31-
| expected (), found enum `std::result::Result`
32-
|
33-
= note: expected type `()`
34-
found type `std::result::Result<bool, std::io::Error>`
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
3534

3635
error: aborting due to 4 previous errors
3736

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

0 commit comments

Comments
 (0)