Rollup merge of #66456 - Centril:driver-codes, r=Mark-Simulacrum

Move `DIAGNOSTICS` usage to `rustc_driver`

Remove `rustc_interface`'s dependency on `rustc_error_codes` and centralize all usages of `DIAGNOSTICS` in `rustc_driver`. Once we remove all references to `rustc_error_codes` in all other crates but `rustc_driver`, this should allow for incremental recompilation of the compiler to be smoother when tweaking error codes. This works towards https://github.com/rust-lang/rust/issues/66210#issuecomment-551862528.

(May include traces of minor drive-by cleanup.)

r? @Mark-Simulacrum
This commit is contained in:
Yuki Okushi 2019-11-17 13:36:18 +09:00 committed by GitHub
commit 6e6c46c694
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 40 additions and 53 deletions

View File

@ -3522,6 +3522,7 @@ dependencies = [
"rustc",
"rustc_codegen_utils",
"rustc_data_structures",
"rustc_error_codes",
"rustc_errors",
"rustc_interface",
"rustc_lint",
@ -3595,7 +3596,6 @@ dependencies = [
"rustc_codegen_ssa",
"rustc_codegen_utils",
"rustc_data_structures",
"rustc_error_codes",
"rustc_errors",
"rustc_incremental",
"rustc_lint",

View File

@ -1037,12 +1037,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,

View File

@ -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" }

View File

@ -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();

View File

@ -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"),

View File

@ -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: () = ();

View File

@ -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"

View File

@ -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<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>,
/// Registry of diagnostics codes.
pub registry: Registry,
}
pub fn run_compiler_in_existing_thread_pool<F, R>(config: Config, f: F) -> R
where
F: FnOnce(&Compiler) -> R,
{
pub fn run_compiler_in_existing_thread_pool<R>(
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<F, R>(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<F, R>(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<F, R>(mut config: Config, f: F) -> R
where
F: FnOnce(&Compiler) -> R + Send,
R: Send,
{
pub fn run_compiler<R: Send>(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<F, R>(mut config: Config, f: F) -> R
)
}
pub fn default_thread_pool<F, R>(edition: edition::Edition, f: F) -> R
where
F: FnOnce() -> R + Send,
R: Send,
{
pub fn default_thread_pool<R: Send>(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

View File

@ -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<Box<dyn FileLoader + Send + Sync + 'static>>,
input_path: Option<PathBuf>,
lint_caps: FxHashMap<lint::LintId, lint::Level>,
descriptions: Registry,
) -> (Lrc<Session>, Lrc<Box<dyn CodegenBackend>>, Lrc<SourceMap>) {
let descriptions = diagnostics_registry();
let loader = file_loader.unwrap_or(box RealFileLoader);
let source_map = Lrc::new(SourceMap::with_file_loader(
loader,

View File

@ -21,7 +21,6 @@
use errors::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| {

View File

@ -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;

View File

@ -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();

View File

@ -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| {