Skip to content

Commit 661f2d3

Browse files
committed
Remove unmatched_delims that unexpected closing delimeter error report to reduce confusion
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent 1b82aec commit 661f2d3

26 files changed

+49
-174
lines changed

compiler/rustc_parse/src/lexer/diagnostics.rs

Lines changed: 11 additions & 5 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+
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
{
@@ -50,18 +53,21 @@ fn report_missing_open_delim(err: &mut Diag<'_>, unmatched_delims: &[UnmatchedDe
5053
format!("missing open `{missed_open}` for this delimiter"),
5154
);
5255
reported_missing_open = true;
56+
false
57+
} else {
58+
true
5359
}
54-
}
60+
});
5561
reported_missing_open
5662
}
5763

5864
pub(super) fn report_suspicious_mismatch_block(
5965
err: &mut Diag<'_>,
60-
diag_info: &TokenTreeDiagInfo,
66+
diag_info: &mut TokenTreeDiagInfo,
6167
sm: &SourceMap,
6268
delim: Delimiter,
6369
) {
64-
if report_missing_open_delim(err, &diag_info.unmatched_delims) {
70+
if report_missing_open_delim(err, &mut diag_info.unmatched_delims) {
6571
return;
6672
}
6773

compiler/rustc_parse/src/lexer/tokentrees.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
7777
);
7878
}
7979

80-
if let Some((delim, _)) = self.diag_info.open_delimiters.last() {
80+
if let Some((delim, _)) = self.diag_info.open_delimiters.last().cloned() {
8181
report_suspicious_mismatch_block(
8282
&mut err,
83-
&self.diag_info,
83+
&mut self.diag_info,
8484
self.psess.source_map(),
85-
*delim,
85+
delim,
8686
)
8787
}
8888
err
@@ -238,13 +238,18 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
238238
this_spacing
239239
}
240240

241-
fn close_delim_err(&mut self, delim: Delimiter) -> Diag<'psess> {
241+
fn close_delim_err(&mut self, close_delim: Delimiter) -> Diag<'psess> {
242242
// An unexpected closing delimiter (i.e., there is no matching opening delimiter).
243243
let token_str = token_to_string(&self.token);
244244
let msg = format!("unexpected closing delimiter: `{token_str}`");
245245
let mut err = self.dcx().struct_span_err(self.token.span, msg);
246246

247-
report_suspicious_mismatch_block(&mut err, &self.diag_info, self.psess.source_map(), delim);
247+
report_suspicious_mismatch_block(
248+
&mut err,
249+
&mut self.diag_info,
250+
self.psess.source_map(),
251+
close_delim,
252+
);
248253
err.span_label(self.token.span, "unexpected closing delimiter");
249254
err
250255
}

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: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
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
|
@@ -15,5 +7,5 @@ LL | }
157
LL | }
168
| ^ unexpected closing delimiter
179

