Skip to content

Commit 4c80f50

Browse files
committed
UPDATE - Complete link.rs migration to new diagnostics infraestructure
1 parent 6718ea1 commit 4c80f50

File tree

4 files changed

+227
-81
lines changed

4 files changed

+227
-81
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 43 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -919,29 +919,17 @@ fn link_natively<'a>(
919919
)
920920
.is_some();
921921

922-
sess.note_without_error("`link.exe` returned an unexpected error");
922+
sess.emit_note(errors::LinkExeUnexpectedError);
923923
if is_vs_installed && has_linker {
924924
// the linker is broken
925-
sess.note_without_error(
926-
"the Visual Studio build tools may need to be repaired \
927-
using the Visual Studio installer",
928-
);
929-
sess.note_without_error(
930-
"or a necessary component may be missing from the \
931-
\"C++ build tools\" workload",
932-
);
925+
sess.emit_note(errors::RepairVSBuildTools);
926+
sess.emit_note(errors::MissingCppBuildToolComponent);
933927
} else if is_vs_installed {
934928
// the linker is not installed
935-
sess.note_without_error(
936-
"in the Visual Studio installer, ensure the \
937-
\"C++ build tools\" workload is selected",
938-
);
929+
sess.emit_note(errors::SelectCppBuildToolWorkload);
939930
} else {
940931
// visual studio is not installed
941-
sess.note_without_error(
942-
"you may need to install Visual Studio build tools with the \
943-
\"C++ build tools\" workload",
944-
);
932+
sess.emit_note(errors::VisualStudioNotInstalled);
945933
}
946934
}
947935
}
@@ -954,35 +942,20 @@ fn link_natively<'a>(
954942
Err(e) => {
955943
let linker_not_found = e.kind() == io::ErrorKind::NotFound;
956944

957-
let mut linker_error = {
958-
if linker_not_found {
959-
sess.struct_err(&format!("linker `{}` not found", linker_path.display()))
960-
} else {
961-
sess.struct_err(&format!(
962-
"could not exec the linker `{}`",
963-
linker_path.display()
964-
))
965-
}
966-
};
967-
968-
linker_error.note(&e.to_string());
969-
970-
if !linker_not_found {
971-
linker_error.note(&format!("{:?}", &cmd));
945+
if linker_not_found {
946+
sess.emit_err(errors::LinkerNotFound { linker_path, error: e });
947+
} else {
948+
sess.emit_err(errors::UnableToExeLinker {
949+
linker_path,
950+
error: e,
951+
command_formatted: format!("{:?}", &cmd),
952+
});
972953
}
973954

974-
linker_error.emit();
975-
976955
if sess.target.is_like_msvc && linker_not_found {
977-
sess.note_without_error(
978-
"the msvc targets depend on the msvc linker \
979-
but `link.exe` was not found",
980-
);
981-
sess.note_without_error(
982-
"please ensure that Visual Studio 2017 or later, or Build Tools \
983-
for Visual Studio were installed with the Visual C++ option.",
984-
);
985-
sess.note_without_error("VS Code is a different product, and is not sufficient.");
956+
sess.emit_note(errors::MsvcMissingLinker);
957+
sess.emit_note(errors::CheckInstalledVisualStudio);
958+
sess.emit_note(errors::UnsufficientVSCodeProduct);
986959
}
987960
sess.abort_if_errors();
988961
}
@@ -1007,15 +980,13 @@ fn link_natively<'a>(
1007980
if !prog.status.success() {
1008981
let mut output = prog.stderr.clone();
1009982
output.extend_from_slice(&prog.stdout);
1010-
sess.struct_warn(&format!(
1011-
"processing debug info with `dsymutil` failed: {}",
1012-
prog.status
1013-
))
1014-
.note(&escape_string(&output))
1015-
.emit();
983+
sess.emit_warning(errors::ProcessingDymutilFailed {
984+
status: prog.status,
985+
output: escape_string(&output),
986+
});
1016987
}
1017988
}
1018-
Err(e) => sess.fatal(&format!("unable to run `dsymutil`: {}", e)),
989+
Err(error) => sess.emit_fatal(errors::UnableToRunDsymutil { error }),
1019990
}
1020991
}
1021992

@@ -1092,21 +1063,21 @@ fn strip_symbols_with_external_utility<'a>(
10921063
if !prog.status.success() {
10931064
let mut output = prog.stderr.clone();
10941065
output.extend_from_slice(&prog.stdout);
1095-
sess.struct_warn(&format!(
1096-
"stripping debug info with `{}` failed: {}",
1097-
util, prog.status
1098-
))
1099-
.note(&escape_string(&output))
1100-
.emit();
1066+
sess.emit_warning(errors::StrippingDebuInfoFailed {
1067+
util,
1068+
status: prog.status,
1069+
output: escape_string(&output),
1070+
});
11011071
}
11021072
}
1103-
Err(e) => sess.fatal(&format!("unable to run `{}`: {}", util, e)),
1073+
Err(error) => sess.emit_fatal(errors::UnableToRun { util, error }),
11041074
}
11051075
}
11061076

