Skip to content

Emit warning while outputs is not exe and prints linkage info #138139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that you can pass multiple crate types at the same time. In that case it does emit the output for the staticlib crate type, but with this PR it would give a warning anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I changed it to check for the presence of staticlib in crate-types.

} 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(
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//@ compile-flags: --print native-static-libs
//@ check-pass
//~? WARN cannot output linkage information without staticlib crate-type

fn main() {}
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
warning: cannot output linkage information when --emit link is not passed

warning: 1 warning emitted

1 change: 1 addition & 0 deletions tests/ui/print-request/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading