Skip to content

Commit 6e6c46c

Browse files
authored
Rollup merge of rust-lang#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 rust-lang#66210 (comment). (May include traces of minor drive-by cleanup.) r? @Mark-Simulacrum
2 parents 05482d0 + 8444e16 commit 6e6c46c

File tree

13 files changed

+40
-53
lines changed

13 files changed

+40
-53
lines changed

Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -3522,6 +3522,7 @@ dependencies = [
35223522
"rustc",
35233523
"rustc_codegen_utils",
35243524
"rustc_data_structures",
3525+
"rustc_error_codes",
35253526
"rustc_errors",
35263527
"rustc_interface",
35273528
"rustc_lint",
@@ -3595,7 +3596,6 @@ dependencies = [
35953596
"rustc_codegen_ssa",
35963597
"rustc_codegen_utils",
35973598
"rustc_data_structures",
3598-
"rustc_error_codes",
35993599
"rustc_errors",
36003600
"rustc_incremental",
36013601
"rustc_lint",

src/librustc/session/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1037,12 +1037,11 @@ pub fn build_session_with_source_map(
10371037

10381038
let external_macro_backtrace = sopts.debugging_opts.external_macro_backtrace;
10391039

1040-
let emitter = match diagnostics_output {
1041-
DiagnosticOutput::Default => default_emitter(&sopts, registry, &source_map, None),
1042-
DiagnosticOutput::Raw(write) => {
1043-
default_emitter(&sopts, registry, &source_map, Some(write))
1044-
}
1040+
let write_dest = match diagnostics_output {
1041+
DiagnosticOutput::Default => None,
1042+
DiagnosticOutput::Raw(write) => Some(write),
10451043
};
1044+
let emitter = default_emitter(&sopts, registry, &source_map, write_dest);
10461045

10471046
let diagnostic_handler = errors::Handler::with_emitter_and_flags(
10481047
emitter,

src/librustc_driver/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ rustc_plugin = { path = "../librustc_plugin/deprecated" } # To get this in the s
2626
rustc_plugin_impl = { path = "../librustc_plugin" }
2727
rustc_save_analysis = { path = "../librustc_save_analysis" }
2828
rustc_codegen_utils = { path = "../librustc_codegen_utils" }
29+
rustc_error_codes = { path = "../librustc_error_codes" }
2930
rustc_interface = { path = "../librustc_interface" }
3031
rustc_serialize = { path = "../libserialize", package = "serialize" }
3132
rustc_resolve = { path = "../librustc_resolve" }

src/librustc_driver/lib.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc::ty::TyCtxt;
4040
use rustc::util::common::{set_time_depth, time, print_time_passes_entry, ErrorReported};
4141
use rustc_metadata::locator;
4242
use rustc_codegen_utils::codegen_backend::CodegenBackend;
43-
use errors::PResult;
43+
use errors::{PResult, registry::Registry};
4444
use rustc_interface::interface;
4545
use rustc_interface::util::get_codegen_sysroot;
4646
use rustc_data_structures::sync::SeqCst;
@@ -140,6 +140,10 @@ impl Callbacks for TimePassesCallbacks {
140140
}
141141
}
142142

143+
pub fn diagnostics_registry() -> Registry {
144+
Registry::new(&rustc_error_codes::DIAGNOSTICS)
145+
}
146+
143147
// Parse args and run the compiler. This is the primary entry point for rustc.
144148
// See comments on CompilerCalls below for details about the callbacks argument.
145149
// The FileLoader provides a way to load files from sources other than the file system.
@@ -182,13 +186,14 @@ pub fn run_compiler(
182186
lint_caps: Default::default(),
183187
register_lints: None,
184188
override_queries: None,
189+
registry: diagnostics_registry(),
185190
};
186191
callbacks.config(&mut config);
187192
config
188193
};
189194

190195
if let Some(ref code) = matches.opt_str("explain") {
191-
handle_explain(code, sopts.error_format);
196+
handle_explain(diagnostics_registry(), code, sopts.error_format);
192197
return Ok(());
193198
}
194199

@@ -261,6 +266,7 @@ pub fn run_compiler(
261266
lint_caps: Default::default(),
262267
register_lints: None,
263268
override_queries: None,
269+
registry: diagnostics_registry(),
264270
};
265271

266272
callbacks.config(&mut config);
@@ -510,15 +516,13 @@ fn stdout_isatty() -> bool {
510516
}
511517
}
512518

513-
fn handle_explain(code: &str,
514-
output: ErrorOutputType) {
515-
let descriptions = rustc_interface::util::diagnostics_registry();
519+
fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {
516520
let normalised = if code.starts_with("E") {
517521
code.to_string()
518522
} else {
519523
format!("E{0:0>4}", code)
520524
};
521-
match descriptions.find_description(&normalised) {
525+
match registry.find_description(&normalised) {
522526
Some(ref description) => {
523527
let mut is_in_code_block = false;
524528
let mut text = String::new();

src/librustc_error_codes/error_codes.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
// Error messages' format must follow the RFC 1567 available here:
88
// https://github.com/rust-lang/rfcs/pull/1567
99

10-
crate::register_diagnostics! {
11-
10+
register_diagnostics! {
1211
E0001: include_str!("./error_codes/E0001.md"),
1312
E0002: include_str!("./error_codes/E0002.md"),
1413
E0004: include_str!("./error_codes/E0004.md"),

src/librustc_error_codes/lib.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
//! This library is used to gather all error codes into one place. The goal
2-
//! being to make their maintenance easier.
1+
//! This library is used to gather all error codes into one place,
2+
//! the goal being to make their maintenance easier.
33
4-
#[macro_export]
54
macro_rules! register_diagnostics {
6-
($($ecode:ident: $message:expr,)*) => (
7-
$crate::register_diagnostics!{$($ecode:$message,)* ;}
8-
);
9-
105
($($ecode:ident: $message:expr,)* ; $($code:ident,)*) => (
116
pub static DIAGNOSTICS: &[(&str, &str)] = &[
127
$( (stringify!($ecode), $message), )*
138
];
149

1510
$(
16-
pub const $ecode: &str = $message;
11+
pub const $ecode: () = ();
1712
)*
1813
$(
1914
pub const $code: () = ();

src/librustc_interface/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ rustc_errors = { path = "../librustc_errors" }
3434
rustc_plugin = { path = "../librustc_plugin", package = "rustc_plugin_impl" }
3535
rustc_privacy = { path = "../librustc_privacy" }
3636
rustc_resolve = { path = "../librustc_resolve" }
37-
rustc_error_codes = { path = "../librustc_error_codes" }
3837
tempfile = "3.0.5"
3938
once_cell = "1"
4039

src/librustc_interface/interface.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_codegen_utils::codegen_backend::CodegenBackend;
1111
use rustc_data_structures::OnDrop;
1212
use rustc_data_structures::sync::Lrc;
1313
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
14+
use rustc_errors::registry::Registry;
1415
use rustc_parse::new_parser_from_source_str;
1516
use rustc::ty;
1617
use std::path::PathBuf;
@@ -141,19 +142,24 @@ pub struct Config {
141142
/// The second parameter is local providers and the third parameter is external providers.
142143
pub override_queries:
143144
Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>,
145+
146+
/// Registry of diagnostics codes.
147+
pub registry: Registry,
144148
}
145149

146-
pub fn run_compiler_in_existing_thread_pool<F, R>(config: Config, f: F) -> R
147-
where
148-
F: FnOnce(&Compiler) -> R,
149-
{
150+
pub fn run_compiler_in_existing_thread_pool<R>(
151+
config: Config,
152+
f: impl FnOnce(&Compiler) -> R,
153+
) -> R {
154+
let registry = &config.registry;
150155
let (sess, codegen_backend, source_map) = util::create_session(
151156
config.opts,
152157
config.crate_cfg,
153158
config.diagnostic_output,
154159
config.file_loader,
155160
config.input_path.clone(),
156161
config.lint_caps,
162+
registry.clone(),
157163
);
158164

159165
let compiler = Compiler {
@@ -171,17 +177,13 @@ where
171177
};
172178

173179
let _sess_abort_error = OnDrop(|| {
174-
compiler.sess.diagnostic().print_error_count(&util::diagnostics_registry());
180+
compiler.sess.diagnostic().print_error_count(registry);
175181
});
176182

177183
f(&compiler)
178184
}
179185

180-
pub fn run_compiler<F, R>(mut config: Config, f: F) -> R
181-
where
182-
F: FnOnce(&Compiler) -> R + Send,
183-
R: Send,
184-
{
186+
pub fn run_compiler<R: Send>(mut config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
185187
let stderr = config.stderr.take();
186188
util::spawn_thread_pool(
187189
config.opts.edition,
@@ -191,11 +193,7 @@ where
191193
)
192194
}
193195

194-
pub fn default_thread_pool<F, R>(edition: edition::Edition, f: F) -> R
195-
where
196-
F: FnOnce() -> R + Send,
197-
R: Send,
198-
{
196+
pub fn default_thread_pool<R: Send>(edition: edition::Edition, f: impl FnOnce() -> R + Send) -> R {
199197
// the 1 here is duplicating code in config.opts.debugging_opts.threads
200198
// which also defaults to 1; it ultimately doesn't matter as the default
201199
// isn't threaded, and just ignores this parameter

src/librustc_interface/util.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_data_structures::fx::{FxHashSet, FxHashMap};
1515
use rustc_errors::registry::Registry;
1616
use rustc_metadata::dynamic_lib::DynamicLibrary;
1717
use rustc_resolve::{self, Resolver};
18-
use rustc_error_codes;
1918
use std::env;
2019
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
2120
use std::io::{self, Write};
@@ -37,15 +36,6 @@ use syntax_pos::edition::Edition;
3736
#[cfg(not(parallel_compiler))]
3837
use std::{thread, panic};
3938

40-
pub fn diagnostics_registry() -> Registry {
41-
let mut all_errors = Vec::new();
42-
all_errors.extend_from_slice(&rustc_error_codes::DIAGNOSTICS);
43-
// FIXME: need to figure out a way to get these back in here
44-
// all_errors.extend_from_slice(get_codegen_backend(sess).diagnostics());
45-
46-
Registry::new(&all_errors)
47-
}
48-
4939
/// Adds `target_feature = "..."` cfgs for a variety of platform
5040
/// specific features (SSE, NEON etc.).
5141
///
@@ -77,9 +67,8 @@ pub fn create_session(
7767
file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>,
7868
input_path: Option<PathBuf>,
7969
lint_caps: FxHashMap<lint::LintId, lint::Level>,
70+
descriptions: Registry,
8071
) -> (Lrc<Session>, Lrc<Box<dyn CodegenBackend>>, Lrc<SourceMap>) {
81-
let descriptions = diagnostics_registry();
82-
8372
let loader = file_loader.unwrap_or(box RealFileLoader);
8473
let source_map = Lrc::new(SourceMap::with_file_loader(
8574
loader,

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use syntax::feature_gate::UnstableFeatures;
2121
use errors::json::JsonEmitter;
2222
use syntax::symbol::sym;
2323
use syntax_pos::DUMMY_SP;
24-
use errors;
2524
use errors::emitter::{Emitter, EmitterWriter};
2625

2726
use std::cell::RefCell;
@@ -341,6 +340,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
341340
lint_caps,
342341
register_lints: None,
343342
override_queries: None,
343+
registry: rustc_driver::diagnostics_registry(),
344344
};
345345

346346
interface::run_compiler_in_existing_thread_pool(config, |compiler| {

src/librustdoc/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ extern crate getopts;
2323
extern crate env_logger;
2424
extern crate rustc;
2525
extern crate rustc_data_structures;
26-
extern crate rustc_index;
2726
extern crate rustc_driver;
27+
extern crate rustc_error_codes;
28+
extern crate rustc_index;
2829
extern crate rustc_resolve;
2930
extern crate rustc_lint;
3031
extern crate rustc_interface;

src/librustdoc/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub fn run(options: Options) -> i32 {
8080
lint_caps: Default::default(),
8181
register_lints: None,
8282
override_queries: None,
83+
registry: rustc_driver::diagnostics_registry(),
8384
};
8485

8586
let mut test_args = options.test_args.clone();

src/test/run-make-fulldeps/issue-19371/foo.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
extern crate rustc;
44
extern crate rustc_interface;
5-
extern crate rustc_driver as _;
5+
extern crate rustc_driver;
66
extern crate syntax;
77

88
use rustc::session::DiagnosticOutput;
@@ -61,6 +61,7 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
6161
lint_caps: Default::default(),
6262
register_lints: None,
6363
override_queries: None,
64+
registry: rustc_driver::diagnostics_registry(),
6465
};
6566

6667
interface::run_compiler(config, |compiler| {

0 commit comments

Comments
 (0)