Skip to content

Commit 1e73a7d

Browse files
committed
Fewer early errors.
`build_session` is passed an `EarlyErrorHandler` and then constructs a `Handler`. But the `EarlyErrorHandler` is still used for some time after that. This commit changes `build_session` so it consumes the passed `EarlyErrorHandler`, and also drops it as soon as the `Handler` is built. As a result, `parse_cfg` and `parse_check_cfg` now take a `Handler` instead of an `EarlyErrorHandler`.
1 parent f9a4947 commit 1e73a7d

File tree

5 files changed

+51
-26
lines changed

5 files changed

+51
-26
lines changed

compiler/rustc_interface/src/interface.rs

+27-16
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct Compiler {
4242
}
4343

4444
/// Converts strings provided as `--cfg [cfgspec]` into a `Cfg`.
45-
pub(crate) fn parse_cfg(handler: &EarlyErrorHandler, cfgs: Vec<String>) -> Cfg {
45+
pub(crate) fn parse_cfg(handler: &Handler, cfgs: Vec<String>) -> Cfg {
4646
cfgs.into_iter()
4747
.map(|s| {
4848
let sess = ParseSess::with_silent_emitter(Some(format!(
@@ -52,10 +52,14 @@ pub(crate) fn parse_cfg(handler: &EarlyErrorHandler, cfgs: Vec<String>) -> Cfg {
5252

5353
macro_rules! error {
5454
($reason: expr) => {
55-
handler.early_error(format!(
56-
concat!("invalid `--cfg` argument: `{}` (", $reason, ")"),
57-
s
58-
));
55+
#[allow(rustc::untranslatable_diagnostic)]
56+
#[allow(rustc::diagnostic_outside_of_impl)]
57+
handler
58+
.struct_fatal(format!(
59+
concat!("invalid `--cfg` argument: `{}` (", $reason, ")"),
60+
s
61+
))
62+
.emit();
5963
};
6064
}
6165

@@ -97,7 +101,10 @@ pub(crate) fn parse_cfg(handler: &EarlyErrorHandler, cfgs: Vec<String>) -> Cfg {
97101
}
98102

99103
/// Converts strings provided as `--check-cfg [specs]` into a `CheckCfg`.
100-
pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -> CheckCfg {
104+
// njn: temp
105+
#[allow(rustc::untranslatable_diagnostic)]
106+
#[allow(rustc::diagnostic_outside_of_impl)]
107+
pub(crate) fn parse_check_cfg(handler: &Handler, specs: Vec<String>) -> CheckCfg {
101108
// If any --check-cfg is passed then exhaustive_values and exhaustive_names
102109
// are enabled by default.
103110
let exhaustive_names = !specs.is_empty();
@@ -113,10 +120,14 @@ pub(crate) fn parse_check_cfg(handler: &EarlyErrorHandler, specs: Vec<String>) -
113120

114121
macro_rules! error {
115122
($reason:expr) => {
116-
handler.early_error(format!(
117-
concat!("invalid `--check-cfg` argument: `{}` (", $reason, ")"),
118-
s
119-
))
123+
#[allow(rustc::untranslatable_diagnostic)]
124+
#[allow(rustc::diagnostic_outside_of_impl)]
125+
handler
126+
.struct_fatal(format!(
127+
concat!("invalid `--check-cfg` argument: `{}` (", $reason, ")"),
128+
s
129+
))
130+
.emit()
120131
};
121132
}
122133

@@ -388,13 +399,13 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
388399
|| {
389400
crate::callbacks::setup_callbacks();
390401

391-
let handler = EarlyErrorHandler::new(config.opts.error_format);
402+
let early_handler = EarlyErrorHandler::new(config.opts.error_format);
392403

393404
let codegen_backend = if let Some(make_codegen_backend) = config.make_codegen_backend {
394405
make_codegen_backend(&config.opts)
395406
} else {
396407
util::get_codegen_backend(
397-
&handler,
408+
&early_handler,
398409
&config.opts.maybe_sysroot,
399410
config.opts.unstable_opts.codegen_backend.as_deref(),
400411
)
@@ -411,7 +422,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
411422
) {
412423
Ok(bundle) => bundle,
413424
Err(e) => {
414-
handler.early_error(format!("failed to load fluent bundle: {e}"));
425+
early_handler.early_error(format!("failed to load fluent bundle: {e}"));
415426
}
416427
};
417428

@@ -422,7 +433,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
422433
let target_override = codegen_backend.target_override(&config.opts);
423434

424435
let mut sess = rustc_session::build_session(
425-
&handler,
436+
early_handler,
426437
config.opts,
427438
CompilerIO {
428439
input: config.input,
@@ -444,12 +455,12 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
444455

445456
codegen_backend.init(&sess);
446457

447-
let cfg = parse_cfg(&handler, config.crate_cfg);
458+
let cfg = parse_cfg(&sess.diagnostic(), config.crate_cfg);
448459
let mut cfg = config::build_configuration(&sess, cfg);
449460
util::add_configuration(&mut cfg, &mut sess, &*codegen_backend);
450461
sess.parse_sess.config = cfg;
451462

452-
let mut check_cfg = parse_check_cfg(&handler, config.crate_check_cfg);
463+
let mut check_cfg = parse_check_cfg(&sess.diagnostic(), config.crate_check_cfg);
453464
check_cfg.fill_well_known(&sess.target);
454465
sess.parse_sess.check_config = check_cfg;
455466

compiler/rustc_interface/src/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ use std::path::{Path, PathBuf};
2626
use std::sync::Arc;
2727

2828
fn mk_session(matches: getopts::Matches) -> (Session, Cfg) {
29-
let mut handler = EarlyErrorHandler::new(ErrorOutputType::default());
29+
let mut early_handler = EarlyErrorHandler::new(ErrorOutputType::default());
3030
let registry = registry::Registry::new(&[]);
31-
let sessopts = build_session_options(&mut handler, &matches);
32-
let cfg = parse_cfg(&handler, matches.opt_strs("cfg"));
31+
let sessopts = build_session_options(&mut early_handler, &matches);
3332
let temps_dir = sessopts.unstable_opts.temps_dir.as_deref().map(PathBuf::from);
3433
let io = CompilerIO {
3534
input: Input::Str { name: FileName::Custom(String::new()), input: String::new() },
@@ -38,7 +37,7 @@ fn mk_session(matches: getopts::Matches) -> (Session, Cfg) {
3837
temps_dir,
3938
};
4039
let sess = build_session(
41-
&handler,
40+
early_handler,
4241
sessopts,
4342
io,
4443
None,
@@ -52,6 +51,7 @@ fn mk_session(matches: getopts::Matches) -> (Session, Cfg) {
5251
Arc::default(),
5352
Default::default(),
5453
);
54+
let cfg = parse_cfg(&sess.diagnostic(), matches.opt_strs("cfg"));
5555
(sess, cfg)
5656
}
5757

compiler/rustc_session/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has
1616
1717
session_expr_parentheses_needed = parentheses are required to parse this as an expression
1818
19+
session_failed_to_create_profiler = failed to create profiler: {$err}
20+
1921
session_feature_diagnostic_for_issue =
2022
see issue #{$n} <https://github.com/rust-lang/rust/issues/{$n}> for more information
2123
@@ -73,6 +75,7 @@ session_not_supported = not supported
7375
session_nul_in_c_str = null characters in C string literals are not supported
7476
7577
session_octal_float_literal_not_supported = octal float literal is not supported
78+
7679
session_optimization_fuel_exhausted = optimization-fuel-exhausted: {$msg}
7780
7881
session_profile_sample_use_file_does_not_exist = file `{$path}` passed to `-C profile-sample-use` does not exist.

compiler/rustc_session/src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,9 @@ pub(crate) struct FunctionReturnRequiresX86OrX8664;
444444
#[derive(Diagnostic)]
445445
#[diag(session_function_return_thunk_extern_requires_non_large_code_model)]
446446
pub(crate) struct FunctionReturnThunkExternRequiresNonLargeCodeModel;
447+
448+
#[derive(Diagnostic)]
449+
#[diag(session_failed_to_create_profiler)]
450+
pub struct FailedToCreateProfiler {
451+
pub err: String,
452+
}

compiler/rustc_session/src/session.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ fn default_emitter(
13491349
// JUSTIFICATION: literally session construction
13501350
#[allow(rustc::bad_opt_access)]
13511351
pub fn build_session(
1352-
handler: &EarlyErrorHandler,
1352+
early_handler: EarlyErrorHandler,
13531353
sopts: config::Options,
13541354
io: CompilerIO,
13551355
bundle: Option<Lrc<rustc_errors::FluentBundle>>,
@@ -1379,12 +1379,13 @@ pub fn build_session(
13791379
None => filesearch::get_or_default_sysroot().expect("Failed finding sysroot"),
13801380
};
13811381

1382-
let target_cfg = config::build_target_config(handler, &sopts, target_override, &sysroot);
1382+
let target_cfg = config::build_target_config(&early_handler, &sopts, target_override, &sysroot);
13831383
let host_triple = TargetTriple::from_triple(config::host_triple());
1384-
let (host, target_warnings) = Target::search(&host_triple, &sysroot)
1385-
.unwrap_or_else(|e| handler.early_error(format!("Error loading host specification: {e}")));
1384+
let (host, target_warnings) = Target::search(&host_triple, &sysroot).unwrap_or_else(|e| {
1385+
early_handler.early_error(format!("Error loading host specification: {e}"))
1386+
});
13861387
for warning in target_warnings.warning_messages() {
1387-
handler.early_warn(warning)
1388+
early_handler.early_warn(warning)
13881389
}
13891390

13901391
let loader = file_loader.unwrap_or_else(|| Box::new(RealFileLoader));
@@ -1413,6 +1414,10 @@ pub fn build_session(
14131414
span_diagnostic = span_diagnostic.with_ice_file(ice_file);
14141415
}
14151416

1417+
// Now that the proper handler has been constructed, drop the early handler
1418+
// to prevent accidental use.
1419+
drop(early_handler);
1420+
14161421
let self_profiler = if let SwitchWithOptPath::Enabled(ref d) = sopts.unstable_opts.self_profile
14171422
{
14181423
let directory =
@@ -1427,7 +1432,7 @@ pub fn build_session(
14271432
match profiler {
14281433
Ok(profiler) => Some(Arc::new(profiler)),
14291434
Err(e) => {
1430-
handler.early_warn(format!("failed to create profiler: {e}"));
1435+
span_diagnostic.emit_warning(errors::FailedToCreateProfiler { err: e.to_string() });
14311436
None
14321437
}
14331438
}

0 commit comments

Comments
 (0)