Skip to content

Commit 7cffa3b

Browse files
Vetle RasmussenNoratrieb
Vetle Rasmussen
authored andcommitted
Fix getting diagnostics example
1 parent 4fa05b6 commit 7cffa3b

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

examples/rustc-driver-getting-diagnostics.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ fn main() {
7777
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
7878
lint_caps: rustc_hash::FxHashMap::default(),
7979
psess_created: Some(Box::new(|parse_sess| {
80-
parse_sess.dcx = DiagCtxt::new(Box::new(DebugEmitter {
80+
parse_sess.set_dcx(DiagCtxt::new(Box::new(DebugEmitter {
8181
source_map: parse_sess.clone_source_map(),
8282
diagnostics,
83-
}))
83+
})));
8484
})),
8585
register_lints: None,
8686
override_queries: None,
@@ -98,6 +98,9 @@ fn main() {
9898
let _ = tcx.analysis(());
9999
});
100100
});
101+
// If the compiler has encountered errors when this closure returns, it will abort (!) the program.
102+
// We avoid this by resetting the error count before returning
103+
compiler.sess.dcx().reset_err_count();
101104
});
102105
// Read buffered diagnostics.
103106
buffer.lock().unwrap().iter().for_each(|diagnostic| {

src/rustc-driver-getting-diagnostics.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ otherwise be printed to stderr.
88
To get diagnostics from the compiler,
99
configure [`rustc_interface::Config`] to output diagnostic to a buffer,
1010
and run [`TyCtxt.analysis`]. The following was tested
11-
with <!-- date-check: may 2024 --> `nightly-2024-05-09`:
11+
with <!-- date-check: september 2024 --> `nightly-2024-09-16`:
1212

1313
```rust
1414
{{#include ../examples/rustc-driver-getting-diagnostics.rs}}

0 commit comments

Comments
 (0)