Skip to content

Commit 181c1bd

Browse files
committed
Deduplicate unmatched_delims in rustc_parse to reduce confusion
Signed-off-by: xizheyin <[email protected]>
1 parent 229be21 commit 181c1bd

19 files changed

+62
-84
lines changed

compiler/rustc_parse/src/lexer/diagnostics.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ pub(super) fn same_indentation_level(sm: &SourceMap, open_sp: Span, close_sp: Sp
3434

3535
// When we get a `)` or `]` for `{`, we should emit help message here
3636
// it's more friendly compared to report `unmatched error` in later phase
37-
fn report_missing_open_delim(err: &mut Diag<'_>, unmatched_delims: &[UnmatchedDelim]) -> bool {
37+
pub(super) fn report_missing_open_delim(
38+
err: &mut Diag<'_>,
39+
unmatched_delims: &mut Vec<UnmatchedDelim>,
40+
) -> bool {
3841
let mut reported_missing_open = false;
39-
for unmatch_brace in unmatched_delims.iter() {
42+
unmatched_delims.retain(|unmatch_brace| {
4043
if let Some(delim) = unmatch_brace.found_delim
4144
&& matches!(delim, Delimiter::Parenthesis | Delimiter::Bracket)
4245
{
@@ -45,13 +48,20 @@ fn report_missing_open_delim(err: &mut Diag<'_>, unmatched_delims: &[UnmatchedDe
4548
Delimiter::Bracket => "[",
4649
_ => unreachable!(),
4750
};
51+
52+
if let Some(unclosed_span) = unmatch_brace.unclosed_span {
53+
err.span_label(unclosed_span, "the nearest open delimiter");
54+
}
4855
err.span_label(
4956
unmatch_brace.found_span.shrink_to_lo(),
5057
format!("missing open `{missed_open}` for this delimiter"),
5158
);
5259
reported_missing_open = true;
60+
false
61+
} else {
62+
true
5363
}
54-
}
64+
});
5565
reported_missing_open
5666
}
5767

@@ -61,10 +71,6 @@ pub(super) fn report_suspicious_mismatch_block(
6171
sm: &SourceMap,
6272
delim: Delimiter,
6373
) {
64-
if report_missing_open_delim(err, &diag_info.unmatched_delims) {
65-
return;
66-
}
67-
6874
let mut matched_spans: Vec<(Span, bool)> = diag_info
6975
.matching_block_spans
7076
.iter()

compiler/rustc_parse/src/lexer/tokentrees.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, Toke
33
use rustc_ast_pretty::pprust::token_to_string;
44
use rustc_errors::Diag;
55

6-
use super::diagnostics::{report_suspicious_mismatch_block, same_indentation_level};
6+
use super::diagnostics::{
7+
report_missing_open_delim, report_suspicious_mismatch_block, same_indentation_level,
8+
};
79
use super::{Lexer, UnmatchedDelim};
810

911
impl<'psess, 'src> Lexer<'psess, 'src> {
@@ -244,7 +246,16 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
244246
let msg = format!("unexpected closing delimiter: `{token_str}`");
245247
let mut err = self.dcx().struct_span_err(self.token.span, msg);
246248

247-
report_suspicious_mismatch_block(&mut err, &self.diag_info, self.psess.source_map(), delim);
249+
// if there is no missing open delim, report suspicious mismatch block
250+
if !report_missing_open_delim(&mut err, &mut self.diag_info.unmatched_delims) {
251+
report_suspicious_mismatch_block(
252+
&mut err,
253+
&self.diag_info,
254+
self.psess.source_map(),
255+
delim,
256+
);
257+
}
258+
248259
err.span_label(self.token.span, "unexpected closing delimiter");
249260
err
250261
}

tests/ui/parser/deli-ident-issue-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn main() {
22
if 1 < 2 {
3-
let _a = vec!]; //~ ERROR mismatched closing delimiter
3+
let _a = vec!];
44
}
55
} //~ ERROR unexpected closing delimiter
66

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
error: mismatched closing delimiter: `]`
2-
--> $DIR/deli-ident-issue-2.rs:2:14
3-
|
4-
LL | if 1 < 2 {
5-
| ^ unclosed delimiter
6-
LL | let _a = vec!];
7-
| ^ mismatched closing delimiter
8-
91
error: unexpected closing delimiter: `}`
102
--> $DIR/deli-ident-issue-2.rs:5:1
113
|
4+
LL | if 1 < 2 {
5+
| - the nearest open delimiter
126
LL | let _a = vec!];
137
| - missing open `[` for this delimiter
148
LL | }
159
LL | }
1610
| ^ unexpected closing delimiter
1711

