Skip to content

Commit f62903b

Browse files
committed
Export #[inline] #[no_mangle] fns in cdylibs and staticlibs
1 parent ce6d3a7 commit f62903b

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
lines changed

src/librustc_codegen_ssa/back/symbol_export.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
8989
| Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) => {
9090
let def_id = tcx.hir().local_def_id(hir_id);
9191
let generics = tcx.generics_of(def_id);
92-
if !generics.requires_monomorphization(tcx) &&
93-
// Functions marked with #[inline] are only ever codegened
94-
// with "internal" linkage and are never exported.
95-
!Instance::mono(tcx, def_id.to_def_id()).def.generates_cgu_internal_copy(tcx)
92+
if !generics.requires_monomorphization(tcx)
93+
&& (!Instance::mono(tcx, def_id.to_def_id())
94+
.def
95+
.generates_cgu_internal_copy(tcx)
96+
|| tcx.inline_exportable(def_id.to_def_id()))
9697
{
9798
Some(def_id)
9899
} else {

src/librustc_middle/mir/mono.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl<'tcx> MonoItem<'tcx> {
9595
// linkage, then we'll be creating a globally shared version.
9696
if self.explicit_linkage(tcx).is_some()
9797
|| !instance.def.generates_cgu_internal_copy(tcx)
98+
|| tcx.inline_exportable(instance.def_id())
9899
|| Some(instance.def_id()) == entry_def_id.map(LocalDefId::to_def_id)
99100
{
100101
return InstantiationMode::GloballyShared { may_conflict: false };

src/librustc_middle/query/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,10 @@ rustc_queries! {
697697
storage(ArenaCacheSelector<'tcx>)
698698
cache_on_disk_if { true }
699699
}
700+
701+
query inline_exportable(def_id: DefId) -> bool {
702+
desc { |tcx| "computing whether `{}` should be explicitly exported", tcx.def_path_str(def_id) }
703+
}
700704
}
701705

702706
Other {

src/librustc_typeck/collect.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use rustc_middle::ty::util::Discr;
4040
use rustc_middle::ty::util::IntTypeExt;
4141
use rustc_middle::ty::{self, AdtKind, Const, ToPolyTraitRef, Ty, TyCtxt};
4242
use rustc_middle::ty::{ReprOptions, ToPredicate, WithConstness};
43+
use rustc_session::config::CrateType;
4344
use rustc_session::lint;
4445
use rustc_session::parse::feature_err;
4546
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -79,6 +80,7 @@ pub fn provide(providers: &mut Providers<'_>) {
7980
static_mutability,
8081
generator_kind,
8182
codegen_fn_attrs,
83+
inline_exportable,
8284
collect_mod_item_types,
8385
..*providers
8486
};
@@ -2599,6 +2601,16 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
25992601
codegen_fn_attrs
26002602
}
26012603

2604+
fn inline_exportable(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
2605+
// Functions marked with #[inline] are only ever codegened
2606+
// with "internal" linkage and are never exported unless we're
2607+
// building a `staticlib` or `cdylib` and they are marked
2608+
// `#[no_mangle]`.
2609+
tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::NO_MANGLE)
2610+
&& (tcx.sess.crate_types().contains(&CrateType::Cdylib)
2611+
|| tcx.sess.crate_types().contains(&CrateType::Staticlib))
2612+
}
2613+
26022614
/// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
26032615
/// applied to the method prototype.
26042616
fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// compile-flags: -C no-prepopulate-passes
2+
3+
#![crate_type = "cdylib"]
4+
5+
// CHECK: define void @a()
6+
#[no_mangle]
7+
#[inline]
8+
pub extern "C" fn a() {
9+
// side effect to keep `a` around
10+
unsafe {
11+
core::ptr::read_volatile(&42);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// compile-flags: -C no-prepopulate-passes
2+
3+
#![crate_type = "staticlib"]
4+
5+
// CHECK: define void @a()
6+
#[no_mangle]
7+
#[inline]
8+
pub extern "C" fn a() {
9+
// side effect to keep `a` around
10+
unsafe {
11+
core::ptr::read_volatile(&42);
12+
}
13+
}

0 commit comments

Comments
 (0)