Skip to content

Commit f00c634

Browse files
committed
Allow using upstream generics in a dylib crate type
... just don't export them!
1 parent 50c57d8 commit f00c634

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/librustc_codegen_ssa/back/linker.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc::middle::dependency_format::Linkage;
1414
use rustc::session::Session;
1515
use rustc::session::config::{self, CrateType, OptLevel, DebugInfo,
1616
LinkerPluginLto, Lto};
17+
use rustc::middle::exported_symbols::ExportedSymbol;
1718
use rustc::ty::TyCtxt;
1819
use rustc_target::spec::{LinkerFlavor, LldFlavor};
1920
use rustc_serialize::{json, Encoder};
@@ -1107,9 +1108,26 @@ fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
11071108
if *dep_format == Linkage::Static {
11081109
// ... we add its symbol list to our export list.
11091110
for &(symbol, level) in tcx.exported_symbols(cnum).iter() {
1110-
if level.is_below_threshold(export_threshold) {
1111-
symbols.push(symbol.symbol_name(tcx).to_string());
1111+
if !level.is_below_threshold(export_threshold) {
1112+
continue;
11121113
}
1114+
1115+
// Do not export generic symbols from upstream crates in linked
1116+
// artifact (notably the `dylib` crate type). The main reason
1117+
// for this is that `symbol_name` is actually wrong for generic
1118+
// symbols because it guesses the name we'd give them locally
1119+
// rather than the name it has upstream (depending on
1120+
// `share_generics` settings and such).
1121+
//
1122+
// To fix that issue we just say that linked artifacts, aka
1123+
// `dylib`s, never export generic symbols and they aren't
1124+
// available to downstream crates. (the not available part is
1125+
// handled elsewhere).
1126+
if let ExportedSymbol::Generic(..) = symbol {
1127+
continue;
1128+
}
1129+
1130+
symbols.push(symbol.symbol_name(tcx).to_string());
11131131
}
11141132
}
11151133
}

src/librustc_metadata/cstore_impl.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc::middle::cstore::{CrateStore, DepKind,
1111
use rustc::middle::exported_symbols::ExportedSymbol;
1212
use rustc::middle::stability::DeprecationEntry;
1313
use rustc::middle::dependency_format::Linkage;
14-
use rustc::session::config::CrateType;
1514
use rustc::hir::def;
1615
use rustc::hir;
1716
use rustc::session::{CrateDisambiguator, Session};
@@ -246,13 +245,13 @@ provide! { <'tcx> tcx, def_id, other, cdata,
246245

247246
// When linked into a dylib crates don't export their generic symbols,
248247
// so if that's happening then we can't load upstream monomorphizations
249-
// from this crate. As a result, if we're creating a dylib or this crate
250-
// is being included from a different dynamic library, then we filter
251-
// out all `Generic` symbols here.
248+
// from this crate.
252249
let formats = tcx.dependency_formats(LOCAL_CRATE);
253-
let remove_generics = formats.iter().any(|(ty, list)| {
254-
*ty == CrateType::Dylib ||
255-
list.get(def_id.krate.as_usize() - 1) == Some(&Linkage::IncludedFromDylib)
250+
let remove_generics = formats.iter().any(|(_ty, list)| {
251+
match list.get(def_id.krate.as_usize() - 1) {
252+
Some(Linkage::IncludedFromDylib) | Some(Linkage::Dynamic) => true,
253+
_ => false,
254+
}
256255
});
257256
if remove_generics {
258257
syms.retain(|(sym, _threshold)| {

0 commit comments

Comments
 (0)