Skip to content

Commit 7396f7f

Browse files
committed
auto merge of #6432 : thomaslee/rust/issue-4202-02, r=catamorphism
My earlier fix for #4202 would not work correctly if the trait being exported was a top-level item relative to the module from which it was being exported. An example that would not work correctly with the original patch: // foo.rs pub use Foo = self::Bar; pub trait Bar { pub fn bar() -> Self; } impl Bar for int { pub fn bar() -> int { 42 } } // bar.rs fn main() { foo::Foo::bar() } This is now supported. I believe this change will allow the GenericPath trait to be exported from core::path as Path in such a manner, which should allow #5389 to move forward.
2 parents ebdb0de + e3a91f6 commit 7396f7f

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/librustc/metadata/encoder.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,20 @@ fn encode_reexported_static_methods(ecx: @EncodeContext,
386386
match ecx.tcx.trait_methods_cache.find(&exp.def_id) {
387387
Some(methods) => {
388388
match ecx.tcx.items.find(&exp.def_id.node) {
389-
Some(&ast_map::node_item(_, path)) => {
390-
if mod_path != *path {
389+
Some(&ast_map::node_item(item, path)) => {
390+
let original_name = ecx.tcx.sess.str_of(item.ident);
391+
392+
//
393+
// We don't need to reexport static methods on traits
394+
// declared in the same module as our `pub use ...` since
395+
// that's done when we encode the trait item.
396+
//
397+
// The only exception is when the reexport *changes* the
398+
// name e.g. `pub use Foo = self::Bar` -- we have
399+
// encoded metadata for static methods relative to Bar,
400+
// but not yet for Foo.
401+
//
402+
if mod_path != *path || *exp.name != *original_name {
391403
for methods.each |&m| {
392404
if m.explicit_self == ast::sty_static {
393405
encode_reexported_static_method(ecx,

src/test/auxiliary/mod_trait_with_static_methods_lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
// except according to those terms.
1010

1111
pub use sub_foo::Foo;
12+
pub use Baz = self::Bar;
13+
14+
pub trait Bar {
15+
pub fn bar() -> Self;
16+
}
17+
18+
impl Bar for int {
19+
pub fn bar() -> int { 84 }
20+
}
1221

1322
pub mod sub_foo {
1423
pub trait Foo {
@@ -18,4 +27,5 @@ pub mod sub_foo {
1827
impl Foo for int {
1928
pub fn foo() -> int { 42 }
2029
}
30+
2131
}

src/test/run-pass/trait_with_static_methods_cross_crate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
extern mod mod_trait_with_static_methods_lib;
1414

1515
use mod_trait_with_static_methods_lib::Foo;
16+
use mod_trait_with_static_methods_lib::Baz;
1617

1718
pub fn main() {
1819
assert_eq!(42, Foo::foo());
20+
assert_eq!(84, Baz::bar());
1921
}

0 commit comments

Comments
 (0)