Skip to content

Commit

Permalink
keep empty block suggestion, and add test case for #69259
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Nov 11, 2022
1 parent 54b190c commit 0868f18
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
23 changes: 17 additions & 6 deletions compiler/rustc_parse/src/lexer/tokentrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ pub(super) struct TokenTreesReader<'a> {
/// Used only for error recovery when arriving to EOF with mismatched braces.
matching_delim_spans: Vec<(Delimiter, Span, Span)>,
last_unclosed_found_span: Option<Span>,

/// Collect empty block spans that might have been auto-inserted by editors.
last_delim_empty_block_spans: FxHashMap<Delimiter, Span>,
empty_block_spans: FxHashMap<Span, Delimiter>,

/// Collect the spans of braces (Open, Close). Used only
/// for detecting if blocks are empty and only braces.
matching_block_spans: Vec<(Span, Span)>,
Expand All @@ -37,7 +39,7 @@ impl<'a> TokenTreesReader<'a> {
unmatched_braces: Vec::new(),
matching_delim_spans: Vec::new(),
last_unclosed_found_span: None,
last_delim_empty_block_spans: FxHashMap::default(),
empty_block_spans: FxHashMap::default(),
matching_block_spans: Vec::new(),
};
let res = tt_reader.parse_token_trees(/* is_delimited */ false);
Expand Down Expand Up @@ -135,11 +137,11 @@ impl<'a> TokenTreesReader<'a> {
if !sm.is_multiline(empty_block_span) {
// Only track if the block is in the form of `{}`, otherwise it is
// likely that it was written on purpose.
self.last_delim_empty_block_spans.insert(open_delim, empty_block_span);
self.empty_block_spans.insert(empty_block_span, open_delim);
}
}

//only add braces
// only add braces
if let (Delimiter::Brace, Delimiter::Brace) = (open_brace, open_delim) {
self.matching_block_spans.push((open_brace_span, close_brace_span));

Expand Down Expand Up @@ -230,9 +232,8 @@ impl<'a> TokenTreesReader<'a> {
}
}

fn report_error_prone_delim_block(&self, delim: Delimiter, err: &mut Diagnostic) {
fn report_error_prone_delim_block(&mut self, delim: Delimiter, err: &mut Diagnostic) {
let mut matched_spans = vec![];
let mut candidate_span = None;

for &(d, open_sp, close_sp) in &self.matching_delim_spans {
if d == delim {
Expand All @@ -259,6 +260,7 @@ impl<'a> TokenTreesReader<'a> {
}
}

let mut candidate_span = None;
// Find the innermost span candidate for final report
for (block_span, same_ident) in matched_spans.into_iter().rev() {
if !same_ident {
Expand All @@ -276,6 +278,15 @@ impl<'a> TokenTreesReader<'a> {
block_span.shrink_to_hi(),
"...as it matches this but it has different indentation",
);

// If there is a empty block in the mismatched span, note it
for span in self.empty_block_spans.keys() {
if let Some(d) = self.empty_block_spans.get(span) &&
*d == delim && block_span.contains(*span) {
err.span_label(*span, "block is empty, you might have not meant to close it");
break;
}
}
}
}
}
4 changes: 3 additions & 1 deletion src/test/ui/parser/issue-68987-unmatch-issue-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ error: unexpected closing delimiter: `}`
|
LL | match o {
| - this delimiter might not be properly closed...
...
LL | Some(_x) => {} // Extra '}'
| -- block is empty, you might have not meant to close it
LL | let _ = if true {};
LL | }
| - ...as it matches this but it has different indentation
...
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/parser/issue-68987-unmatch-issue.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ error: unexpected closing delimiter: `}`
|
LL | match o {
| - this delimiter might not be properly closed...
...
LL | Some(_x) => // Missing '{'
LL | let _ = if true {};
| -- block is empty, you might have not meant to close it
LL | }
| - ...as it matches this but it has different indentation
...
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/parser/issues/issue-69259.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {}

fn f) {} //~ ERROR unexpected closing delimiter
8 changes: 8 additions & 0 deletions src/test/ui/parser/issues/issue-69259.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unexpected closing delimiter: `)`
--> $DIR/issue-69259.rs:3:5
|
LL | fn f) {}
| ^ unexpected closing delimiter

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ error: unexpected closing delimiter: `}`
LL | match self {
| - this delimiter might not be properly closed...
LL | ErrorHandled::Reported => {}}
| - ...as it matches this but it has different indentation
| --- ...as it matches this but it has different indentation
| |
| block is empty, you might have not meant to close it
...
LL | }
| ^ unexpected closing delimiter
Expand Down

0 comments on commit 0868f18

Please sign in to comment.