Skip to content

Commit c84beef

Browse files
author
hyd-dev
committed
Add associated functions that have custom linkage to reachable_set
1 parent eb2226b commit c84beef

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

compiler/rustc_passes/src/reachable.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,15 @@ impl<'tcx> ReachableContext<'tcx> {
211211
if !self.any_library {
212212
// If we are building an executable, only explicitly extern
213213
// types need to be exported.
214-
if let Node::Item(item) = *node {
215-
let reachable = if let hir::ItemKind::Fn(ref sig, ..) = item.kind {
216-
sig.header.abi != Abi::Rust
217-
} else {
218-
false
219-
};
220-
let codegen_attrs = self.tcx.codegen_fn_attrs(item.def_id);
214+
if let Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), def_id, .. })
215+
| Node::ImplItem(hir::ImplItem {
216+
kind: hir::ImplItemKind::Fn(sig, ..),
217+
def_id,
218+
..
219+
}) = *node
220+
{
221+
let reachable = sig.header.abi != Abi::Rust;
222+
let codegen_attrs = self.tcx.codegen_fn_attrs(*def_id);
221223
let is_extern = codegen_attrs.contains_extern_indicator();
222224
let std_internal =
223225
codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL);
@@ -335,17 +337,23 @@ struct CollectPrivateImplItemsVisitor<'a, 'tcx> {
335337
worklist: &'a mut Vec<LocalDefId>,
336338
}
337339

338-
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a, 'tcx> {
339-
fn visit_item(&mut self, item: &hir::Item<'_>) {
340+
impl CollectPrivateImplItemsVisitor<'_, '_> {
341+
fn push_to_worklist_if_has_custom_linkage(&mut self, def_id: LocalDefId) {
340342
// Anything which has custom linkage gets thrown on the worklist no
341343
// matter where it is in the crate, along with "special std symbols"
342344
// which are currently akin to allocator symbols.
343-
let codegen_attrs = self.tcx.codegen_fn_attrs(item.def_id);
345+
let codegen_attrs = self.tcx.codegen_fn_attrs(def_id);
344346
if codegen_attrs.contains_extern_indicator()
345347
|| codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
346348
{
347-
self.worklist.push(item.def_id);
349+
self.worklist.push(def_id);
348350
}
351+
}
352+
}
353+
354+
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a, 'tcx> {
355+
fn visit_item(&mut self, item: &hir::Item<'_>) {
356+
self.push_to_worklist_if_has_custom_linkage(item.def_id);
349357

350358
// We need only trait impls here, not inherent impls, and only non-exported ones
351359
if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(ref trait_ref), ref items, .. }) =
@@ -375,8 +383,8 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a, 'tcx
375383

376384
fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'_>) {}
377385

378-
fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) {
379-
// processed in visit_item above
386+
fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) {
387+
self.push_to_worklist_if_has_custom_linkage(impl_item.def_id);
380388
}
381389

382390
fn visit_foreign_item(&mut self, _foreign_item: &hir::ForeignItem<'_>) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![crate_type = "lib"]
2+
3+
struct Bar;
4+
5+
impl Bar {
6+
#[no_mangle]
7+
fn bar() -> u8 {
8+
2
9+
}
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// aux-build: no-mangle-associated-fn.rs
2+
// run-pass
3+
4+
extern crate no_mangle_associated_fn;
5+
6+
struct Foo;
7+
8+
impl Foo {
9+
#[no_mangle]
10+
fn foo() -> u8 {
11+
1
12+
}
13+
}
14+
15+
fn main() {
16+
extern "Rust" {
17+
fn foo() -> u8;
18+
fn bar() -> u8;
19+
}
20+
assert_eq!(unsafe { foo() }, 1);
21+
assert_eq!(unsafe { bar() }, 2);
22+
}

0 commit comments

Comments
 (0)