-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Improve unexpected close and mismatch delimiter hint in TokenTreesReader #104012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bors
merged 2 commits into
rust-lang:master
from
chenyukang:yukang/fix-103882-deli-indentation
Jan 28, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use super::UnmatchedBrace; | ||
use rustc_ast::token::Delimiter; | ||
use rustc_errors::Diagnostic; | ||
use rustc_span::source_map::SourceMap; | ||
use rustc_span::Span; | ||
|
||
#[derive(Default)] | ||
pub struct TokenTreeDiagInfo { | ||
/// Stack of open delimiters and their spans. Used for error message. | ||
pub open_braces: Vec<(Delimiter, Span)>, | ||
pub unmatched_braces: Vec<UnmatchedBrace>, | ||
|
||
/// Used only for error recovery when arriving to EOF with mismatched braces. | ||
pub last_unclosed_found_span: Option<Span>, | ||
|
||
/// Collect empty block spans that might have been auto-inserted by editors. | ||
pub empty_block_spans: Vec<Span>, | ||
|
||
/// Collect the spans of braces (Open, Close). Used only | ||
/// for detecting if blocks are empty and only braces. | ||
pub matching_block_spans: Vec<(Span, Span)>, | ||
} | ||
|
||
pub fn same_identation_level(sm: &SourceMap, open_sp: Span, close_sp: Span) -> bool { | ||
match (sm.span_to_margin(open_sp), sm.span_to_margin(close_sp)) { | ||
(Some(open_padding), Some(close_padding)) => open_padding == close_padding, | ||
_ => false, | ||
} | ||
chenyukang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// When we get a `)` or `]` for `{`, we should emit help message here | ||
// it's more friendly compared to report `unmatched error` in later phase | ||
pub fn report_missing_open_delim( | ||
err: &mut Diagnostic, | ||
unmatched_braces: &[UnmatchedBrace], | ||
) -> bool { | ||
let mut reported_missing_open = false; | ||
for unmatch_brace in unmatched_braces.iter() { | ||
if let Some(delim) = unmatch_brace.found_delim | ||
&& matches!(delim, Delimiter::Parenthesis | Delimiter::Bracket) | ||
{ | ||
let missed_open = match delim { | ||
Delimiter::Parenthesis => "(", | ||
Delimiter::Bracket => "[", | ||
_ => unreachable!(), | ||
}; | ||
err.span_label( | ||
unmatch_brace.found_span.shrink_to_lo(), | ||
format!("missing open `{}` for this delimiter", missed_open), | ||
); | ||
reported_missing_open = true; | ||
} | ||
} | ||
reported_missing_open | ||
} | ||
|
||
pub fn report_suspicious_mismatch_block( | ||
err: &mut Diagnostic, | ||
diag_info: &TokenTreeDiagInfo, | ||
sm: &SourceMap, | ||
delim: Delimiter, | ||
) { | ||
if report_missing_open_delim(err, &diag_info.unmatched_braces) { | ||
return; | ||
} | ||
|
||
let mut matched_spans: Vec<(Span, bool)> = diag_info | ||
.matching_block_spans | ||
.iter() | ||
.map(|&(open, close)| (open.with_hi(close.lo()), same_identation_level(sm, open, close))) | ||
.collect(); | ||
|
||
// sort by `lo`, so the large block spans in the front | ||
matched_spans.sort_by(|a, b| a.0.lo().cmp(&b.0.lo())); | ||
|
||
// We use larger block whose identation is well to cover those inner mismatched blocks | ||
// O(N^2) here, but we are on error reporting path, so it is fine | ||
for i in 0..matched_spans.len() { | ||
let (block_span, same_ident) = matched_spans[i]; | ||
if same_ident { | ||
for j in i + 1..matched_spans.len() { | ||
let (inner_block, inner_same_ident) = matched_spans[j]; | ||
if block_span.contains(inner_block) && !inner_same_ident { | ||
matched_spans[j] = (inner_block, true); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Find the inner-most span candidate for final report | ||
let candidate_span = | ||
matched_spans.into_iter().rev().find(|&(_, same_ident)| !same_ident).map(|(span, _)| span); | ||
|
||
if let Some(block_span) = candidate_span { | ||
err.span_label(block_span.shrink_to_lo(), "this delimiter might not be properly closed..."); | ||
err.span_label( | ||
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 | ||
if delim == Delimiter::Brace { | ||
for span in diag_info.empty_block_spans.iter() { | ||
if block_span.contains(*span) { | ||
err.span_label(*span, "block is empty, you might have not meant to close it"); | ||
break; | ||
} | ||
} | ||
} | ||
} else { | ||
// If there is no suspicious span, give the last properly closed block may help | ||
if let Some(parent) = diag_info.matching_block_spans.last() | ||
&& diag_info.open_braces.last().is_none() | ||
&& diag_info.empty_block_spans.iter().all(|&sp| sp != parent.0.to(parent.1)) { | ||
err.span_label(parent.0, "this opening brace..."); | ||
err.span_label(parent.1, "...matches this closing brace"); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#![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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.