Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move some driver code around #138786

Merged
merged 3 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_driver_impl/messages.ftl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
driver_impl_cant_emit_mir = could not emit MIR: {$error}

driver_impl_ice = the compiler unexpectedly panicked. this is a bug.
driver_impl_ice_bug_report = we would appreciate a bug report: {$bug_report_url}
driver_impl_ice_bug_report_internal_feature = using internal features is not supported and expected to cause internal compiler errors when used incorrectly
Expand Down
27 changes: 14 additions & 13 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ mod signal_handler {
}

use crate::session_diagnostics::{
RLinkEmptyVersionNumber, RLinkEncodingVersionMismatch, RLinkRustcVersionMismatch,
CantEmitMIR, RLinkEmptyVersionNumber, RLinkEncodingVersionMismatch, RLinkRustcVersionMismatch,
RLinkWrongFileType, RlinkCorruptFile, RlinkNotAFile, RlinkUnableToRead, UnstableFeatureUsage,
};

Expand Down Expand Up @@ -243,12 +243,17 @@ pub fn run_compiler(at_args: &[String], callbacks: &mut (dyn Callbacks + Send))
return;
}

let input = make_input(&default_early_dcx, &matches.free);
let has_input = input.is_some();
let (odir, ofile) = make_output(&matches);

drop(default_early_dcx);

let mut config = interface::Config {
opts: sopts,
crate_cfg: matches.opt_strs("cfg"),
crate_check_cfg: matches.opt_strs("check-cfg"),
input: Input::File(PathBuf::new()),
input: input.unwrap_or(Input::File(PathBuf::new())),
output_file: ofile,
output_dir: odir,
ice_file,
Expand All @@ -265,16 +270,6 @@ pub fn run_compiler(at_args: &[String], callbacks: &mut (dyn Callbacks + Send))
expanded_args: args,
};

let has_input = match make_input(&default_early_dcx, &matches.free) {
Some(input) => {
config.input = input;
true // has input: normal compilation
}
None => false, // no input: we will exit early
};

drop(default_early_dcx);

callbacks.config(&mut config);

let registered_lints = config.register_lints.is_some();
Expand Down Expand Up @@ -379,6 +374,12 @@ pub fn run_compiler(at_args: &[String], callbacks: &mut (dyn Callbacks + Send))
return early_exit();
}

if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
if let Err(error) = rustc_mir_transform::dump_mir::emit_mir(tcx) {
tcx.dcx().emit_fatal(CantEmitMIR { error });
}
}

Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend))
});

Expand Down Expand Up @@ -407,7 +408,7 @@ fn dump_feature_usage_metrics(tcxt: TyCtxt<'_>, metrics_dir: &Path) {
}
}

