Deduplicate unmatched_delims in rustc_parse to reduce confusion
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
This commit is contained in:
@@ -34,9 +34,12 @@ pub(super) fn same_indentation_level(sm: &SourceMap, open_sp: Span, close_sp: Sp
|
||||
|
||||
// When we get a `)` or `]` for `{`, we should emit help message here
|
||||
// it's more friendly compared to report `unmatched error` in later phase
|
||||
fn report_missing_open_delim(err: &mut Diag<'_>, unmatched_delims: &[UnmatchedDelim]) -> bool {
|
||||
pub(super) fn report_missing_open_delim(
|
||||
err: &mut Diag<'_>,
|
||||
unmatched_delims: &mut Vec<UnmatchedDelim>,
|
||||
) -> bool {
|
||||
let mut reported_missing_open = false;
|
||||
for unmatch_brace in unmatched_delims.iter() {
|
||||
unmatched_delims.retain(|unmatch_brace| {
|
||||
if let Some(delim) = unmatch_brace.found_delim
|
||||
&& matches!(delim, Delimiter::Parenthesis | Delimiter::Bracket)
|
||||
{
|
||||
@@ -45,13 +48,20 @@ fn report_missing_open_delim(err: &mut Diag<'_>, unmatched_delims: &[UnmatchedDe
|
||||
Delimiter::Bracket => "[",
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
if let Some(unclosed_span) = unmatch_brace.unclosed_span {
|
||||
err.span_label(unclosed_span, "the nearest open delimiter");
|
||||
}
|
||||
err.span_label(
|
||||
unmatch_brace.found_span.shrink_to_lo(),
|
||||
format!("missing open `{missed_open}` for this delimiter"),
|
||||
);
|
||||
reported_missing_open = true;
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
});
|
||||
reported_missing_open
|
||||
}
|
||||
|
||||
@@ -61,10 +71,6 @@ pub(super) fn report_suspicious_mismatch_block(
|
||||
sm: &SourceMap,
|
||||
delim: Delimiter,
|
||||
) {
|
||||
if report_missing_open_delim(err, &diag_info.unmatched_delims) {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut matched_spans: Vec<(Span, bool)> = diag_info
|
||||
.matching_block_spans
|
||||
.iter()
|
||||
|
||||
@@ -3,7 +3,9 @@ use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, Toke
|
||||
use rustc_ast_pretty::pprust::token_to_string;
|
||||
use rustc_errors::Diag;
|
||||
|
||||
use super::diagnostics::{report_suspicious_mismatch_block, same_indentation_level};
|
||||
use super::diagnostics::{
|
||||
report_missing_open_delim, report_suspicious_mismatch_block, same_indentation_level,
|
||||
};
|
||||
use super::{Lexer, UnmatchedDelim};
|
||||
|
||||
impl<'psess, 'src> Lexer<'psess, 'src> {
|
||||
@@ -244,7 +246,16 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
|
||||
let msg = format!("unexpected closing delimiter: `{token_str}`");
|
||||
let mut err = self.dcx().struct_span_err(self.token.span, msg);
|
||||
|
||||
report_suspicious_mismatch_block(&mut err, &self.diag_info, self.psess.source_map(), delim);
|
||||
// if there is no missing open delim, report suspicious mismatch block
|
||||
if !report_missing_open_delim(&mut err, &mut self.diag_info.unmatched_delims) {
|
||||
report_suspicious_mismatch_block(
|
||||
&mut err,
|
||||
&self.diag_info,
|
||||
self.psess.source_map(),
|
||||
delim,
|
||||
);
|
||||
}
|
||||
|
||||
err.span_label(self.token.span, "unexpected closing delimiter");
|
||||
err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user