Skip to content

Commit acbc6c5

Browse files
Allow for re-using hidden monomorphizations on platforms that don't support Rust dylibs.
1 parent edb1088 commit acbc6c5

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/librustc/ty/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
15191519

15201520
#[inline]
15211521
pub fn local_crate_exports_generics(self) -> bool {
1522+
debug_assert!(self.share_generics());
1523+
15221524
self.sess.crate_types.borrow().iter().any(|crate_type| {
15231525
match crate_type {
15241526
CrateTypeExecutable |

src/librustc_trans/back/symbol_export.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,37 @@ fn exported_symbols_provider_local<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
247247
use rustc::mir::mono::{Linkage, Visibility, MonoItem};
248248
use rustc::ty::InstanceDef;
249249

250+
// Normally, we require that shared monomorphizations are not hidden,
251+
// because if we want to re-use a monomorphization from a Rust dylib, it
252+
// needs to be exported.
253+
// However, on platforms that don't allow for Rust dylibs, having
254+
// external linkage is enough for monomorphization to be linked to.
255+
let need_visibility = tcx.sess.target.target.options.dynamic_linking &&
256+
!tcx.sess.target.target.options.only_cdylib;
257+
250258
let (_, cgus) = tcx.collect_and_partition_translation_items(LOCAL_CRATE);
251259

252260
for (mono_item, &(linkage, visibility)) in cgus.iter()
253261
.flat_map(|cgu| cgu.items().iter()) {
254-
if linkage == Linkage::External && visibility == Visibility::Default {
255-
if let &MonoItem::Fn(Instance {
256-
def: InstanceDef::Item(def_id),
257-
substs,
258-
}) = mono_item {
259-
if substs.types().next().is_some() {
260-
symbols.push((ExportedSymbol::Generic(def_id, substs),
261-
SymbolExportLevel::Rust));
262-
}
262+
if linkage != Linkage::External {
263+
// We can only re-use things with external linkage, otherwise
264+
// we'll get a linker error
265+
continue
266+
}
267+
268+
if need_visibility && visibility == Visibility::Hidden {
269+
// If we potentially share things from Rust dylibs, they must
270+
// not be hidden
271+
continue
272+
}
273+
274+
if let &MonoItem::Fn(Instance {
275+
def: InstanceDef::Item(def_id),
276+
substs,
277+
}) = mono_item {
278+
if substs.types().next().is_some() {
279+
symbols.push((ExportedSymbol::Generic(def_id, substs),
280+
SymbolExportLevel::Rust));
263281
}
264282
}
265283
}

0 commit comments

Comments
 (0)