@@ -31,7 +31,9 @@ use super::command::Command;
31
31
use super :: linker:: { self , Linker } ;
32
32
use super :: metadata:: { create_rmeta_file, MetadataPosition } ;
33
33
use 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
+ } ;
35
37
36
38
use cc:: windows_registry;
37
39
use regex:: Regex ;
@@ -93,7 +95,7 @@ pub fn link_binary<'a>(
93
95
let tmpdir = TempFileBuilder :: new ( )
94
96
. prefix ( "rustc" )
95
97
. 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 } ) ) ;
97
99
let path = MaybeTempDir :: new ( tmpdir, sess. opts . cg . save_temps ) ;
98
100
let out_filename = out_filename (
99
101
sess,
@@ -208,7 +210,7 @@ pub fn link_binary<'a>(
208
210
pub fn each_linked_rlib (
209
211
info : & CrateInfo ,
210
212
f : & mut dyn FnMut ( CrateNum , & Path ) ,
211
- ) -> Result < ( ) , String > {
213
+ ) -> Result < ( ) , errors :: LinkRlibError > {
212
214
let crates = info. used_crates . iter ( ) ;
213
215
let mut fmts = None ;
214
216
for ( ty, list) in info. dependency_formats . iter ( ) {
@@ -224,26 +226,23 @@ pub fn each_linked_rlib(
224
226
}
225
227
}
226
228
let Some ( fmts) = fmts else {
227
- return Err ( "could not find formats for rlibs" . to_string ( ) ) ;
229
+ return Err ( errors :: LinkRlibError :: MissingFormat ) ;
228
230
} ;
229
231
for & cnum in crates {
230
232
match fmts. get ( cnum. as_usize ( ) - 1 ) {
231
233
Some ( & Linkage :: NotLinked | & Linkage :: IncludedFromDylib ) => continue ,
232
234
Some ( _) => { }
233
- None => return Err ( "could not find formats for rlibs" . to_string ( ) ) ,
235
+ None => return Err ( errors :: LinkRlibError :: MissingFormat ) ,
234
236
}
235
- let name = info. crate_name [ & cnum] ;
237
+ let crate_name = info. crate_name [ & cnum] ;
236
238
let used_crate_source = & info. used_crate_source [ & cnum] ;
237
239
if let Some ( ( path, _) ) = & used_crate_source. rlib {
238
240
f ( cnum, & path) ;
239
241
} else {
240
242
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 } ) ;
245
244
} else {
246
- return Err ( format ! ( "could not find rlib for: `{}`" , name ) ) ;
245
+ return Err ( errors :: LinkRlibError :: NotFound { crate_name } ) ;
247
246
}
248
247
}
249
248
}
@@ -340,10 +339,7 @@ fn link_rlib<'a>(
340
339
// -whole-archive and it isn't clear how we can currently handle such a
341
340
// situation correctly.
342
341
// 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 ) ;
347
343
}
348
344
NativeLibKind :: Static { bundle : None | Some ( true ) , .. } => { }
349
345
NativeLibKind :: Static { bundle : Some ( false ) , .. }
@@ -365,12 +361,8 @@ fn link_rlib<'a>(
365
361
) ) ;
366
362
continue ;
367
363
}
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 } ) ;
374
366
} ) ;
375
367
}
376
368
}
@@ -385,8 +377,8 @@ fn link_rlib<'a>(
385
377
tmpdir. as_ref ( ) ,
386
378
) ;
387
379
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 } ) ;
390
382
} ) ;
391
383
}
392
384
@@ -451,14 +443,11 @@ fn collate_raw_dylibs(
451
443
// FIXME: when we add support for ordinals, figure out if we need to do anything
452
444
// if we have two DllImport values with the same name but different ordinals.
453
445
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
+ } ) ;
462
451
}
463
452
}
464
453
}
@@ -560,7 +549,7 @@ fn link_staticlib<'a>(
560
549
all_native_libs. extend ( codegen_results. crate_info . native_libraries [ & cnum] . iter ( ) . cloned ( ) ) ;
561
550
} ) ;
562
551
if let Err ( e) = res {
563
- sess. fatal ( & e) ;
552
+ sess. emit_fatal ( e) ;
564
553
}
565
554
566
555
ab. build ( out_filename) ;
@@ -673,9 +662,7 @@ fn link_dwarf_object<'a>(
673
662
} ) {
674
663
Ok ( ( ) ) => { }
675
664
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) ) ;
679
666
sess. abort_if_errors ( ) ;
680
667
}
681
668
}
@@ -879,23 +866,14 @@ fn link_natively<'a>(
879
866
let mut output = prog. stderr . clone ( ) ;
880
867
output. extend_from_slice ( & prog. stdout ) ;
881
868
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) ;
899
877
// If MSVC's `link.exe` was expected but the return code
900
878
// is not a Microsoft LNK error then suggest a way to fix or
901
879
// install the Visual Studio build tools.
0 commit comments