Skip to content

Commit d32539a

Browse files
committed
Rename make_unclosed_delims_error and return Vec<Diag>
Signed-off-by: xizheyin <[email protected]>
1 parent 460259d commit d32539a

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

compiler/rustc_parse/src/lexer/diagnostics.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,29 @@ pub(super) fn report_suspicious_mismatch_block(
126126
}
127127
}
128128

129-
pub(crate) fn make_unclosed_delims_error(
130-
unmatched: UnmatchedDelim,
131-
psess: &ParseSess,
132-
) -> Option<Diag<'_>> {
133-
// `None` here means an `Eof` was found. We already emit those errors elsewhere, we add them to
134-
// `unmatched_delims` only for error recovery in the `Parser`.
135-
let found_delim = unmatched.found_delim?;
136-
let mut spans = vec![unmatched.found_span];
137-
if let Some(sp) = unmatched.unclosed_span {
138-
spans.push(sp);
139-
};
140-
let err = psess.dcx().create_err(MismatchedClosingDelimiter {
141-
spans,
142-
delimiter: pprust::token_kind_to_string(&found_delim.as_close_token_kind()).to_string(),
143-
unmatched: unmatched.found_span,
144-
opening_candidate: unmatched.candidate_span,
145-
unclosed: unmatched.unclosed_span,
146-
});
147-
Some(err)
129+
pub(crate) fn make_errors_for_mismatched_closing_delims<'psess>(
130+
unmatcheds: &[UnmatchedDelim],
131+
psess: &'psess ParseSess,
132+
) -> Vec<Diag<'psess>> {
133+
unmatcheds
134+
.iter()
135+
.filter_map(|unmatched| {
136+
// `None` here means an `Eof` was found. We already emit those errors elsewhere, we add them to
137+
// `unmatched_delims` only for error recovery in the `Parser`.
138+
let found_delim = unmatched.found_delim?;
139+
let mut spans = vec![unmatched.found_span];
140+
if let Some(sp) = unmatched.unclosed_span {
141+
spans.push(sp);
142+
};
143+
let err = psess.dcx().create_err(MismatchedClosingDelimiter {
144+
spans,
145+
delimiter: pprust::token_kind_to_string(&found_delim.as_close_token_kind())
146+
.to_string(),
147+
unmatched: unmatched.found_span,
148+
opening_candidate: unmatched.candidate_span,
149+
unclosed: unmatched.unclosed_span,
150+
});
151+
Some(err)
152+
})
153+
.collect()
148154
}

compiler/rustc_parse/src/lexer/mod.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use diagnostics::make_unclosed_delims_error;
1+
use diagnostics::make_errors_for_mismatched_closing_delims;
22
use rustc_ast::ast::{self, AttrStyle};
33
use rustc_ast::token::{self, CommentKind, Delimiter, IdentIsRaw, Token, TokenKind};
44
use rustc_ast::tokenstream::TokenStream;
@@ -71,27 +71,23 @@ pub(crate) fn lex_token_trees<'psess, 'src>(
7171
};
7272
let res = lexer.lex_token_trees(/* is_delimited */ false);
7373

74-
let mut unmatched_delims: Vec<_> = lexer
75-
.diag_info
76-
.unmatched_delims
77-
.into_iter()
78-
.filter_map(|unmatched_delim| make_unclosed_delims_error(unmatched_delim, psess))
79-
.collect();
74+
let mut unmatched_closing_delims: Vec<_> =
75+
make_errors_for_mismatched_closing_delims(&lexer.diag_info.unmatched_delims, psess);
8076

8177
match res {
8278
Ok((_open_spacing, stream)) => {
83-
if unmatched_delims.is_empty() {
79+
if unmatched_closing_delims.is_empty() {
8480
Ok(stream)
8581
} else {
8682
// Return error if there are unmatched delimiters or unclosed delimiters.
87-
Err(unmatched_delims)
83+
Err(unmatched_closing_delims)
8884
}
8985
}
9086
Err(errs) => {
9187
// We emit delimiter mismatch errors first, then emit the unclosing delimiter mismatch
9288
// because the delimiter mismatch is more likely to be the root cause of error
93-
unmatched_delims.extend(errs);
94-
Err(unmatched_delims)
89+
unmatched_closing_delims.extend(errs);
90+
Err(unmatched_closing_delims)
9591
}
9692
}
9793
}

0 commit comments

Comments
 (0)