// Extract output directory and file from matches.
/// Extract output directory and file from matches.
fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<OutFileName>) {
let odir = matches.opt_str("out-dir").map(|o| PathBuf::from(&o));
let ofile = matches.opt_str("o").map(|o| match o.as_str() {
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_driver_impl/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ use std::error::Error;

use rustc_macros::{Diagnostic, Subdiagnostic};

#[derive(Diagnostic)]
#[diag(driver_impl_cant_emit_mir)]
pub struct CantEmitMIR {
pub error: std::io::Error,
}

#[derive(Diagnostic)]
#[diag(driver_impl_rlink_unable_to_read)]
pub(crate) struct RlinkUnableToRead {
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_interface/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ interface_abi_required_feature =
.note = this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
interface_abi_required_feature_issue = for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>

interface_cant_emit_mir =
could not emit MIR: {$error}

interface_crate_name_does_not_match = `--crate-name` and `#[crate_name]` are required to match, but `{$crate_name}` != `{$attr_crate_name}`

interface_crate_name_invalid = crate names cannot start with a `-`, but `{$crate_name}` has a leading hyphen
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_interface/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ pub struct TempsDirError;
#[diag(interface_out_dir_error)]
pub struct OutDirError;

#[derive(Diagnostic)]
#[diag(interface_cant_emit_mir)]
pub struct CantEmitMIR {
pub error: io::Error,
}

#[derive(Diagnostic)]
#[diag(interface_rustc_error_fatal)]
pub struct RustcErrorFatal {
Expand Down
24 changes: 9 additions & 15 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,16 +1078,22 @@ pub(crate) fn start_codegen<'tcx>(
codegen_backend: &dyn CodegenBackend,
tcx: TyCtxt<'tcx>,
) -> Box<dyn Any> {
// Hook for UI tests.
check_for_rustc_errors_attr(tcx);

// Don't run this test assertions when not doing codegen. Compiletest tries to build
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment feels misplaced 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or at least I'm not seeing its point. Are the ui tests relying on //@ build-fail?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the attributes in report_symbol_names emit errors so //@ build-fail is necessary, which requires a check build to pass, thus necessitating this pass not to run in check mode.

// build-fail tests in check mode first and expects it to not give an error in that case.
if tcx.sess.opts.output_types.should_codegen() {
rustc_symbol_mangling::test::report_symbol_names(tcx);
}

// Don't do code generation if there were any errors. Likewise if
// there were any delayed bugs, because codegen will likely cause
// more ICEs, obscuring the original problem.
if let Some(guar) = tcx.sess.dcx().has_errors_or_delayed_bugs() {
guar.raise_fatal();
}

// Hook for UI tests.
check_for_rustc_errors_attr(tcx);

info!("Pre-codegen\n{:?}", tcx.debug_stats());

let (metadata, need_metadata_module) = rustc_metadata::fs::encode_and_write_metadata(tcx);
Expand All @@ -1096,20 +1102,8 @@ pub(crate) fn start_codegen<'tcx>(
codegen_backend.codegen_crate(tcx, metadata, need_metadata_module)
});

// Don't run this test assertions when not doing codegen. Compiletest tries to build
// build-fail tests in check mode first and expects it to not give an error in that case.
if tcx.sess.opts.output_types.should_codegen() {
rustc_symbol_mangling::test::report_symbol_names(tcx);
}

info!("Post-codegen\n{:?}", tcx.debug_stats());

if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
if let Err(error) = rustc_mir_transform::dump_mir::emit_mir(tcx) {
tcx.dcx().emit_fatal(errors::CantEmitMIR { error });
}
}

// This must run after monomorphization so that all generic types
// have been instantiated.
if tcx.sess.opts.unstable_opts.print_type_sizes {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -811,8 +811,8 @@ passes_unused_duplicate =
passes_unused_empty_lints_note =
attribute `{$name}` with an empty list has no effect

passes_unused_linker_warnings_note =
the `linker_warnings` lint can only be controlled at the root of a crate that needs to be linked
passes_unused_linker_messages_note =
the `linker_messages` lint can only be controlled at the root of a crate that needs to be linked

passes_unused_multiple =
multiple `{$name}` attributes
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2388,7 +2388,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
.iter()
.all(|kind| matches!(kind, CrateType::Rlib | CrateType::Staticlib));
if never_needs_link {
errors::UnusedNote::LinkerWarningsBinaryCrateOnly
errors::UnusedNote::LinkerMessagesBinaryCrateOnly
} else {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,8 @@ pub(crate) enum UnusedNote {
NoLints { name: Symbol },
#[note(passes_unused_default_method_body_const_note)]
DefaultMethodBodyConst,
#[note(passes_unused_linker_warnings_note)]
LinkerWarningsBinaryCrateOnly,
#[note(passes_unused_linker_messages_note)]
LinkerMessagesBinaryCrateOnly,
}

#[derive(LintDiagnostic)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/linker-warning.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ warning: unused attribute
LL | #![allow(linker_messages)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
|
= note: the `linker_warnings` lint can only be controlled at the root of a crate that needs to be linked
= note: the `linker_messages` lint can only be controlled at the root of a crate that needs to be linked

warning: 2 warnings emitted

Loading