Skip to content

Commit 41f1ed1

Browse files
committed
Move some calls to before calling codegen_crate
`--emit mir`, `#[rustc_symbol_name]` and `#[rustc_def_path]` now run before codegen and thus work even if codegen fails. This can help with debugging.
1 parent 7d3965e commit 41f1ed1

File tree

6 files changed

+24
-25
lines changed

6 files changed

+24
-25
lines changed

compiler/rustc_driver_impl/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
driver_impl_cant_emit_mir = could not emit MIR: {$error}
2+
13
driver_impl_ice = the compiler unexpectedly panicked. this is a bug.
24
driver_impl_ice_bug_report = we would appreciate a bug report: {$bug_report_url}
35
driver_impl_ice_bug_report_internal_feature = using internal features is not supported and expected to cause internal compiler errors when used incorrectly

compiler/rustc_driver_impl/src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ mod signal_handler {
108108
}
109109

110110
use crate::session_diagnostics::{
111-
RLinkEmptyVersionNumber, RLinkEncodingVersionMismatch, RLinkRustcVersionMismatch,
111+
CantEmitMIR, RLinkEmptyVersionNumber, RLinkEncodingVersionMismatch, RLinkRustcVersionMismatch,
112112
RLinkWrongFileType, RlinkCorruptFile, RlinkNotAFile, RlinkUnableToRead, UnstableFeatureUsage,
113113
};
114114

@@ -374,6 +374,12 @@ pub fn run_compiler(at_args: &[String], callbacks: &mut (dyn Callbacks + Send))
374374
return early_exit();
375375
}
376376

377+
if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
378+
if let Err(error) = rustc_mir_transform::dump_mir::emit_mir(tcx) {
379+
tcx.dcx().emit_fatal(CantEmitMIR { error });
380+
}
381+
}
382+
377383
Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend))
378384
});
379385

compiler/rustc_driver_impl/src/session_diagnostics.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ use std::error::Error;
22

33
use rustc_macros::{Diagnostic, Subdiagnostic};
44

5+
#[derive(Diagnostic)]
6+
#[diag(driver_impl_cant_emit_mir)]
7+
pub struct CantEmitMIR {
8+
pub error: std::io::Error,
9+
}
10+
511
#[derive(Diagnostic)]
612
#[diag(driver_impl_rlink_unable_to_read)]
713
pub(crate) struct RlinkUnableToRead {

compiler/rustc_interface/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ interface_abi_required_feature =
33
.note = this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
44
interface_abi_required_feature_issue = for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
55
6-
interface_cant_emit_mir =
7-
could not emit MIR: {$error}
8-
96
interface_crate_name_does_not_match = `--crate-name` and `#[crate_name]` are required to match, but `{$crate_name}` != `{$attr_crate_name}`
107
118
interface_crate_name_invalid = crate names cannot start with a `-`, but `{$crate_name}` has a leading hyphen

compiler/rustc_interface/src/errors.rs

-6
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@ pub struct TempsDirError;
7373
#[diag(interface_out_dir_error)]
7474
pub struct OutDirError;
7575

76-
#[derive(Diagnostic)]
77-
#[diag(interface_cant_emit_mir)]
78-
pub struct CantEmitMIR {
79-
pub error: io::Error,
80-
}
81-
8276
#[derive(Diagnostic)]
8377
#[diag(interface_rustc_error_fatal)]
8478
pub struct RustcErrorFatal {

compiler/rustc_interface/src/passes.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -1078,16 +1078,22 @@ pub(crate) fn start_codegen<'tcx>(
10781078
codegen_backend: &dyn CodegenBackend,
10791079
tcx: TyCtxt<'tcx>,
10801080
) -> Box<dyn Any> {
1081+
// Hook for UI tests.
1082+
check_for_rustc_errors_attr(tcx);
1083+
1084+
// Don't run this test assertions when not doing codegen. Compiletest tries to build
1085+
// build-fail tests in check mode first and expects it to not give an error in that case.
1086+
if tcx.sess.opts.output_types.should_codegen() {
1087+
rustc_symbol_mangling::test::report_symbol_names(tcx);
1088+
}
1089+
10811090
// Don't do code generation if there were any errors. Likewise if
10821091
// there were any delayed bugs, because codegen will likely cause
10831092
// more ICEs, obscuring the original problem.
10841093
if let Some(guar) = tcx.sess.dcx().has_errors_or_delayed_bugs() {
10851094
guar.raise_fatal();
10861095
}
10871096

1088-
// Hook for UI tests.
1089-
check_for_rustc_errors_attr(tcx);
1090-
10911097
info!("Pre-codegen\n{:?}", tcx.debug_stats());
10921098

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

1099-
// Don't run this test assertions when not doing codegen. Compiletest tries to build
1100-
// build-fail tests in check mode first and expects it to not give an error in that case.
1101-
if tcx.sess.opts.output_types.should_codegen() {
1102-
rustc_symbol_mangling::test::report_symbol_names(tcx);
1103-
}
1104-
11051105
info!("Post-codegen\n{:?}", tcx.debug_stats());
11061106

1107-
if tcx.sess.opts.output_types.contains_key(&OutputType::Mir) {
1108-
if let Err(error) = rustc_mir_transform::dump_mir::emit_mir(tcx) {
1109-
tcx.dcx().emit_fatal(errors::CantEmitMIR { error });
1110-
}
1111-
}
1112-
11131107
// This must run after monomorphization so that all generic types
11141108
// have been instantiated.
11151109
if tcx.sess.opts.unstable_opts.print_type_sizes {

0 commit comments

Comments
 (0)