@@ -31,7 +31,9 @@ use super::command::Command;
3131use super :: linker:: { self , Linker } ;
3232use super :: metadata:: { create_rmeta_file, MetadataPosition } ;
3333use super :: rpath:: { self , RPathConfig } ;
34- use crate :: { looks_like_rust_object_file, CodegenResults , CompiledModule , CrateInfo , NativeLib } ;
34+ use crate :: {
35+ errors, looks_like_rust_object_file, CodegenResults , CompiledModule , CrateInfo , NativeLib ,
36+ } ;
3537
3638use cc:: windows_registry;
3739use regex:: Regex ;
@@ -93,7 +95,7 @@ pub fn link_binary<'a>(
9395 let tmpdir = TempFileBuilder :: new ( )
9496 . prefix ( "rustc" )
9597 . tempdir ( )
96- . unwrap_or_else ( |err | sess. fatal ( & format ! ( "couldn't create a temp dir: {}" , err ) ) ) ;
98+ . unwrap_or_else ( |error | sess. emit_fatal ( errors :: CreateTempDir { error } ) ) ;
9799 let path = MaybeTempDir :: new ( tmpdir, sess. opts . cg . save_temps ) ;
98100 let out_filename = out_filename (
99101 sess,
@@ -208,7 +210,7 @@ pub fn link_binary<'a>(
208210pub fn each_linked_rlib (
209211 info : & CrateInfo ,
210212 f : & mut dyn FnMut ( CrateNum , & Path ) ,
211- ) -> Result < ( ) , String > {
213+ ) -> Result < ( ) , errors :: LinkRlibError > {
212214 let crates = info. used_crates . iter ( ) ;
213215 let mut fmts = None ;
214216 for ( ty, list) in info. dependency_formats . iter ( ) {
@@ -224,26 +226,23 @@ pub fn each_linked_rlib(
224226 }
225227 }
226228 let Some ( fmts) = fmts else {
227- return Err ( "could not find formats for rlibs" . to_string ( ) ) ;
229+ return Err ( errors :: LinkRlibError :: MissingFormat ) ;
228230 } ;
229231 for & cnum in crates {
230232 match fmts. get ( cnum. as_usize ( ) - 1 ) {
231233 Some ( & Linkage :: NotLinked | & Linkage :: IncludedFromDylib ) => continue ,
232234 Some ( _) => { }
233- None => return Err ( "could not find formats for rlibs" . to_string ( ) ) ,
235+ None => return Err ( errors :: LinkRlibError :: MissingFormat ) ,
234236 }
235- let name = info. crate_name [ & cnum] ;
237+ let crate_name = info. crate_name [ & cnum] ;
236238 let used_crate_source = & info. used_crate_source [ & cnum] ;
237239 if let Some ( ( path, _) ) = & used_crate_source. rlib {
238240 f ( cnum, & path) ;
239241 } else {
240242 if used_crate_source. rmeta . is_some ( ) {
241- return Err ( format ! (
242- "could not find rlib for: `{}`, found rmeta (metadata) file" ,
243- name
244- ) ) ;
243+ return Err ( errors:: LinkRlibError :: OnlyRmetaFound { crate_name } ) ;
245244 } else {
246- return Err ( format ! ( "could not find rlib for: `{}`" , name ) ) ;
245+ return Err ( errors :: LinkRlibError :: NotFound { crate_name } ) ;
247246 }
248247 }
249248 }
@@ -340,10 +339,7 @@ fn link_rlib<'a>(
340339 // -whole-archive and it isn't clear how we can currently handle such a
341340 // situation correctly.
342341 // See https://github.com/rust-lang/rust/issues/88085#issuecomment-901050897
343- sess. err (
344- "the linking modifiers `+bundle` and `+whole-archive` are not compatible \
345- with each other when generating rlibs",
346- ) ;
342+ sess. emit_err ( errors:: IncompatibleLinkingModifiers ) ;
347343 }
348344 NativeLibKind :: Static { bundle : None | Some ( true ) , .. } => { }
349345 NativeLibKind :: Static { bundle : Some ( false ) , .. }
@@ -365,12 +361,8 @@ fn link_rlib<'a>(
365361 ) ) ;
366362 continue ;
367363 }
368- ab. add_archive ( & location, Box :: new ( |_| false ) ) . unwrap_or_else ( |e| {
369- sess. fatal ( & format ! (
370- "failed to add native library {}: {}" ,
371- location. to_string_lossy( ) ,
372- e
373- ) ) ;
364+ ab. add_archive ( & location, Box :: new ( |_| false ) ) . unwrap_or_else ( |error| {
365+ sess. emit_fatal ( errors:: AddNativeLibrary { library_path : location, error } ) ;
374366 } ) ;
375367 }
376368 }
@@ -385,8 +377,8 @@ fn link_rlib<'a>(
385377 tmpdir. as_ref ( ) ,
386378 ) ;
387379
388- ab. add_archive ( & output_path, Box :: new ( |_| false ) ) . unwrap_or_else ( |e | {
389- sess. fatal ( & format ! ( "failed to add native library {}: {}" , output_path. display ( ) , e ) ) ;
380+ ab. add_archive ( & output_path, Box :: new ( |_| false ) ) . unwrap_or_else ( |error | {
381+ sess. emit_fatal ( errors :: AddNativeLibrary { library_path : output_path, error } ) ;
390382 } ) ;
391383 }
392384
@@ -451,14 +443,11 @@ fn collate_raw_dylibs(
451443 // FIXME: when we add support for ordinals, figure out if we need to do anything
452444 // if we have two DllImport values with the same name but different ordinals.
453445 if import. calling_convention != old_import. calling_convention {
454- sess. span_err (
455- import. span ,
456- & format ! (
457- "multiple declarations of external function `{}` from \
458- library `{}` have different calling conventions",
459- import. name, name,
460- ) ,
461- ) ;
446+ sess. emit_err ( errors:: MultipleExternalFuncDecl {
447+ span : import. span ,
448+ function : import. name ,
449+ library_name : & name,
450+ } ) ;
462451 }
463452 }
464453 }
@@ -560,7 +549,7 @@ fn link_staticlib<'a>(
560549 all_native_libs. extend ( codegen_results. crate_info . native_libraries [ & cnum] . iter ( ) . cloned ( ) ) ;
561550 } ) ;
562551 if let Err ( e) = res {
563- sess. fatal ( & e) ;
552+ sess. emit_fatal ( e) ;
564553 }
565554
566555 ab. build ( out_filename) ;
@@ -673,9 +662,7 @@ fn link_dwarf_object<'a>(
673662 } ) {
674663 Ok ( ( ) ) => { }
675664 Err ( e) => {
676- sess. struct_err ( "linking dwarf objects with thorin failed" )
677- . note ( & format ! ( "{:?}" , e) )
678- . emit ( ) ;
665+ sess. emit_err ( errors:: ThorinErrorWrapper ( e) ) ;
679666 sess. abort_if_errors ( ) ;
680667 }
681668 }
@@ -879,23 +866,14 @@ fn link_natively<'a>(
879866 let mut output = prog. stderr . clone ( ) ;
880867 output. extend_from_slice ( & prog. stdout ) ;
881868 let escaped_output = escape_string ( & output) ;
882- let mut err = sess. struct_err ( & format ! (
883- "linking with `{}` failed: {}" ,
884- linker_path. display( ) ,
885- prog. status
886- ) ) ;
887- err. note ( & format ! ( "{:?}" , & cmd) ) . note ( & escaped_output) ;
888- if escaped_output. contains ( "undefined reference to" ) {
889- err. help (
890- "some `extern` functions couldn't be found; some native libraries may \
891- need to be installed or have their path specified",
892- ) ;
893- err. note ( "use the `-l` flag to specify native libraries to link" ) ;
894- err. note ( "use the `cargo:rustc-link-lib` directive to specify the native \
895- libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)") ;
896- }
897- err. emit ( ) ;
898-
869+ // FIXME: Add UI tests for this error.
870+ let err = errors:: LinkingFailed {
871+ linker_path : & linker_path,
872+ exit_status : prog. status ,
873+ command : & cmd,
874+ escaped_output : & escaped_output,
875+ } ;
876+ sess. diagnostic ( ) . emit_err ( err) ;
899877 // If MSVC's `link.exe` was expected but the return code
900878 // is not a Microsoft LNK error then suggest a way to fix or
901879 // install the Visual Studio build tools.
0 commit comments