From 38518c44b0325398ba07c2e23e502a0c2f5d1ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Mon, 11 Mar 2024 17:02:12 +0100 Subject: [PATCH] Print the crates not available as static --- compiler/rustc_metadata/messages.ftl | 3 ++ .../rustc_metadata/src/dependency_format.rs | 29 ++++++++++++++----- compiler/rustc_metadata/src/errors.rs | 8 +++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_metadata/messages.ftl b/compiler/rustc_metadata/messages.ftl index a1c6fba4d43..2f5dfad265c 100644 --- a/compiler/rustc_metadata/messages.ftl +++ b/compiler/rustc_metadata/messages.ftl @@ -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} diff --git a/compiler/rustc_metadata/src/dependency_format.rs b/compiler/rustc_metadata/src/dependency_format.rs index b80a9e9612a..4d1bd455412 100644 --- a/compiler/rustc_metadata/src/dependency_format.rs +++ b/compiler/rustc_metadata/src/dependency_format.rs @@ -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; @@ -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; } @@ -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); } } } @@ -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; } } @@ -252,6 +254,7 @@ fn add_library( cnum: CrateNum, link: LinkagePreference, m: &mut FxHashMap, + unavailable_as_static: &mut Vec, ) { match m.get(&cnum) { Some(&link2) => { @@ -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 => { @@ -272,7 +281,7 @@ fn add_library( } } -fn attempt_static(tcx: TyCtxt<'_>) -> Option { +fn attempt_static(tcx: TyCtxt<'_>, unavailable: &mut Vec) -> Option { let all_crates_available_as_rlib = tcx .crates(()) .iter() @@ -281,7 +290,11 @@ fn attempt_static(tcx: TyCtxt<'_>) -> Option { 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 { diff --git a/compiler/rustc_metadata/src/errors.rs b/compiler/rustc_metadata/src/errors.rs index 8bf6b665de8..b50ae057709 100644 --- a/compiler/rustc_metadata/src/errors.rs +++ b/compiler/rustc_metadata/src/errors.rs @@ -38,6 +38,14 @@ pub struct RustcLibRequired<'a> { #[help] pub struct CrateDepMultiple { pub crate_name: Symbol, + #[subdiagnostic] + pub non_static_deps: Vec, +} + +#[derive(Subdiagnostic)] +#[note(metadata_crate_dep_not_static)] +pub struct NonStaticCrateDep { + pub crate_name: Symbol, } #[derive(Diagnostic)]