18-
error: aborting due to 2 previous errors
10+
error: aborting due to 1 previous error
1911

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
struct S {
33
d: [u32; {
44
#![cfg] {
5-
#![w,) //~ ERROR mismatched closing delimiter
5+
#![w,)
66
//~ ERROR this file contains an unclosed delimiter
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error: mismatched closing delimiter: `)`
2-
--> $DIR/issue-104367.rs:5:15
3-
|
4-
LL | #![w,)
5-
| ^ ^ mismatched closing delimiter
6-
| |
7-
| unclosed delimiter
8-
91
error: this file contains an unclosed delimiter
102
--> $DIR/issue-104367.rs:6:71
113
|
@@ -22,5 +14,5 @@ LL | #![w,)
2214
LL |
2315
| ^
2416

25-
error: aborting due to 2 previous errors
17+
error: aborting due to 1 previous error
2618

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
//@ compile-flags: -Zunpretty=ast-tree
2-
#![c={#![c[)x //~ ERROR mismatched closing delimiter
2+
#![c={#![c[)x
33
//~ ERROR this file contains an unclosed delimiter
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error: mismatched closing delimiter: `)`
2-
--> $DIR/issue-105209.rs:2:11
3-
|
4-
LL | #![c={#![c[)x
5-
| ^^ mismatched closing delimiter
6-
| |
7-
| unclosed delimiter
8-
91
error: this file contains an unclosed delimiter
102
--> $DIR/issue-105209.rs:3:68
113
|
@@ -18,5 +10,5 @@ LL | #![c={#![c[)x
1810
LL |
1911
| ^
2012

21-
error: aborting due to 2 previous errors
13+
error: aborting due to 1 previous error
2214

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
fn main() {}
44

5-
//~vvv ERROR mismatched closing delimiter: `)`
6-
//~vv ERROR mismatched closing delimiter: `)`
75
//~vvv ERROR this file contains an unclosed delimiter
86
fn p() { match s { v, E { [) {) }
97

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
1-
error: mismatched closing delimiter: `)`
2-
--> $DIR/issue-62973.rs:8:27
3-
|
4-
LL | fn p() { match s { v, E { [) {) }
5-
| ^^ mismatched closing delimiter
6-
| |
7-
| unclosed delimiter
8-
9-
error: mismatched closing delimiter: `)`
10-
--> $DIR/issue-62973.rs:8:30
11-
|
12-
LL | fn p() { match s { v, E { [) {) }
13-
| ^^ mismatched closing delimiter
14-
| |
15-
| unclosed delimiter
16-
171
error: this file contains an unclosed delimiter
18-
--> $DIR/issue-62973.rs:10:2
2+
--> $DIR/issue-62973.rs:8:2
193
|
204
LL | fn p() { match s { v, E { [) {) }
215
| - - - - missing open `(` for this delimiter
@@ -27,5 +11,5 @@ LL |
2711
LL |
2812
| ^
2913

30-
error: aborting due to 3 previous errors
14+
error: aborting due to 1 previous error
3115

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
// fixed by #66361
2-
//~vv ERROR mismatched closing delimiter: `]`
32
//~v ERROR this file contains an unclosed delimiter
43
impl W <s(f;Y(;]
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
error: mismatched closing delimiter: `]`
2-
--> $DIR/issue-63116.rs:4:14
3-
|
4-
LL | impl W <s(f;Y(;]
5-
| ^ ^ mismatched closing delimiter
6-
| |
7-
| unclosed delimiter
8-
91
error: this file contains an unclosed delimiter
10-
--> $DIR/issue-63116.rs:4:18
2+
--> $DIR/issue-63116.rs:3:18
113
|
124
LL | impl W <s(f;Y(;]
135
| - -^
146
| | |
157
| | missing open `[` for this delimiter
168
| unclosed delimiter
179

18-
error: aborting due to 2 previous errors
10+
error: aborting due to 1 previous error
1911

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@ mod a {
33

44
enum Bug {
55
V = [PhantomData; { [ () ].len() ].len() as isize,
6-
//~^ ERROR mismatched closing delimiter: `]`
76
}
87
}
98

109
mod b {
1110
enum Bug {
1211
V = [Vec::new; { [].len() ].len() as isize,
13-
//~^ ERROR mismatched closing delimiter: `]`
1412
}
1513
}
1614

1715
mod c {
1816
enum Bug {
1917
V = [Vec::new; { [0].len() ].len() as isize,
20-
//~^ ERROR mismatched closing delimiter: `]`
2118
}
2219

2320
fn main() {} //~ ERROR this file contains an unclosed delimiter
Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,5 @@
1-
error: mismatched closing delimiter: `]`
2-
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:27
3-
|
4-
LL | V = [PhantomData; { [ () ].len() ].len() as isize,
5-
| - ^ ^ mismatched closing delimiter
6-
| | |
7-
| | unclosed delimiter
8-
| closing delimiter possibly meant for this
9-
10-
error: mismatched closing delimiter: `]`
11-
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:12:24
12-
|
13-
LL | V = [Vec::new; { [].len() ].len() as isize,
14-
| - ^ ^ mismatched closing delimiter
15-
| | |
16-
| | unclosed delimiter
17-
| closing delimiter possibly meant for this
18-
19-
error: mismatched closing delimiter: `]`
20-
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:19:24
21-
|
22-
LL | V = [Vec::new; { [0].len() ].len() as isize,
23-
| - ^ ^ mismatched closing delimiter
24-
| | |
25-
| | unclosed delimiter
26-
| closing delimiter possibly meant for this
27-
281
error: this file contains an unclosed delimiter
29-
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:23:65
2+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:20:65
303
|
314
LL | V = [PhantomData; { [ () ].len() ].len() as isize,
325
| - missing open `[` for this delimiter
@@ -43,5 +16,5 @@ LL | V = [Vec::new; { [0].len() ].len() as isize,
4316
LL | fn main() {}
4417
| ^
4518

46-
error: aborting due to 4 previous errors
19+
error: aborting due to 1 previous error
4720

tests/ui/parser/issues/issue-68987-unmatch-issue-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// FIXME: this case need more work to fix
22
// currently the TokenTree matching ')' with '{', which is not user friendly for diagnostics
33
async fn obstest() -> Result<> {
4-
let obs_connect = || -> Result<(), MyError) { //~ ERROR mismatched closing delimiter
4+
let obs_connect = || -> Result<(), MyError) {
55
async {
66
}
77
}
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error: mismatched closing delimiter: `)`
2-
--> $DIR/issue-68987-unmatch-issue-2.rs:3:32
3-
|
4-
LL | async fn obstest() -> Result<> {
5-
| ^ unclosed delimiter
6-
LL | let obs_connect = || -> Result<(), MyError) {
7-
| ^ mismatched closing delimiter
8-
91
error: unexpected closing delimiter: `}`
102
--> $DIR/issue-68987-unmatch-issue-2.rs:14:1
113
|
@@ -15,5 +7,5 @@ LL | let obs_connect = || -> Result<(), MyError) {
157
LL | }
168
| ^ unexpected closing delimiter
179

18-
error: aborting due to 2 previous errors
10+
error: aborting due to 1 previous error
1911

tests/ui/parser/issues/issue-68987-unmatch-issue-3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ fn f(i: u32, j: u32) {
33
let res = String::new();
44
let mut cnt = i;
55
while cnt < j {
6-
write!&mut res, " "); //~ ERROR mismatched closing delimiter
6+
write!&mut res, " ");
77
}
88
} //~ ERROR unexpected closing delimiter
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error: mismatched closing delimiter: `)`
2-
--> $DIR/issue-68987-unmatch-issue-3.rs:5:19
3-
|
4-
LL | while cnt < j {
5-
| ^ unclosed delimiter
6-
LL | write!&mut res, " ");
7-
| ^ mismatched closing delimiter
8-
91
error: unexpected closing delimiter: `}`
102
--> $DIR/issue-68987-unmatch-issue-3.rs:8:1
113
|
@@ -15,5 +7,5 @@ LL | }
157
LL | }
168
| ^ unexpected closing delimiter
179

18-
error: aborting due to 2 previous errors
10+
error: aborting due to 1 previous error
1911

tests/ui/parser/issues/issue-81827.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22

33
fn main() {}
44

5-
//~vv ERROR mismatched closing delimiter: `]`
65
//~v ERROR this file contains an unclosed delimiter
76
fn r()->i{0|{#[cfg(r(0{]0
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
error: mismatched closing delimiter: `]`
2-
--> $DIR/issue-81827.rs:7:23
3-
|
4-
LL | fn r()->i{0|{#[cfg(r(0{]0
5-
| - ^^ mismatched closing delimiter
6-
| | |
7-
| | unclosed delimiter
8-
| closing delimiter possibly meant for this
9-
101
error: this file contains an unclosed delimiter
11-
--> $DIR/issue-81827.rs:7:27
2+
--> $DIR/issue-81827.rs:6:27
123
|
134
LL | fn r()->i{0|{#[cfg(r(0{]0
145
| - - - ^
@@ -17,5 +8,5 @@ LL | fn r()->i{0|{#[cfg(r(0{]0
178
| | unclosed delimiter
189
| unclosed delimiter
1910

20-
error: aborting due to 2 previous errors
11+
error: aborting due to 1 previous error
2112

tests/ui/parser/issues/unnessary-error-issue-138401.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
pub fn foo(x: i64) -> i64 {
2-
x.abs)
3-
//~^ ERROR mismatched closing delimiter
2+
x.abs)
43
}
54
//~^ ERROR unexpected closing delimiter: `}`
65

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
error: mismatched closing delimiter: `)`
2-
--> $DIR/unnessary-error-issue-138401.rs:1:27
3-
|
4-
LL | pub fn foo(x: i64) -> i64 {
5-
| ^ unclosed delimiter
6-
LL | x.abs)
7-
| ^ mismatched closing delimiter
8-
91
error: unexpected closing delimiter: `}`
10-
--> $DIR/unnessary-error-issue-138401.rs:4:1
2+
--> $DIR/unnessary-error-issue-138401.rs:3:1
113
|
12-
LL | x.abs)
4+
LL | x.abs)
135
| - missing open `(` for this delimiter
14-
LL |
156
LL | }
167
| ^ unexpected closing delimiter
178

18-
error: aborting due to 2 previous errors
9+
error: aborting due to 1 previous error
1910

0 commit comments

Comments
 (0)