11071077
fn escape_string(s: &[u8]) -> String {
11081078
match str::from_utf8(s) {
11091079
Ok(s) => s.to_owned(),
1080+
// FIXME: return a type that can conform to IntoDiagnosticArg
11101081
Err(_) => format!("Non-UTF-8 output: {}", s.escape_ascii()),
11111082
}
11121083
}
@@ -1251,7 +1222,7 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
12511222
)),
12521223
(Some(linker), None) => {
12531224
let stem = linker.file_stem().and_then(|stem| stem.to_str()).unwrap_or_else(|| {
1254-
sess.fatal("couldn't extract file stem from specified linker")
1225+
sess.emit_fatal(errors::LinkerFileStem);
12551226
});
12561227

12571228
let flavor = if stem == "emcc" {
@@ -1378,13 +1349,9 @@ fn print_native_static_libs(sess: &Session, all_native_libs: &[NativeLib]) {
13781349
})
13791350
.collect();
13801351
if !lib_args.is_empty() {
1381-
sess.note_without_error(
1382-
"Link against the following native artifacts when linking \
1383-
against this static library. The order and any duplication \
1384-
can be significant on some platforms.",
1385-
);
1352+
sess.emit_note(errors::StaticLibraryNativeArtifacts);
13861353
// Prefix for greppability
1387-
sess.note_without_error(&format!("native-static-libs: {}", &lib_args.join(" ")));
1354+
sess.emit_note(errors::NativeStaticLibs { arguments: lib_args.join(" ") });
13881355
}
13891356
}
13901357

