Skip to content

Commit 2b6226c

Browse files
fix: resolve some parser related bugs
1 parent 86a41bc commit 2b6226c

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/syntux/parser.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ impl<'a> ParserBuilder<'a> {
6565
let parser = match Self::parser(sess.inner(), input) {
6666
Ok(p) => p,
6767
Err(db) => {
68-
sess.emit_diagnostics(db);
69-
return Err(ParserError::ParserCreationError);
68+
if let Some(diagnostics) = db {
69+
sess.emit_diagnostics(diagnostics);
70+
return Err(ParserError::ParserCreationError);
71+
}
72+
return Err(ParserError::ParsePanicError);
7073
}
7174
};
7275

@@ -76,14 +79,18 @@ impl<'a> ParserBuilder<'a> {
7679
fn parser(
7780
sess: &'a rustc_session::parse::ParseSess,
7881
input: Input,
79-
) -> Result<rustc_parse::parser::Parser<'a>, Vec<Diagnostic>> {
82+
) -> Result<rustc_parse::parser::Parser<'a>, Option<Vec<Diagnostic>>> {
8083
match input {
81-
Input::File(ref file) => Ok(new_parser_from_file(sess, file, None)),
84+
Input::File(ref file) => catch_unwind(AssertUnwindSafe(move || {
85+
new_parser_from_file(sess, file, None)
86+
}))
87+
.map_err(|_| None),
8288
Input::Text(text) => rustc_parse::maybe_new_parser_from_source_str(
8389
sess,
8490
rustc_span::FileName::Custom("stdin".to_owned()),
8591
text,
86-
),
92+
)
93+
.map_err(|db| Some(db)),
8794
}
8895
}
8996
}
@@ -120,8 +127,10 @@ impl<'a> Parser<'a> {
120127
match parser.parse_mod(&TokenKind::Eof, ast::Unsafe::No) {
121128
Ok(result) => Some(result),
122129
Err(mut e) => {
123-
e.cancel();
124-
sess.reset_errors();
130+
sess.emit_or_cancel_diagnostic(&mut e);
131+
if sess.can_reset_errors() {
132+
sess.reset_errors();
133+
}
125134
None
126135
}
127136
}

src/syntux/session.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,17 @@ impl ParseSess {
230230
}
231231
}
232232

233+
pub(crate) fn emit_or_cancel_diagnostic(&self, diagnostic: &mut Diagnostic) {
234+
self.parse_sess.span_diagnostic.emit_diagnostic(diagnostic);
235+
// The Handler will check whether the diagnostic should be emitted
236+
// based on the user's rustfmt configuration and the originating file
237+
// that caused the parser error. If the Handler determined it should skip
238+
// emission then we need to ensure the diagnostic is cancelled.
239+
if !diagnostic.cancelled() {
240+
diagnostic.cancel();
241+
}
242+
}
243+
233244
pub(super) fn can_reset_errors(&self) -> bool {
234245
*self.can_reset_errors.borrow()
235246
}

0 commit comments

Comments
 (0)