Skip to content

Commit b4f8351

Browse files
fix: resolve some parser related bugs
1 parent 9b78491 commit b4f8351

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

Diff for: src/formatting/syntux/parser.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ impl<'a> ParserBuilder<'a> {
6666
let parser = match Self::parser(sess.inner(), input) {
6767
Ok(p) => p,
6868
Err(db) => {
69-
sess.emit_diagnostics(db);
70-
return Err(ParserError::ParserCreationError);
69+
if let Some(diagnostics) = db {
70+
sess.emit_diagnostics(diagnostics);
71+
return Err(ParserError::ParserCreationError);
72+
}
73+
return Err(ParserError::ParsePanicError);
7174
}
7275
};
7376

@@ -77,14 +80,18 @@ impl<'a> ParserBuilder<'a> {
7780
fn parser(
7881
sess: &'a rustc_session::parse::ParseSess,
7982
input: Input,
80-
) -> Result<rustc_parse::parser::Parser<'a>, Vec<Diagnostic>> {
83+
) -> Result<rustc_parse::parser::Parser<'a>, Option<Vec<Diagnostic>>> {
8184
match input {
82-
Input::File(ref file) => Ok(new_parser_from_file(sess, file, None)),
85+
Input::File(ref file) => catch_unwind(AssertUnwindSafe(move || {
86+
new_parser_from_file(sess, file, None)
87+
}))
88+
.map_err(|_| None),
8389
Input::Text(text) => rustc_parse::maybe_new_parser_from_source_str(
8490
sess,
8591
rustc_span::FileName::Custom("stdin".to_owned()),
8692
text,
87-
),
93+
)
94+
.map_err(|db| Some(db)),
8895
}
8996
}
9097
}
@@ -121,7 +128,7 @@ impl<'a> Parser<'a> {
121128
match parser.parse_mod(&TokenKind::Eof, ast::Unsafe::No) {
122129
Ok(result) => Some(result),
123130
Err(mut e) => {
124-
e.cancel();
131+
sess.emit_or_cancel_diagnostic(&mut e);
125132
if sess.can_reset_errors() {
126133
sess.reset_errors();
127134
}

Diff for: src/formatting/syntux/session.rs

+11
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,17 @@ impl ParseSess {
219219
}
220220
}
221221

222+
pub(crate) fn emit_or_cancel_diagnostic(&self, diagnostic: &mut Diagnostic) {
223+
self.parse_sess.span_diagnostic.emit_diagnostic(diagnostic);
224+
// The Handler will check whether the diagnostic should be emitted
225+
// based on the user's rustfmt configuration and the originating file
226+
// that caused the parser error. If the Handler determined it should skip
227+
// emission then we need to ensure the diagnostic is cancelled.
228+
if !diagnostic.cancelled() {
229+
diagnostic.cancel();
230+
}
231+
}
232+
222233
pub(super) fn can_reset_errors(&self) -> bool {
223234
*self.can_reset_errors.borrow()
224235
}

Diff for: src/result.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub enum OperationError {
118118
#[error("invalid glob pattern found in ignore list: {0}")]
119119
InvalidGlobPattern(ignore::Error),
120120
/// Parse error occurred while parsing the input.
121-
#[error("failed to parse {input:?}")]
121+
#[error("failed to parse {input}")]
122122
ParseError { input: FileName, is_panic: bool },
123123
/// Io error.
124124
#[error("{0}")]

0 commit comments

Comments
 (0)