From 8444e1628cd5e2d7229b3176cb5b248271defe22 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Fri, 15 Nov 2019 19:41:50 +0100 Subject: [PATCH] move DIAGNOSTICS usage to rustc_driver --- Cargo.lock | 2 +- src/librustc/session/mod.rs | 9 +++--- src/librustc_driver/Cargo.toml | 1 + src/librustc_driver/lib.rs | 16 +++++++---- src/librustc_error_codes/error_codes.rs | 3 +- src/librustc_error_codes/lib.rs | 11 ++------ src/librustc_interface/Cargo.toml | 1 - src/librustc_interface/interface.rs | 28 +++++++++---------- src/librustc_interface/util.rs | 13 +-------- src/librustdoc/core.rs | 2 +- src/librustdoc/lib.rs | 3 +- src/librustdoc/test.rs | 1 + src/test/run-make-fulldeps/issue-19371/foo.rs | 3 +- 13 files changed, 40 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bb2bd3c314c..9094461067b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3519,6 +3519,7 @@ dependencies = [ "rustc", "rustc_codegen_utils", "rustc_data_structures", + "rustc_error_codes", "rustc_errors", "rustc_interface", "rustc_lint", @@ -3592,7 +3593,6 @@ dependencies = [ "rustc_codegen_ssa", "rustc_codegen_utils", "rustc_data_structures", - "rustc_error_codes", "rustc_errors", "rustc_incremental", "rustc_lint", diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index a69584cb90a..63ff92b5ecb 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -1039,12 +1039,11 @@ pub fn build_session_with_source_map( let external_macro_backtrace = sopts.debugging_opts.external_macro_backtrace; - let emitter = match diagnostics_output { - DiagnosticOutput::Default => default_emitter(&sopts, registry, &source_map, None), - DiagnosticOutput::Raw(write) => { - default_emitter(&sopts, registry, &source_map, Some(write)) - } + let write_dest = match diagnostics_output { + DiagnosticOutput::Default => None, + DiagnosticOutput::Raw(write) => Some(write), }; + let emitter = default_emitter(&sopts, registry, &source_map, write_dest); let diagnostic_handler = errors::Handler::with_emitter_and_flags( emitter, diff --git a/src/librustc_driver/Cargo.toml b/src/librustc_driver/Cargo.toml index f1565a2bd0a..ff673e52b60 100644 --- a/src/librustc_driver/Cargo.toml +++ b/src/librustc_driver/Cargo.toml @@ -26,6 +26,7 @@ rustc_plugin = { path = "../librustc_plugin/deprecated" } # To get this in the s rustc_plugin_impl = { path = "../librustc_plugin" } rustc_save_analysis = { path = "../librustc_save_analysis" } rustc_codegen_utils = { path = "../librustc_codegen_utils" } +rustc_error_codes = { path = "../librustc_error_codes" } rustc_interface = { path = "../librustc_interface" } rustc_serialize = { path = "../libserialize", package = "serialize" } rustc_resolve = { path = "../librustc_resolve" } diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index d4f796ee3a7..ef638464adc 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -40,7 +40,7 @@ use rustc::util::common::{set_time_depth, time, print_time_passes_entry, ErrorReported}; use rustc_metadata::locator; use rustc_codegen_utils::codegen_backend::CodegenBackend; -use errors::PResult; +use errors::{PResult, registry::Registry}; use rustc_interface::interface; use rustc_interface::util::get_codegen_sysroot; use rustc_data_structures::sync::SeqCst; @@ -140,6 +140,10 @@ fn config(&mut self, config: &mut interface::Config) { } } +pub fn diagnostics_registry() -> Registry { + Registry::new(&rustc_error_codes::DIAGNOSTICS) +} + // Parse args and run the compiler. This is the primary entry point for rustc. // See comments on CompilerCalls below for details about the callbacks argument. // The FileLoader provides a way to load files from sources other than the file system. @@ -182,13 +186,14 @@ pub fn run_compiler( lint_caps: Default::default(), register_lints: None, override_queries: None, + registry: diagnostics_registry(), }; callbacks.config(&mut config); config }; if let Some(ref code) = matches.opt_str("explain") { - handle_explain(code, sopts.error_format); + handle_explain(diagnostics_registry(), code, sopts.error_format); return Ok(()); } @@ -261,6 +266,7 @@ pub fn run_compiler( lint_caps: Default::default(), register_lints: None, override_queries: None, + registry: diagnostics_registry(), }; callbacks.config(&mut config); @@ -510,15 +516,13 @@ fn stdout_isatty() -> bool { } } -fn handle_explain(code: &str, - output: ErrorOutputType) { - let descriptions = rustc_interface::util::diagnostics_registry(); +fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) { let normalised = if code.starts_with("E") { code.to_string() } else { format!("E{0:0>4}", code) }; - match descriptions.find_description(&normalised) { + match registry.find_description(&normalised) { Some(ref description) => { let mut is_in_code_block = false; let mut text = String::new(); diff --git a/src/librustc_error_codes/error_codes.rs b/src/librustc_error_codes/error_codes.rs index ffefe51f854..428cecf13a3 100644 --- a/src/librustc_error_codes/error_codes.rs +++ b/src/librustc_error_codes/error_codes.rs @@ -7,8 +7,7 @@ // Error messages' format must follow the RFC 1567 available here: // https://github.com/rust-lang/rfcs/pull/1567 -crate::register_diagnostics! { - +register_diagnostics! { E0001: include_str!("./error_codes/E0001.md"), E0002: include_str!("./error_codes/E0002.md"), E0004: include_str!("./error_codes/E0004.md"), diff --git a/src/librustc_error_codes/lib.rs b/src/librustc_error_codes/lib.rs index 36b1518c869..14210fd69ad 100644 --- a/src/librustc_error_codes/lib.rs +++ b/src/librustc_error_codes/lib.rs @@ -1,19 +1,14 @@ -//! This library is used to gather all error codes into one place. The goal -//! being to make their maintenance easier. +//! This library is used to gather all error codes into one place, +//! the goal being to make their maintenance easier. -#[macro_export] macro_rules! register_diagnostics { - ($($ecode:ident: $message:expr,)*) => ( - $crate::register_diagnostics!{$($ecode:$message,)* ;} - ); - ($($ecode:ident: $message:expr,)* ; $($code:ident,)*) => ( pub static DIAGNOSTICS: &[(&str, &str)] = &[ $( (stringify!($ecode), $message), )* ]; $( - pub const $ecode: &str = $message; + pub const $ecode: () = (); )* $( pub const $code: () = (); diff --git a/src/librustc_interface/Cargo.toml b/src/librustc_interface/Cargo.toml index b16cfe9efa7..de59882bbdf 100644 --- a/src/librustc_interface/Cargo.toml +++ b/src/librustc_interface/Cargo.toml @@ -34,7 +34,6 @@ rustc_errors = { path = "../librustc_errors" } rustc_plugin = { path = "../librustc_plugin", package = "rustc_plugin_impl" } rustc_privacy = { path = "../librustc_privacy" } rustc_resolve = { path = "../librustc_resolve" } -rustc_error_codes = { path = "../librustc_error_codes" } tempfile = "3.0.5" once_cell = "1" diff --git a/src/librustc_interface/interface.rs b/src/librustc_interface/interface.rs index fae4eb48c4a..c153de7103a 100644 --- a/src/librustc_interface/interface.rs +++ b/src/librustc_interface/interface.rs @@ -11,6 +11,7 @@ use rustc_data_structures::OnDrop; use rustc_data_structures::sync::Lrc; use rustc_data_structures::fx::{FxHashSet, FxHashMap}; +use rustc_errors::registry::Registry; use rustc_parse::new_parser_from_source_str; use rustc::ty; use std::path::PathBuf; @@ -141,12 +142,16 @@ pub struct Config { /// The second parameter is local providers and the third parameter is external providers. pub override_queries: Option, &mut ty::query::Providers<'_>)>, + + /// Registry of diagnostics codes. + pub registry: Registry, } -pub fn run_compiler_in_existing_thread_pool(config: Config, f: F) -> R -where - F: FnOnce(&Compiler) -> R, -{ +pub fn run_compiler_in_existing_thread_pool( + config: Config, + f: impl FnOnce(&Compiler) -> R, +) -> R { + let registry = &config.registry; let (sess, codegen_backend, source_map) = util::create_session( config.opts, config.crate_cfg, @@ -154,6 +159,7 @@ pub fn run_compiler_in_existing_thread_pool(config: Config, f: F) -> R config.file_loader, config.input_path.clone(), config.lint_caps, + registry.clone(), ); let compiler = Compiler { @@ -171,17 +177,13 @@ pub fn run_compiler_in_existing_thread_pool(config: Config, f: F) -> R }; let _sess_abort_error = OnDrop(|| { - compiler.sess.diagnostic().print_error_count(&util::diagnostics_registry()); + compiler.sess.diagnostic().print_error_count(registry); }); f(&compiler) } -pub fn run_compiler(mut config: Config, f: F) -> R -where - F: FnOnce(&Compiler) -> R + Send, - R: Send, -{ +pub fn run_compiler(mut config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R { let stderr = config.stderr.take(); util::spawn_thread_pool( config.opts.edition, @@ -191,11 +193,7 @@ pub fn run_compiler(mut config: Config, f: F) -> R ) } -pub fn default_thread_pool(edition: edition::Edition, f: F) -> R -where - F: FnOnce() -> R + Send, - R: Send, -{ +pub fn default_thread_pool(edition: edition::Edition, f: impl FnOnce() -> R + Send) -> R { // the 1 here is duplicating code in config.opts.debugging_opts.threads // which also defaults to 1; it ultimately doesn't matter as the default // isn't threaded, and just ignores this parameter diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index a5653a21b31..2b4320c04e6 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -15,7 +15,6 @@ use rustc_errors::registry::Registry; use rustc_metadata::dynamic_lib::DynamicLibrary; use rustc_resolve::{self, Resolver}; -use rustc_error_codes; use std::env; use std::env::consts::{DLL_PREFIX, DLL_SUFFIX}; use std::io::{self, Write}; @@ -37,15 +36,6 @@ #[cfg(not(parallel_compiler))] use std::{thread, panic}; -pub fn diagnostics_registry() -> Registry { - let mut all_errors = Vec::new(); - all_errors.extend_from_slice(&rustc_error_codes::DIAGNOSTICS); - // FIXME: need to figure out a way to get these back in here - // all_errors.extend_from_slice(get_codegen_backend(sess).diagnostics()); - - Registry::new(&all_errors) -} - /// Adds `target_feature = "..."` cfgs for a variety of platform /// specific features (SSE, NEON etc.). /// @@ -77,9 +67,8 @@ pub fn create_session( file_loader: Option>, input_path: Option, lint_caps: FxHashMap, + descriptions: Registry, ) -> (Lrc, Lrc>, Lrc) { - let descriptions = diagnostics_registry(); - let loader = file_loader.unwrap_or(box RealFileLoader); let source_map = Lrc::new(SourceMap::with_file_loader( loader, diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index a40325448b1..d6f67514394 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -21,7 +21,6 @@ use syntax::json::JsonEmitter; use syntax::symbol::sym; use syntax_pos::DUMMY_SP; -use errors; use errors::emitter::{Emitter, EmitterWriter}; use std::cell::RefCell; @@ -341,6 +340,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt lint_caps, register_lints: None, override_queries: None, + registry: rustc_driver::diagnostics_registry(), }; interface::run_compiler_in_existing_thread_pool(config, |compiler| { diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 4f102dbf128..8bfaf98f086 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -23,8 +23,9 @@ extern crate env_logger; extern crate rustc; extern crate rustc_data_structures; -extern crate rustc_index; extern crate rustc_driver; +extern crate rustc_error_codes; +extern crate rustc_index; extern crate rustc_resolve; extern crate rustc_lint; extern crate rustc_interface; diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index c483f6fb477..98e18d666f1 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -80,6 +80,7 @@ pub fn run(options: Options) -> i32 { lint_caps: Default::default(), register_lints: None, override_queries: None, + registry: rustc_driver::diagnostics_registry(), }; let mut test_args = options.test_args.clone(); diff --git a/src/test/run-make-fulldeps/issue-19371/foo.rs b/src/test/run-make-fulldeps/issue-19371/foo.rs index 9f1850dfb4c..f9ecff2abaa 100644 --- a/src/test/run-make-fulldeps/issue-19371/foo.rs +++ b/src/test/run-make-fulldeps/issue-19371/foo.rs @@ -2,7 +2,7 @@ extern crate rustc; extern crate rustc_interface; -extern crate rustc_driver as _; +extern crate rustc_driver; extern crate syntax; use rustc::session::DiagnosticOutput; @@ -61,6 +61,7 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) { lint_caps: Default::default(), register_lints: None, override_queries: None, + registry: rustc_driver::diagnostics_registry(), }; interface::run_compiler(config, |compiler| {