Skip to content
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

Explicitly call emit_stashed_diagnostics. #121487

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/tools/rustfmt/src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,21 @@ impl<'a> Parser<'a> {
fn parse_crate_mod(&mut self) -> Result<ast::Crate, ParserError> {
let mut parser = AssertUnwindSafe(&mut self.parser);

match catch_unwind(move || parser.parse_crate_mod()) {
Ok(Ok(k)) => Ok(k),
Ok(Err(db)) => {
// rustfmt doesn't use `run_compiler` like other tools, so it must emit
// any stashed diagnostics itself, otherwise the `DiagCtxt` will assert
// when dropped. The final result here combines the parsing result and
// the `emit_stashed_diagnostics` result.
let parse_res = catch_unwind(move || parser.parse_crate_mod());
let stashed_res = self.parser.dcx().emit_stashed_diagnostics();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering will this emit anything when using the silent emitter? I'm guessing it won't, but just want to double check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't. DiagCtxt::emit_stashed_diagnostics calls DiagCtxtInner:emit_diagnostic on each diagnostic, which calls emit_diagnostic on the emitter held by the DiagCtxt. So if that's the silent emitter, then nothing will be printed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation 😁

let err = Err(ParserError::ParsePanicError);
match (parse_res, stashed_res) {
(Ok(Ok(k)), None) => Ok(k),
(Ok(Ok(_)), Some(_guar)) => err,
(Ok(Err(db)), _) => {
db.emit();
Err(ParserError::ParseError)
err
}
Err(_) => Err(ParserError::ParsePanicError),
(Err(_), _) => err,
}
}
}
7 changes: 7 additions & 0 deletions src/tools/rustfmt/src/test/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ fn crate_parsing_errors_on_unclosed_delims() {
let filename = "tests/parser/unclosed-delims/issue_4466.rs";
assert_parser_error(filename);
}

#[test]
fn crate_parsing_stashed_diag() {
// See also https://github.com/rust-lang/rust/issues/121450
let filename = "tests/parser/stashed-diag.rs";
assert_parser_error(filename);
}
3 changes: 3 additions & 0 deletions src/tools/rustfmt/tests/parser/stashed-diag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![u={static N;}]

fn main() {}
Loading