Skip to content

Commit 83c0934

Browse files
committed
Only use indentation to detect mismatch delimiter when the line starting with delimiter
1 parent 6718ea1 commit 83c0934

File tree

8 files changed

+119
-16
lines changed

8 files changed

+119
-16
lines changed

compiler/rustc_parse/src/lexer/tokentrees.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ impl<'a> TokenTreesReader<'a> {
107107
if let Some((_, open_sp, close_sp)) =
108108
self.matching_delim_spans.iter().find(|(d, open_sp, close_sp)| {
109109
let sm = self.string_reader.sess.source_map();
110-
if let Some(close_padding) = sm.span_to_margin(*close_sp) {
111-
if let Some(open_padding) = sm.span_to_margin(*open_sp) {
110+
if let Some(open_padding) = sm.span_to_margin(*open_sp) &&
111+
let Some(close_padding) = sm.span_to_margin(*close_sp) &&
112+
sm.is_line_before_span_empty(*open_sp) &&
113+
sm.is_line_before_span_empty(*close_sp) {
112114
return delim == d && close_padding != open_padding;
113115
}
114-
}
115116
false
116117
})
117118
// these are in reverse order as they get inserted on close, but
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// ignore-tidy-trailing-newlines
2+
//
3+
// error-pattern: this file contains an unclosed delimiter
4+
#![feature(let_chains)]
5+
trait Demo {}
6+
7+
impl dyn Demo {
8+
pub fn report(&self) -> u32 {
9+
let sum = |a: u32,
10+
b: u32,
11+
c: u32| {
12+
a + b + c
13+
};
14+
sum(1, 2, 3)
15+
}
16+
17+
fn check(&self, val: Option<u32>, num: Option<u32>) {
18+
if let Some(b) = val
19+
&& let Some(c) = num {
20+
&& b == c {
21+
//~^ ERROR expected struct
22+
//~| ERROR mismatched types
23+
}
24+
}
25+
}
26+
27+
fn main() { } //~ ERROR this file contains an unclosed delimiter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: this file contains an unclosed delimiter
2+
--> $DIR/deli-ident-issue-1.rs:27:65
3+
|
4+
LL | impl dyn Demo {
5+
| - unclosed delimiter
6+
...
7+
LL | fn main() { }
8+
| ^
9+
10+
error[E0574]: expected struct, variant or union type, found local variable `c`
11+
--> $DIR/deli-ident-issue-1.rs:20:17
12+
|
13+
LL | && b == c {
14+
| ^ not a struct, variant or union type
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/deli-ident-issue-1.rs:20:9
18+
|
19+
LL | fn check(&self, val: Option<u32>, num: Option<u32>) {
20+
| - expected `()` because of default return type
21+
...
22+
LL | / && b == c {
23+
LL | |
24+
LL | |
25+
LL | | }
26+
| |_________^ expected `()`, found `bool`
27+
28+
error: aborting due to 3 previous errors
29+
30+
Some errors have detailed explanations: E0308, E0574.
31+
For more information about an error, try `rustc --explain E0308`.
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ignore-tidy-trailing-newlines
2+
//
3+
// error-pattern: this file contains an unclosed delimiter
4+
#![feature(let_chains)]
5+
trait Demo {}
6+
7+
impl dyn Demo {
8+
pub fn report(&self,
9+
a: u32,
10+
b: u32,
11+
c: u32) -> u32 {
12+
return a + b + c;
13+
}
14+
15+
fn check(&self, val: Option<u32>, num: Option<u32>) {
16+
if let Some(b) = val
17+
&& let Some(c) = num {
18+
&& b == c {
19+
//~^ ERROR expected struct
20+
//~| ERROR mismatched types
21+
}
22+
}
23+
}
24+
25+
fn main() { } //~ ERROR this file contains an unclosed delimiter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: this file contains an unclosed delimiter
2+
--> $DIR/deli-ident-issue-2.rs:25:65
3+
|
4+
LL | impl dyn Demo {
5+
| - unclosed delimiter
6+
...
7+
LL | fn main() { }
8+
| ^
9+
10+
error[E0574]: expected struct, variant or union type, found local variable `c`
11+
--> $DIR/deli-ident-issue-2.rs:18:17
12+
|
13+
LL | && b == c {
14+
| ^ not a struct, variant or union type
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/deli-ident-issue-2.rs:18:9
18+
|
19+
LL | fn check(&self, val: Option<u32>, num: Option<u32>) {
20+
| - expected `()` because of default return type
21+
...
22+
LL | / && b == c {
23+
LL | |
24+
LL | |
25+
LL | | }
26+
| |_________^ expected `()`, found `bool`
27+
28+
error: aborting due to 3 previous errors
29+
30+
Some errors have detailed explanations: E0308, E0574.
31+
For more information about an error, try `rustc --explain E0308`.

src/test/ui/parser/issues/issue-2354.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
fn foo() { //~ NOTE unclosed delimiter
22
match Some(10) {
3-
//~^ NOTE this delimiter might not be properly closed...
43
Some(y) => { panic!(); }
54
None => { panic!(); }
65
}
7-
//~^ NOTE ...as it matches this but it has different indentation
86

97
fn bar() {
108
let mut i = 0;

src/test/ui/parser/issues/issue-2354.stderr

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
error: this file contains an unclosed delimiter
2-
--> $DIR/issue-2354.rs:15:52
2+
--> $DIR/issue-2354.rs:13:52
33
|
44
LL | fn foo() {
55
| - unclosed delimiter
6-
LL | match Some(10) {
7-
| - this delimiter might not be properly closed...
8-
...
9-
LL | }
10-
| - ...as it matches this but it has different indentation
116
...
127
LL |
138
| ^

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

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ error: this file contains an unclosed delimiter
33
|
44
LL | trait Foo {
55
| - unclosed delimiter
6-
LL | fn bar() {
7-
| - this delimiter might not be properly closed...
8-
...
9-
LL | }
10-
| - ...as it matches this but it has different indentation
116
...
127
LL | }
138
| ^

0 commit comments

Comments
 (0)