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

Fix getting diagnostics example #2067

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions examples/rustc-driver-getting-diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ fn main() {
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
lint_caps: rustc_hash::FxHashMap::default(),
psess_created: Some(Box::new(|parse_sess| {
parse_sess.dcx = DiagCtxt::new(Box::new(DebugEmitter {
parse_sess.set_dcx(DiagCtxt::new(Box::new(DebugEmitter {
source_map: parse_sess.clone_source_map(),
diagnostics,
}))
})));
})),
register_lints: None,
override_queries: None,
Expand All @@ -98,6 +98,9 @@ fn main() {
let _ = tcx.analysis(());
});
});
// If the compiler has encountered errors when this closure returns, it will abort (!) the program.
// We avoid this by resetting the error count before returning
compiler.sess.dcx().reset_err_count();
});
// Read buffered diagnostics.
buffer.lock().unwrap().iter().for_each(|diagnostic| {
Expand Down
2 changes: 1 addition & 1 deletion src/rustc-driver-getting-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ otherwise be printed to stderr.
To get diagnostics from the compiler,
configure [`rustc_interface::Config`] to output diagnostic to a buffer,
and run [`TyCtxt.analysis`]. The following was tested
with <!-- date-check: may 2024 --> `nightly-2024-05-09`:
with <!-- date-check: september 2024 --> `nightly-2024-09-16`:

```rust
{{#include ../examples/rustc-driver-getting-diagnostics.rs}}
Expand Down