Skip to content

Commit

Permalink
Only use indentation to detect mismatch delimiter when the line start…
Browse files Browse the repository at this point in the history
…ing with delimiter
  • Loading branch information
chenyukang committed Nov 5, 2022
1 parent 6718ea1 commit 83c0934
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 16 deletions.
7 changes: 4 additions & 3 deletions compiler/rustc_parse/src/lexer/tokentrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ impl<'a> TokenTreesReader<'a> {
if let Some((_, open_sp, close_sp)) =
self.matching_delim_spans.iter().find(|(d, open_sp, close_sp)| {
let sm = self.string_reader.sess.source_map();
if let Some(close_padding) = sm.span_to_margin(*close_sp) {
if let Some(open_padding) = sm.span_to_margin(*open_sp) {
if let Some(open_padding) = sm.span_to_margin(*open_sp) &&
let Some(close_padding) = sm.span_to_margin(*close_sp) &&
sm.is_line_before_span_empty(*open_sp) &&
sm.is_line_before_span_empty(*close_sp) {
return delim == d && close_padding != open_padding;
}
}
false
})
// these are in reverse order as they get inserted on close, but
Expand Down
27 changes: 27 additions & 0 deletions src/test/ui/parser/deli-ident-issue-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ignore-tidy-trailing-newlines
//
// error-pattern: this file contains an unclosed delimiter
#![feature(let_chains)]
trait Demo {}

impl dyn Demo {
pub fn report(&self) -> u32 {
let sum = |a: u32,
b: u32,
c: u32| {
a + b + c
};
sum(1, 2, 3)
}

fn check(&self, val: Option<u32>, num: Option<u32>) {
if let Some(b) = val
&& let Some(c) = num {
&& b == c {
//~^ ERROR expected struct
//~| ERROR mismatched types
}
}
}

fn main() { } //~ ERROR this file contains an unclosed delimiter
31 changes: 31 additions & 0 deletions src/test/ui/parser/deli-ident-issue-1.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error: this file contains an unclosed delimiter
--> $DIR/deli-ident-issue-1.rs:27:65
|
LL | impl dyn Demo {
| - unclosed delimiter
...
LL | fn main() { }
| ^

error[E0574]: expected struct, variant or union type, found local variable `c`
--> $DIR/deli-ident-issue-1.rs:20:17
|
LL | && b == c {
| ^ not a struct, variant or union type

error[E0308]: mismatched types
--> $DIR/deli-ident-issue-1.rs:20:9
|
LL | fn check(&self, val: Option<u32>, num: Option<u32>) {
| - expected `()` because of default return type
...
LL | / && b == c {
LL | |
LL | |
LL | | }
| |_________^ expected `()`, found `bool`

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0308, E0574.
For more information about an error, try `rustc --explain E0308`.
25 changes: 25 additions & 0 deletions src/test/ui/parser/deli-ident-issue-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ignore-tidy-trailing-newlines
//
// error-pattern: this file contains an unclosed delimiter
#![feature(let_chains)]
trait Demo {}

impl dyn Demo {
pub fn report(&self,
a: u32,
b: u32,
c: u32) -> u32 {
return a + b + c;
}

fn check(&self, val: Option<u32>, num: Option<u32>) {
if let Some(b) = val
&& let Some(c) = num {
&& b == c {
//~^ ERROR expected struct
//~| ERROR mismatched types
}
}
}

fn main() { } //~ ERROR this file contains an unclosed delimiter
31 changes: 31 additions & 0 deletions src/test/ui/parser/deli-ident-issue-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error: this file contains an unclosed delimiter
--> $DIR/deli-ident-issue-2.rs:25:65
|
LL | impl dyn Demo {
| - unclosed delimiter
...
LL | fn main() { }
| ^

error[E0574]: expected struct, variant or union type, found local variable `c`
--> $DIR/deli-ident-issue-2.rs:18:17
|
LL | && b == c {
| ^ not a struct, variant or union type

error[E0308]: mismatched types
--> $DIR/deli-ident-issue-2.rs:18:9
|
LL | fn check(&self, val: Option<u32>, num: Option<u32>) {
| - expected `()` because of default return type
...
LL | / && b == c {
LL | |
LL | |
LL | | }
| |_________^ expected `()`, found `bool`

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0308, E0574.
For more information about an error, try `rustc --explain E0308`.
2 changes: 0 additions & 2 deletions src/test/ui/parser/issues/issue-2354.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
fn foo() { //~ NOTE unclosed delimiter
match Some(10) {
//~^ NOTE this delimiter might not be properly closed...
Some(y) => { panic!(); }
None => { panic!(); }
}
//~^ NOTE ...as it matches this but it has different indentation

fn bar() {
let mut i = 0;
Expand Down
7 changes: 1 addition & 6 deletions src/test/ui/parser/issues/issue-2354.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
error: this file contains an unclosed delimiter
--> $DIR/issue-2354.rs:15:52
--> $DIR/issue-2354.rs:13:52
|
LL | fn foo() {
| - unclosed delimiter
LL | match Some(10) {
| - this delimiter might not be properly closed...
...
LL | }
| - ...as it matches this but it has different indentation
...
LL |
| ^
Expand Down
5 changes: 0 additions & 5 deletions src/test/ui/parser/parser-recovery-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ error: this file contains an unclosed delimiter
|
LL | trait Foo {
| - unclosed delimiter
LL | fn bar() {
| - this delimiter might not be properly closed...
...
LL | }
| - ...as it matches this but it has different indentation
...
LL | }
| ^
Expand Down

0 comments on commit 83c0934

Please sign in to comment.