Skip to content

Commit f01d0c0

Browse files
committed
Exit when there are unmatched delims to avoid noisy diagnostics
1 parent f0bc76a commit f01d0c0

File tree

99 files changed

+252
-1277
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+252
-1277
lines changed

compiler/rustc_parse/src/lib.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,23 @@ pub fn maybe_file_to_stream(
204204
lexer::parse_token_trees(sess, src.as_str(), source_file.start_pos, override_span);
205205

206206
match token_trees {
207-
Ok(stream) => Ok((stream, unmatched_braces)),
208-
Err(err) => {
207+
Ok(stream) if unmatched_braces.is_empty() => Ok((stream, unmatched_braces)),
208+
_ => {
209+
// Return error if there are unmatched delimiters or unclosng delimiters.
210+
// We emit delimiter mismatch errors first, then emit the unclosing delimiter mismatch
211+
// because the delimiter mismatch is more likely to be the root cause of the
212+
209213
let mut buffer = Vec::with_capacity(1);
210-
err.buffer(&mut buffer);
211214
// Not using `emit_unclosed_delims` to use `db.buffer`
212215
for unmatched in unmatched_braces {
213216
if let Some(err) = make_unclosed_delims_error(unmatched, &sess) {
214217
err.buffer(&mut buffer);
215218
}
216219
}
220+
if let Err(err) = token_trees {
221+
// Add unclosing delimiter error
222+
err.buffer(&mut buffer);
223+
}
217224
Err(buffer)
218225
}
219226
}

src/librustdoc/doctest.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -769,24 +769,16 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
769769
match maybe_new_parser_from_source_str(&sess, filename, source.to_owned()) {
770770
Ok(p) => p,
771771
Err(_) => {
772-
debug!("Cannot build a parser to check mod attr so skipping...");
773-
return true;
772+
// If there is an unclosed delimiter, an error will be returned by the tokentrees.
773+
return false;
774774
}
775775
};
776776
// If a parsing error happened, it's very likely that the attribute is incomplete.
777777
if let Err(e) = parser.parse_attribute(InnerAttrPolicy::Permitted) {
778778
e.cancel();
779779
return false;
780780
}
781-
// We now check if there is an unclosed delimiter for the attribute. To do so, we look at
782-
// the `unclosed_delims` and see if the opening square bracket was closed.
783-
parser
784-
.unclosed_delims()
785-
.get(0)
786-
.map(|unclosed| {
787-
unclosed.unclosed_span.map(|s| s.lo()).unwrap_or(BytePos(0)) != BytePos(2)
788-
})
789-
.unwrap_or(true)
781+
true
790782
})
791783
})
792784
.unwrap_or(false)

tests/ui/lint/issue-104897.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// error-pattern: this file contains an unclosed delimiter
22
// error-pattern: this file contains an unclosed delimiter
33
// error-pattern: this file contains an unclosed delimiter
4-
// error-pattern: format argument must be a string literal
54

