Skip to content

Commit

Permalink
Auto merge of #122359 - Zoxc:missing-static-notes, r=wesleywiser
Browse files Browse the repository at this point in the history
Print the crates not available as static

This prints out the crates not available to be statically linked when static linking is preferred and we run into an error with duplicated crates.
  • Loading branch information
bors committed Mar 20, 2024
2 parents a128516 + 38518c4 commit 94b72d6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_metadata/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ metadata_crate_dep_multiple =
cannot satisfy dependencies so `{$crate_name}` only shows up once
.help = having upstream crates all available in one format will likely make this go away
metadata_crate_dep_not_static =
`{$crate_name}` was unavailable as a static crate, preventing fully static linking
metadata_crate_location_unknown_type =
extern location for {$crate_name} is of an unknown type: {$path}
Expand Down
29 changes: 21 additions & 8 deletions compiler/rustc_metadata/src/dependency_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
use crate::creader::CStore;
use crate::errors::{
BadPanicStrategy, CrateDepMultiple, IncompatiblePanicInDropStrategy, LibRequired,
RequiredPanicStrategy, RlibRequired, RustcLibRequired, TwoPanicRuntimes,
NonStaticCrateDep, RequiredPanicStrategy, RlibRequired, RustcLibRequired, TwoPanicRuntimes,
};

use rustc_data_structures::fx::FxHashMap;
Expand Down Expand Up @@ -123,13 +123,15 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
CrateType::Rlib => Linkage::NotLinked,
};

let mut unavailable_as_static = Vec::new();

match preferred_linkage {
// If the crate is not linked, there are no link-time dependencies.
Linkage::NotLinked => return Vec::new(),
Linkage::Static => {
// Attempt static linkage first. For dylibs and executables, we may be
// able to retry below with dynamic linkage.
if let Some(v) = attempt_static(tcx) {
if let Some(v) = attempt_static(tcx, &mut unavailable_as_static) {
return v;
}

Expand Down Expand Up @@ -169,11 +171,11 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
let src = tcx.used_crate_source(cnum);
if src.dylib.is_some() {
info!("adding dylib: {}", name);
add_library(tcx, cnum, RequireDynamic, &mut formats);
add_library(tcx, cnum, RequireDynamic, &mut formats, &mut unavailable_as_static);
let deps = tcx.dylib_dependency_formats(cnum);
for &(depnum, style) in deps.iter() {
info!("adding {:?}: {}", style, tcx.crate_name(depnum));
add_library(tcx, depnum, style, &mut formats);
add_library(tcx, depnum, style, &mut formats, &mut unavailable_as_static);
}
}
}
Expand Down Expand Up @@ -201,7 +203,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
{
assert!(src.rlib.is_some() || src.rmeta.is_some());
info!("adding staticlib: {}", tcx.crate_name(cnum));
add_library(tcx, cnum, RequireStatic, &mut formats);
add_library(tcx, cnum, RequireStatic, &mut formats, &mut unavailable_as_static);
ret[cnum.as_usize() - 1] = Linkage::Static;
}
}
Expand Down Expand Up @@ -252,6 +254,7 @@ fn add_library(
cnum: CrateNum,
link: LinkagePreference,
m: &mut FxHashMap<CrateNum, LinkagePreference>,
unavailable_as_static: &mut Vec<CrateNum>,
) {
match m.get(&cnum) {
Some(&link2) => {
Expand All @@ -263,7 +266,13 @@ fn add_library(
// This error is probably a little obscure, but I imagine that it
// can be refined over time.
if link2 != link || link == RequireStatic {
tcx.dcx().emit_err(CrateDepMultiple { crate_name: tcx.crate_name(cnum) });
tcx.dcx().emit_err(CrateDepMultiple {
crate_name: tcx.crate_name(cnum),
non_static_deps: unavailable_as_static
.drain(..)
.map(|cnum| NonStaticCrateDep { crate_name: tcx.crate_name(cnum) })
.collect(),
});
}
}
None => {
Expand All @@ -272,7 +281,7 @@ fn add_library(
}
}

fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {
fn attempt_static(tcx: TyCtxt<'_>, unavailable: &mut Vec<CrateNum>) -> Option<DependencyList> {
let all_crates_available_as_rlib = tcx
.crates(())
.iter()
Expand All @@ -281,7 +290,11 @@ fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {
if tcx.dep_kind(cnum).macros_only() {
return None;
}
Some(tcx.used_crate_source(cnum).rlib.is_some())
let is_rlib = tcx.used_crate_source(cnum).rlib.is_some();
if !is_rlib {
unavailable.push(cnum);
}
Some(is_rlib)
})
.all(|is_rlib| is_rlib);
if !all_crates_available_as_rlib {
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_metadata/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ pub struct RustcLibRequired<'a> {
#[help]
pub struct CrateDepMultiple {
pub crate_name: Symbol,
#[subdiagnostic]
pub non_static_deps: Vec<NonStaticCrateDep>,
}

#[derive(Subdiagnostic)]
#[note(metadata_crate_dep_not_static)]
pub struct NonStaticCrateDep {
pub crate_name: Symbol,
}

#[derive(Diagnostic)]
Expand Down

0 comments on commit 94b72d6

Please sign in to comment.