Fix getting diagnostics example

This commit is contained in:
Vetle Rasmussen 2024-09-18 07:06:24 +02:00 committed by nora
parent c852d1c693
commit 06f9ccf1f2
2 changed files with 6 additions and 3 deletions

View File

@ -77,10 +77,10 @@ fn main() {
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES, locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
lint_caps: rustc_hash::FxHashMap::default(), lint_caps: rustc_hash::FxHashMap::default(),
psess_created: Some(Box::new(|parse_sess| { 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(), source_map: parse_sess.clone_source_map(),
diagnostics, diagnostics,
})) })));
})), })),
register_lints: None, register_lints: None,
override_queries: None, override_queries: None,
@ -98,6 +98,9 @@ fn main() {
let _ = tcx.analysis(()); 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. // Read buffered diagnostics.
buffer.lock().unwrap().iter().for_each(|diagnostic| { buffer.lock().unwrap().iter().for_each(|diagnostic| {

View File

@ -8,7 +8,7 @@ otherwise be printed to stderr.
To get diagnostics from the compiler, To get diagnostics from the compiler,
configure [`rustc_interface::Config`] to output diagnostic to a buffer, configure [`rustc_interface::Config`] to output diagnostic to a buffer,
and run [`TyCtxt.analysis`]. The following was tested 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 ```rust
{{#include ../examples/rustc-driver-getting-diagnostics.rs}} {{#include ../examples/rustc-driver-getting-diagnostics.rs}}