65
fn f(){(print!(á

tests/ui/lint/issue-104897.stderr

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this file contains an unclosed delimiter
2-
--> $DIR/issue-104897.rs:6:18
2+
--> $DIR/issue-104897.rs:5:18
33
|
44
LL | fn f(){(print!(á
55
| -- - ^
@@ -9,7 +9,7 @@ LL | fn f(){(print!(á
99
| unclosed delimiter
1010

1111
error: this file contains an unclosed delimiter
12-
--> $DIR/issue-104897.rs:6:18
12+
--> $DIR/issue-104897.rs:5:18
1313
|
1414
LL | fn f(){(print!(á
1515
| -- - ^
@@ -19,7 +19,7 @@ LL | fn f(){(print!(á
1919
| unclosed delimiter
2020

2121
error: this file contains an unclosed delimiter
22-
--> $DIR/issue-104897.rs:6:18
22+
--> $DIR/issue-104897.rs:5:18
2323
|
2424
LL | fn f(){(print!(á
2525
| -- - ^
@@ -28,16 +28,5 @@ LL | fn f(){(print!(á
2828
| |unclosed delimiter
2929
| unclosed delimiter
3030

31-
error: format argument must be a string literal
32-
--> $DIR/issue-104897.rs:6:16
33-
|
34-
LL | fn f(){(print!(á
35-
| ^
36-
|
37-
help: you might be missing a string literal to format with
38-
|
39-
LL | fn f(){(print!("{}", á
40-
| +++++
41-
42-
error: aborting due to 4 previous errors
31+
error: aborting due to 3 previous errors
4332

tests/ui/lint/unused_parens_multibyte_recovery.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// error-pattern: this file contains an unclosed delimiter
44
// error-pattern: this file contains an unclosed delimiter
55
// error-pattern: this file contains an unclosed delimiter
6-
// error-pattern: format argument must be a string literal
76
//
87
// Verify that unused parens lint does not try to create a span
98
// which points in the middle of a multibyte character.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this file contains an unclosed delimiter
2-
--> $DIR/unused_parens_multibyte_recovery.rs:11:17
2+
--> $DIR/unused_parens_multibyte_recovery.rs:10:17
33
|
44
LL | fn f(){(print!(á
55
| -- - ^
@@ -9,7 +9,7 @@ LL | fn f(){(print!(á
99
| unclosed delimiter
1010

1111
error: this file contains an unclosed delimiter
12-
--> $DIR/unused_parens_multibyte_recovery.rs:11:17
12+
--> $DIR/unused_parens_multibyte_recovery.rs:10:17
1313
|
1414
LL | fn f(){(print!(á
1515
| -- - ^
@@ -19,7 +19,7 @@ LL | fn f(){(print!(á
1919
| unclosed delimiter
2020

2121
error: this file contains an unclosed delimiter
22-
--> $DIR/unused_parens_multibyte_recovery.rs:11:17
22+
--> $DIR/unused_parens_multibyte_recovery.rs:10:17
2323
|
2424
LL | fn f(){(print!(á
2525
| -- - ^
@@ -28,16 +28,5 @@ LL | fn f(){(print!(á
2828
| |unclosed delimiter
2929
| unclosed delimiter
3030

31-
error: format argument must be a string literal
32-
--> $DIR/unused_parens_multibyte_recovery.rs:11:16
33-
|
34-
LL | fn f(){(print!(á
35-
| ^
36-
|
37-
help: you might be missing a string literal to format with
38-
|
39-
LL | fn f(){(print!("{}", á
40-
| +++++
41-
42-
error: aborting due to 4 previous errors
31+
error: aborting due to 3 previous errors
4332

tests/ui/macros/issue-102878.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
macro_rules!test{($l:expr,$_:r)=>({const:y y)}
22
//~^ ERROR mismatched closing delimiter: `)`
3-
//~| ERROR invalid fragment specifier `r`
4-
//~| ERROR expected identifier, found keyword `const`
5-
//~| ERROR expected identifier, found keyword `const`
6-
//~| ERROR expected identifier, found `:`
73

84
fn s(){test!(1,i)}
95

tests/ui/macros/issue-102878.stderr

+1-50
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,5 @@ LL | macro_rules!test{($l:expr,$_:r)=>({const:y y)}
77
| |unclosed delimiter
88
| closing delimiter possibly meant for this
99

10-
error: invalid fragment specifier `r`
11-
--> $DIR/issue-102878.rs:1:27
12-
|
13-
LL | macro_rules!test{($l:expr,$_:r)=>({const:y y)}
14-
| ^^^^
15-
|
16-
= help: valid fragment specifiers are `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, `item` and `vis`
17-
18-
error: expected identifier, found keyword `const`
19-
--> $DIR/issue-102878.rs:1:36
20-
|
21-
LL | macro_rules!test{($l:expr,$_:r)=>({const:y y)}
22-
| ^^^^^ expected identifier, found keyword
23-
...
24-
LL | fn s(){test!(1,i)}
25-
| ---------- in this macro invocation
26-
|
27-
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
28-
help: escape `const` to use it as an identifier
29-
|
30-
LL | macro_rules!test{($l:expr,$_:r)=>({r#const:y y)}
31-
| ++
32-
33-
error: expected identifier, found keyword `const`
34-
--> $DIR/issue-102878.rs:1:36
35-
|
36-
LL | macro_rules!test{($l:expr,$_:r)=>({const:y y)}
37-
| ^^^^^ expected identifier, found keyword
38-
...
39-
LL | fn s(){test!(1,i)}
40-
| ---------- in this macro invocation
41-
|
42-
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
43-
help: escape `const` to use it as an identifier
44-
|
45-
LL | macro_rules!test{($l:expr,$_:r)=>({r#const:y y)}
46-
| ++
47-
48-
error: expected identifier, found `:`
49-
--> $DIR/issue-102878.rs:1:41
50-
|
51-
LL | macro_rules!test{($l:expr,$_:r)=>({const:y y)}
52-
| ^ expected identifier
53-
...
54-
LL | fn s(){test!(1,i)}
55-
| ---------- in this macro invocation
56-
|
57-
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
58-
59-
error: aborting due to 5 previous errors
10+
error: aborting due to previous error
6011

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

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ impl dyn Demo {
1515
if let Some(b) = val
1616
&& let Some(c) = num {
1717
&& b == c {
18-
//~^ ERROR expected struct
19-
//~| ERROR mismatched types
2018
}
2119
}
2220
}
+2-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this file contains an unclosed delimiter
2-
--> $DIR/deli-ident-issue-1.rs:24:66
2+
--> $DIR/deli-ident-issue-1.rs:22:66
33
|
44
LL | impl dyn Demo {
55
| - unclosed delimiter
@@ -13,25 +13,5 @@ LL | }
1313
LL | fn main() { }
1414
| ^
1515

16-
error[E0574]: expected struct, variant or union type, found local variable `c`
17-
--> $DIR/deli-ident-issue-1.rs:17:17
18-
|
19-
LL | && b == c {
20-
| ^ not a struct, variant or union type
21-
22-
error[E0308]: mismatched types
23-
--> $DIR/deli-ident-issue-1.rs:17:9
24-
|
25-
LL | fn check(&self, val: Option<u32>, num: Option<u32>) {
26-
| - expected `()` because of default return type
27-
...
28-
LL | / && b == c {
29-
LL | |
30-
LL | |
31-
LL | | }
32-
| |_________^ expected `()`, found `bool`
33-
34-
error: aborting due to 3 previous errors
16+
error: aborting due to previous error
3517

36-
Some errors have detailed explanations: E0308, E0574.
37-
For more information about an error, try `rustc --explain E0308`.
+8-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
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+
19
error: unexpected closing delimiter: `}`
210
--> $DIR/deli-ident-issue-2.rs:5:1
311
|
@@ -7,13 +15,5 @@ LL | }
715
LL | }
816
| ^ unexpected closing delimiter
917

10-
error: mismatched closing delimiter: `]`
11-
--> $DIR/deli-ident-issue-2.rs:2:14
12-
|
13-
LL | if 1 < 2 {
14-
| ^ unclosed delimiter
15-
LL | let _a = vec!];
16-
| ^ mismatched closing delimiter
17-
1818
error: aborting due to 2 previous errors
1919

tests/ui/parser/do-not-suggest-semicolon-before-array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ fn foo() {}
22

33
fn bar() -> [u8; 2] {
44
foo()
5-
[1, 3) //~ ERROR expected one of `.`, `?`, `]`, or an operator, found `,`
5+
[1, 3) //~ ERROR mismatched closing delimiter
66
}
77

88
fn main() {}

tests/ui/parser/do-not-suggest-semicolon-before-array.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `.`, `?`, `]`, or an operator, found `,`
1+
error: mismatched closing delimiter: `)`
22
--> $DIR/do-not-suggest-semicolon-before-array.rs:5:5
33
|
44
LL | [1, 3)
5-
| ^ ^ help: `]` may belong here
5+
| ^ ^ mismatched closing delimiter
66
| |
77
| unclosed delimiter
88

tests/ui/parser/issue-103451.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// error-pattern: this file contains an unclosed delimiter
2-
// error-pattern: expected value, found struct `R`
32
struct R { }
43
struct S {
54
x: [u8; R

tests/ui/parser/issue-103451.stderr

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this file contains an unclosed delimiter
2-
--> $DIR/issue-103451.rs:5:15
2+
--> $DIR/issue-103451.rs:4:15
33
|
44
LL | struct S {
55
| - unclosed delimiter
@@ -9,7 +9,7 @@ LL | x: [u8; R
99
| unclosed delimiter
1010

1111
error: this file contains an unclosed delimiter
12-
--> $DIR/issue-103451.rs:5:15
12+
--> $DIR/issue-103451.rs:4:15
1313
|
1414
LL | struct S {
1515
| - unclosed delimiter
@@ -18,15 +18,5 @@ LL | x: [u8; R
1818
| |
1919
| unclosed delimiter
2020

21-
error[E0423]: expected value, found struct `R`
22-
--> $DIR/issue-103451.rs:5:13
23-
|
24-
LL | struct R { }
25-
| ------------ `R` defined here
26-
LL | struct S {
27-
LL | x: [u8; R
28-
| ^ help: use struct literal syntax instead: `R {}`
29-
30-
error: aborting due to 3 previous errors
21+
error: aborting due to 2 previous errors
3122

32-
For more information about this error, try `rustc --explain E0423`.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
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+
19
error: unexpected closing delimiter: `}`
210
--> $DIR/issue-68987-unmatch-issue-2.rs:14:1
311
|
@@ -7,13 +15,5 @@ LL | let obs_connect = || -> Result<(), MyError) {
715
LL | }
816
| ^ unexpected closing delimiter
917

10-
error: mismatched closing delimiter: `)`
11-
--> $DIR/issue-68987-unmatch-issue-2.rs:3:32
12-
|
13-
LL | async fn obstest() -> Result<> {
14-
| ^ unclosed delimiter
15-
LL | let obs_connect = || -> Result<(), MyError) {
16-
| ^ mismatched closing delimiter
17-
1818
error: aborting due to 2 previous errors
1919

0 commit comments

Comments
 (0)