@@ -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
11071077fn 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}
0 commit comments