18-
error: aborting due to 2 previous errors
12+
error: aborting due to 1 previous error
1913

tests/ui/parser/issues/issue-104367.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ LL | d: [u32; {
1818
LL | #![cfg] {
1919
| - unclosed delimiter
2020
LL | #![w,)
21-
| - missing open `(` for this delimiter
2221
LL |
2322
| ^
2423

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
//@ compile-flags: -Zunpretty=ast-tree
22
#![c={#![c[)x //~ ERROR mismatched closing delimiter
3-
//~ ERROR this file contains an unclosed delimiter
3+
//~ ERROR this file contains an unclosed delimiter

tests/ui/parser/issues/issue-105209.stderr

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ LL | #![c={#![c[)x
77
| unclosed delimiter
88

99
error: this file contains an unclosed delimiter
10-
--> $DIR/issue-105209.rs:3:68
10+
--> $DIR/issue-105209.rs:3:56
1111
|
1212
LL | #![c={#![c[)x
13-
| - - - - missing open `(` for this delimiter
14-
| | | |
15-
| | | unclosed delimiter
13+
| - - - unclosed delimiter
14+
| | |
1615
| | unclosed delimiter
1716
| unclosed delimiter
1817
LL |
19-
| ^
18+
| ^
2019

2120
error: aborting due to 2 previous errors
2221

tests/ui/parser/issues/issue-62973.stderr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ error: this file contains an unclosed delimiter
1818
--> $DIR/issue-62973.rs:10:2
1919
|
2020
LL | fn p() { match s { v, E { [) {) }
21-
| - - - - missing open `(` for this delimiter
22-
| | | |
23-
| | | missing open `(` for this delimiter
24-
| | unclosed delimiter
21+
| - - unclosed delimiter
22+
| |
2523
| unclosed delimiter
2624
LL |
2725
LL |

tests/ui/parser/issues/issue-63116.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ error: this file contains an unclosed delimiter
1010
--> $DIR/issue-63116.rs:4:18
1111
|
1212
LL | impl W <s(f;Y(;]
13-
| - -^
14-
| | |
15-
| | missing open `[` for this delimiter
13+
| - ^
14+
| |
1615
| unclosed delimiter
1716

1817
error: aborting due to 2 previous errors

tests/ui/parser/issues/issue-67377-invalid-syntax-in-enum-discriminant.stderr

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,14 @@ LL | V = [Vec::new; { [0].len() ].len() as isize,
2828
error: this file contains an unclosed delimiter
2929
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:23:65
3030
|
31-
LL | V = [PhantomData; { [ () ].len() ].len() as isize,
32-
| - missing open `[` for this delimiter
33-
...
34-
LL | V = [Vec::new; { [].len() ].len() as isize,
35-
| - missing open `[` for this delimiter
36-
...
3731
LL | mod c {
3832
| - unclosed delimiter
3933
LL | enum Bug {
40-
LL | V = [Vec::new; { [0].len() ].len() as isize,
41-
| - missing open `[` for this delimiter
34+
| - this delimiter might not be properly closed...
4235
...
36+
LL | }
37+
| - ...as it matches this but it has different indentation
38+
LL |
4339
LL | fn main() {}
4440
| ^
4541

0 commit comments

Comments
 (0)