@@ -1688,14 +1655,14 @@ fn add_link_script(cmd: &mut dyn Linker, sess: &Session, tmpdir: &Path, crate_ty
16881655
match (crate_type, &sess.target.link_script) {
16891656
(CrateType::Cdylib | CrateType::Executable, Some(script)) => {
16901657
if !sess.target.linker_flavor.is_gnu() {
1691-
sess.fatal("can only use link script when linking with GNU-like linker");
1658+
sess.emit_fatal(errors::LinkScriptUnavailable);
16921659
}
16931660

16941661
let file_name = ["rustc", &sess.target.llvm_target, "linkfile.ld"].join("-");
16951662

16961663
let path = tmpdir.join(file_name);
1697-
if let Err(e) = fs::write(&path, script.as_ref()) {
1698-
sess.fatal(&format!("failed to write link script to {}: {}", path.display(), e));
1664+
if let Err(error) = fs::write(&path, script.as_ref()) {
1665+
sess.emit_fatal(errors::LinkScriptWriteFailure { path, error });
16991666
}
17001667

17011668
cmd.arg("--script");
@@ -1841,8 +1808,8 @@ fn add_linked_symbol_object(
18411808

18421809
let path = tmpdir.join("symbols.o");
18431810
let result = std::fs::write(&path, file.write().unwrap());
1844-
if let Err(e) = result {
1845-
sess.fatal(&format!("failed to write {}: {}", path.display(), e));
1811+
if let Err(error) = result {
1812+
sess.emit_fatal(errors::FailedToWrite { path, error });
18461813
}
18471814
cmd.add_object(&path);
18481815
}
@@ -2299,14 +2266,10 @@ fn collect_natvis_visualizers(
22992266
visualizer_paths.push(visualizer_out_file);
23002267
}
23012268
Err(error) => {
2302-
sess.warn(
2303-
format!(
2304-
"Unable to write debugger visualizer file `{}`: {} ",
2305-
visualizer_out_file.display(),
2306-
error
2307-
)
2308-
.as_str(),
2309-
);
2269+
sess.emit_warning(errors::UnableToWriteDebuggerVisualizer {
2270+
path: visualizer_out_file,
2271+
error,
2272+
});
23102273
}
23112274
};
23122275
}
@@ -2641,7 +2604,7 @@ fn add_upstream_rust_crates<'a>(
26412604
|| !codegen_results.crate_info.is_no_builtins.contains(&cnum);
26422605

26432606
let mut archive = archive_builder_builder.new_archive_builder(sess);
2644-
if let Err(e) = archive.add_archive(
2607+
if let Err(error) = archive.add_archive(
26452608
cratepath,
26462609
Box::new(move |f| {
26472610
if f == METADATA_FILENAME {
@@ -2681,7 +2644,7 @@ fn add_upstream_rust_crates<'a>(
26812644
false
26822645
}),
26832646
) {
2684-
sess.fatal(&format!("failed to build archive from rlib: {}", e));
2647+
sess.emit_fatal(errors::RlibArchiveBuildFailure { error });
26852648
}
26862649
if archive.build(&dst) {
26872650
link_upstream(&dst);
@@ -2919,7 +2882,7 @@ fn add_gcc_ld_path(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
29192882
}
29202883
}
29212884
} else {
2922-
sess.fatal("option `-Z gcc-ld` is used even though linker flavor is not gcc");
2885+
sess.emit_fatal(errors::OptionGccOnly);
29232886
}
29242887
}
29252888
}

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,133 @@ impl IntoDiagnostic<'_> for LinkingFailed<'_> {
354354
diag
355355
}
356356
}
357+
358+
#[derive(Diagnostic)]
359+
#[diag(codegen_ssa_link_exe_unexpected_error)]
360+
pub struct LinkExeUnexpectedError;
361+
362+
#[derive(Diagnostic)]
363+
#[diag(codegen_ssa_repair_vs_build_tools)]
364+
pub struct RepairVSBuildTools;
365+
366+
#[derive(Diagnostic)]
367+
#[diag(codegen_ssa_missing_cpp_build_tool_component)]
368+
pub struct MissingCppBuildToolComponent;
369+
370+
#[derive(Diagnostic)]
371+
#[diag(codegen_ssa_select_cpp_build_tool_workload)]
372+
pub struct SelectCppBuildToolWorkload;
373+
374+
#[derive(Diagnostic)]
375+
#[diag(codegen_ssa_visual_studio_not_installed)]
376+
pub struct VisualStudioNotInstalled;
377+
378+
#[derive(Diagnostic)]
379+
#[diag(codegen_ssa_linker_not_found)]
380+
#[note]
381+
pub struct LinkerNotFound {
382+
pub linker_path: PathBuf,
383+
pub error: Error,
384+
}
385+
386+
#[derive(Diagnostic)]
387+
#[diag(codegen_ssa_unable_to_exe_linker)]
388+
#[note]
389+
#[note(command_note)]
390+
pub struct UnableToExeLinker {
391+
pub linker_path: PathBuf,
392+
pub error: Error,
393+
pub command_formatted: String,
394+
}
395+
396+
#[derive(Diagnostic)]
397+
#[diag(codegen_ssa_msvc_missing_linker)]
398+
pub struct MsvcMissingLinker;
399+
400+
#[derive(Diagnostic)]
401+
#[diag(codegen_ssa_check_installed_visual_studio)]
402+
pub struct CheckInstalledVisualStudio;
403+
404+
#[derive(Diagnostic)]
405+
#[diag(codegen_ssa_unsufficient_vs_code_product)]
406+
pub struct UnsufficientVSCodeProduct;
407+
408+
#[derive(Diagnostic)]
409+
#[diag(codegen_ssa_processing_dymutil_failed)]
410+
#[note]
411+
pub struct ProcessingDymutilFailed {
412+
pub status: ExitStatus,
413+
pub output: String,
414+
}
415+
416+
#[derive(Diagnostic)]
417+
#[diag(codegen_ssa_unable_to_run_dsymutil)]
418+
#[note]
419+
pub struct UnableToRunDsymutil {
420+
pub error: Error,
421+
}
422+
423+
#[derive(Diagnostic)]
424+
#[diag(codegen_ssa_stripping_debu_info_failed)]
425+
#[note]
426+
pub struct StrippingDebuInfoFailed<'a> {
427+
pub util: &'a str,
428+
pub status: ExitStatus,
429+
pub output: String,
430+
}
431+
432+
#[derive(Diagnostic)]
433+
#[diag(codegen_ssa_unable_to_run)]
434+
pub struct UnableToRun<'a> {
435+
pub util: &'a str,
436+
pub error: Error,
437+
}
438+
439+
#[derive(Diagnostic)]
440+
#[diag(codegen_ssa_linker_file_stem)]
441+
pub struct LinkerFileStem;
442+
443+
#[derive(Diagnostic)]
444+
#[diag(codegen_ssa_static_library_native_artifacts)]
445+
pub struct StaticLibraryNativeArtifacts;
446+
447+
#[derive(Diagnostic)]
448+
#[diag(codegen_ssa_native_static_libs)]
449+
pub struct NativeStaticLibs {
450+
pub arguments: String,
451+
}
452+
453+
#[derive(Diagnostic)]
454+
#[diag(codegen_ssa_link_script_unavailable)]
455+
pub struct LinkScriptUnavailable;
456+
457+
#[derive(Diagnostic)]
458+
#[diag(codegen_ssa_link_script_write_failure)]
459+
pub struct LinkScriptWriteFailure {
460+
pub path: PathBuf,
461+
pub error: Error,
462+
}
463+
464+
#[derive(Diagnostic)]
465+
#[diag(codegen_ssa_failed_to_write)]
466+
pub struct FailedToWrite {
467+
pub path: PathBuf,
468+
pub error: Error,
469+
}
470+
471+
#[derive(Diagnostic)]
472+
#[diag(codegen_ssa_unable_to_write_debugger_visualizer)]
473+
pub struct UnableToWriteDebuggerVisualizer {
474+
pub path: PathBuf,
475+
pub error: Error,
476+
}
477+
478+
#[derive(Diagnostic)]
479+
#[diag(codegen_ssa_rlib_archive_build_failure)]
480+
pub struct RlibArchiveBuildFailure {
481+
pub error: Error,
482+
}
483+
484+
#[derive(Diagnostic)]
485+
#[diag(codegen_ssa_option_gcc_only)]
486+
pub struct OptionGccOnly;

0 commit comments

Comments
 (0)