diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 323538969d73b..62c389ec79150 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -68,6 +68,23 @@ pub fn ensure_removed(dcx: DiagCtxtHandle<'_>, path: &Path) { } } +fn check_link_info_print_request(sess: &Session, crate_types: &[CrateType]) { + let print_native_static_libs = + sess.opts.prints.iter().any(|p| p.kind == PrintKind::NativeStaticLibs); + let has_staticlib = crate_types.iter().any(|ct| *ct == CrateType::Staticlib); + if print_native_static_libs { + if !has_staticlib { + sess.dcx() + .warn(format!("cannot output linkage information without staticlib crate-type")); + sess.dcx() + .note(format!("consider `--crate-type staticlib` to print linkage information")); + } else if !sess.opts.output_types.should_link() { + sess.dcx() + .warn(format!("cannot output linkage information when --emit link is not passed")); + } + } +} + /// Performs the linkage portion of the compilation phase. This will generate all /// of the requested outputs for this compilation session. pub fn link_binary( @@ -180,6 +197,8 @@ pub fn link_binary( } } + check_link_info_print_request(sess, &codegen_results.crate_info.crate_types); + // Remove the temporary object file and metadata if we aren't saving temps. sess.time("link_binary_remove_temps", || { // If the user requests that temporaries are saved, don't delete any. diff --git a/tests/ui/print-request/emit-warning-print-link-info-without-staticlib.rs b/tests/ui/print-request/emit-warning-print-link-info-without-staticlib.rs new file mode 100644 index 0000000000000..b100c062bba4c --- /dev/null +++ b/tests/ui/print-request/emit-warning-print-link-info-without-staticlib.rs @@ -0,0 +1,5 @@ +//@ compile-flags: --print native-static-libs +//@ check-pass +//~? WARN cannot output linkage information without staticlib crate-type + +fn main() {} diff --git a/tests/ui/print-request/emit-warning-print-link-info-without-staticlib.stderr b/tests/ui/print-request/emit-warning-print-link-info-without-staticlib.stderr new file mode 100644 index 0000000000000..ceff08baa13a4 --- /dev/null +++ b/tests/ui/print-request/emit-warning-print-link-info-without-staticlib.stderr @@ -0,0 +1,6 @@ +warning: cannot output linkage information without staticlib crate-type + +note: consider `--crate-type staticlib` to print linkage information + +warning: 1 warning emitted + diff --git a/tests/ui/print-request/emit-warning-while-exe-and-print-link-info.rs b/tests/ui/print-request/emit-warning-while-exe-and-print-link-info.rs new file mode 100644 index 0000000000000..3e9ca457a9c9b --- /dev/null +++ b/tests/ui/print-request/emit-warning-while-exe-and-print-link-info.rs @@ -0,0 +1,3 @@ +//@ compile-flags: --print native-static-libs --crate-type staticlib --emit metadata +//@ check-pass +//~? WARN cannot output linkage information when --emit link is not passed diff --git a/tests/ui/print-request/emit-warning-while-exe-and-print-link-info.stderr b/tests/ui/print-request/emit-warning-while-exe-and-print-link-info.stderr new file mode 100644 index 0000000000000..b32e1437d6b50 --- /dev/null +++ b/tests/ui/print-request/emit-warning-while-exe-and-print-link-info.stderr @@ -0,0 +1,4 @@ +warning: cannot output linkage information when --emit link is not passed + +warning: 1 warning emitted + diff --git a/tests/ui/print-request/stability.rs b/tests/ui/print-request/stability.rs index 54142ce78cefb..fbcdf916cc7cd 100644 --- a/tests/ui/print-request/stability.rs +++ b/tests/ui/print-request/stability.rs @@ -110,3 +110,4 @@ fn main() {} //[check_cfg]~? ERROR the `-Z unstable-options` flag must also be passed to enable the `check-cfg` print option //[supported_crate_types]~? ERROR the `-Z unstable-options` flag must also be passed to enable the `supported-crate-types` print option //[target_spec_json]~? ERROR the `-Z unstable-options` flag must also be passed to enable the `target-spec-json` print option +//[native_static_libs]~? WARNING cannot output linkage information without staticlib crate-type