Skip to content

Commit 18ac265

Browse files
committed
rustc: Crawl static initializers for reachability
This ensures that private functions exported through static initializers will actually end up being public in the object file (so other objects can continue to reference the function). Closes #13620
1 parent b5d6b07 commit 18ac265

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/librustc/middle/reachable.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,12 @@ impl<'a> ReachableContext<'a> {
270270

271271
// Statics with insignificant addresses are not reachable
272272
// because they're inlined specially into all other crates.
273-
ast::ItemStatic(..) => {
273+
ast::ItemStatic(_, _, init) => {
274274
if attr::contains_name(item.attrs.as_slice(),
275275
"address_insignificant") {
276276
self.reachable_symbols.remove(&search_item);
277277
}
278+
visit::walk_expr(self, init, ());
278279
}
279280

280281
// These are normal, nothing reachable about these

src/test/auxiliary/issue-13620-1.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub struct Foo {
12+
pub foo: extern fn()
13+
}
14+
15+
extern fn the_foo() {}
16+
17+
pub static FOO: Foo = Foo {
18+
foo: the_foo
19+
};

src/test/auxiliary/issue-13620-2.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
extern crate crate1 = "issue-13620-1";
12+
13+
pub static FOO2: crate1::Foo = crate1::FOO;

src/test/run-pass/issue-13620.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:issue-13620-1.rs
12+
// aux-build:issue-13620-2.rs
13+
14+
extern crate crate2 = "issue-13620-2";
15+
16+
fn main() {
17+
(crate2::FOO2.foo)();
18+
}

0 commit comments

Comments
 (0)