Rollup merge of #138139 - xizheyin:issue-137384, r=ChrisDenton

Emit warning while outputs is not exe and prints linkage info

cc #137384

```bash
$ rustc +stage1 /dev/null --print native-static-libs --crate-type staticlib  --emit metadata
warning: skipping link step due to conflict: cannot output linkage information without emitting executable

note: consider emitting executable to print link information

warning: 1 warning emitted
```
This commit is contained in:
Jacob Pratt 2025-05-29 04:49:40 +02:00 committed by GitHub
commit 394fde04a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 38 additions and 0 deletions

View file

@ -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.

View file

@ -0,0 +1,5 @@
//@ compile-flags: --print native-static-libs
//@ check-pass
//~? WARN cannot output linkage information without staticlib crate-type
fn main() {}

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,4 @@
warning: cannot output linkage information when --emit link is not passed
warning: 1 warning emitted

View file

@ -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