|
| 1 | +# Example: Getting diagnostic through `rustc_interface` |
| 2 | + |
| 3 | +`rustc_interface` allows you to intercept diagnostics that would otherwise be printed to stderr. |
| 4 | + |
| 5 | +## Getting diagnostics |
| 6 | + |
| 7 | +NOTE: For the example to compile, you will need to first run the following: |
| 8 | + |
| 9 | + rustup component add rustc-dev |
| 10 | + |
| 11 | +To get diagnostics from the compiler, configure `rustc_interface::Config` to output diagnostic to a buffer, and run `TyCtxt.analysis`: |
| 12 | + |
| 13 | +```rust |
| 14 | +#![feature(rustc_private)] |
| 15 | + |
| 16 | +extern crate rustc; |
| 17 | +extern crate rustc_error_codes; |
| 18 | +extern crate rustc_errors; |
| 19 | +extern crate rustc_hash; |
| 20 | +extern crate rustc_hir; |
| 21 | +extern crate rustc_interface; |
| 22 | +extern crate rustc_session; |
| 23 | +extern crate rustc_span; |
| 24 | + |
| 25 | +use rustc_errors::registry; |
| 26 | +use rustc_session::config; |
| 27 | +use rustc_span::source_map; |
| 28 | +use std::io; |
| 29 | +use std::path; |
| 30 | +use std::process; |
| 31 | +use std::str; |
| 32 | +use std::sync; |
| 33 | + |
| 34 | +// Buffer diagnostics in a Vec<u8>. |
| 35 | +#[derive(Clone)] |
| 36 | +pub struct DiagnosticSink(sync::Arc<sync::Mutex<Vec<u8>>>); |
| 37 | + |
| 38 | +impl io::Write for DiagnosticSink { |
| 39 | + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 40 | + self.0.lock().unwrap().write(buf) |
| 41 | + } |
| 42 | + fn flush(&mut self) -> io::Result<()> { |
| 43 | + self.0.lock().unwrap().flush() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +fn main() { |
| 48 | + let out = process::Command::new("rustc") |
| 49 | + .arg("--print=sysroot") |
| 50 | + .current_dir(".") |
| 51 | + .output() |
| 52 | + .unwrap(); |
| 53 | + let sysroot = str::from_utf8(&out.stdout).unwrap().trim(); |
| 54 | + let buffer = sync::Arc::new(sync::Mutex::new(Vec::new())); |
| 55 | + let config = rustc_interface::Config { |
| 56 | + opts: config::Options { |
| 57 | + maybe_sysroot: Some(path::PathBuf::from(sysroot)), |
| 58 | + // Configure the compiler to emit diagnostics in compact JSON format. |
| 59 | + error_format: config::ErrorOutputType::Json { |
| 60 | + pretty: false, |
| 61 | + json_rendered: rustc_errors::emitter::HumanReadableErrorType::Default( |
| 62 | + rustc_errors::emitter::ColorConfig::Never, |
| 63 | + ), |
| 64 | + }, |
| 65 | + ..config::Options::default() |
| 66 | + }, |
| 67 | + // This program contains a type error. |
| 68 | + input: config::Input::Str { |
| 69 | + name: source_map::FileName::Custom("main.rs".to_string()), |
| 70 | + input: "fn main() { let x: &str = 1; }".to_string(), |
| 71 | + }, |
| 72 | + // Redirect the diagnostic output of the compiler to a buffer. |
| 73 | + diagnostic_output: rustc_session::DiagnosticOutput::Raw(Box::from(DiagnosticSink( |
| 74 | + buffer.clone(), |
| 75 | + ))), |
| 76 | + crate_cfg: rustc_hash::FxHashSet::default(), |
| 77 | + input_path: None, |
| 78 | + output_dir: None, |
| 79 | + output_file: None, |
| 80 | + file_loader: None, |
| 81 | + stderr: None, |
| 82 | + crate_name: None, |
| 83 | + lint_caps: rustc_hash::FxHashMap::default(), |
| 84 | + register_lints: None, |
| 85 | + override_queries: None, |
| 86 | + registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS), |
| 87 | + }; |
| 88 | + rustc_interface::run_compiler(config, |compiler| { |
| 89 | + compiler.enter(|queries| { |
| 90 | + queries.global_ctxt().unwrap().take().enter(|tcx| { |
| 91 | + // Run the analysis phase on the local crate to trigger the type error. |
| 92 | + tcx.analysis(rustc_hir::def_id::LOCAL_CRATE); |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | + // Read and print buffered diagnostics. |
| 97 | + let diagnostics = String::from_utf8(buffer.lock().unwrap().clone()).unwrap(); |
| 98 | + println!("{}", diagnostics); |
| 99 | +} |
| 100 | + |
| 101 | +``` |
0 commit comments