Skip to content

Commit 0daaeab

Browse files
committed
Conservatively export all trait methods and impls
The comments have more information as to why this is done, but the basic idea is that finding an exported trait is actually a fairly difficult problem. The true answer lies in whether a trait is ever referenced from another exported method, and right now this kind of analysis doesn't exist, so the conservative answer of "yes" is always returned to answer whether a trait is exported. Closes #11224 Closes #11225
1 parent 1502b11 commit 0daaeab

File tree

8 files changed

+157
-0
lines changed

8 files changed

+157
-0
lines changed

src/librustc/middle/privacy.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,23 @@ struct EmbargoVisitor<'a> {
167167
reexports: HashSet<ast::NodeId>,
168168
}
169169

170+
impl<'a> EmbargoVisitor<'a> {
171+
// There are checks inside of privacy which depend on knowing whether a
172+
// trait should be exported or not. The two current consumers of this are:
173+
//
174+
// 1. Should default methods of a trait be exported?
175+
// 2. Should the methods of an implementation of a trait be exported?
176+
//
177+
// The answer to both of these questions partly rely on whether the trait
178+
// itself is exported or not. If the trait is somehow exported, then the
179+
// answers to both questions must be yes. Right now this question involves
180+
// more analysis than is currently done in rustc, so we conservatively
181+
// answer "yes" so that all traits need to be exported.
182+
fn exported_trait(&self, _id: ast::NodeId) -> bool {
183+
true
184+
}
185+
}
186+
170187
impl<'a> Visitor<()> for EmbargoVisitor<'a> {
171188
fn visit_item(&mut self, item: @ast::item, _: ()) {
172189
let orig_all_pub = self.prev_exported;
@@ -175,6 +192,12 @@ impl<'a> Visitor<()> for EmbargoVisitor<'a> {
175192
// cannot have visibility qualifiers on them anyway
176193
ast::item_impl(..) | ast::item_foreign_mod(..) => {}
177194

195+
// Traits are a little special in that even if they themselves are
196+
// not public they may still be exported.
197+
ast::item_trait(..) => {
198+
self.prev_exported = self.exported_trait(item.id);
199+
}
200+
178201
// Private by default, hence we only retain the "public chain" if
179202
// `pub` is explicitly listed.
180203
_ => {

src/libstd/local_data.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub type Key<T> = &'static KeyValue<T>;
6262
#[allow(missing_doc)]
6363
pub enum KeyValue<T> { Key }
6464

65+
#[allow(missing_doc)]
6566
trait LocalData {}
6667
impl<T: 'static> LocalData for T {}
6768

src/test/auxiliary/issue-11224.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2013 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+
#[deny(dead_code)];
12+
13+
mod inner {
14+
pub trait Trait {
15+
fn f(&self) { f(); }
16+
}
17+
18+
impl Trait for int {}
19+
20+
fn f() {}
21+
}
22+
23+
pub fn foo() {
24+
let a = &1 as &inner::Trait;
25+
a.f();
26+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2013 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+
mod inner {
12+
pub trait Trait {
13+
fn f(&self) { f(); }
14+
}
15+
16+
impl Trait for int {}
17+
18+
fn f() {}
19+
}
20+
21+
pub fn foo<T: inner::Trait>(t: T) {
22+
t.f();
23+
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2013 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+
use inner::Trait;
12+
13+
mod inner {
14+
pub struct Foo;
15+
pub trait Trait {
16+
fn f(&self);
17+
}
18+
19+
impl Trait for Foo {
20+
fn f(&self) { }
21+
}
22+
}
23+
24+
pub trait Outer {
25+
fn foo<T: Trait>(&self, t: T) { t.f(); }
26+
}
27+
28+
impl Outer for int {}
29+
30+
pub fn foo<T: Outer>(t: T) {
31+
t.foo(inner::Foo);
32+
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2013 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-11224.rs
12+
// xfail-fast
13+
14+
extern mod unused = "issue-11224";
15+
16+
fn main() {}

src/test/run-pass/issue-11225-1.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2013 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-11225-1.rs
12+
// xfail-fast
13+
14+
extern mod foo = "issue-11225-1";
15+
16+
fn main() {
17+
foo::foo(1);
18+
}

src/test/run-pass/issue-11225-2.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2013 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-11225-2.rs
12+
// xfail-fast
13+
14+
extern mod foo = "issue-11225-2";
15+
16+
fn main() {
17+
foo::foo(1);
18+
}

0 commit comments

Comments
 (0)