Skip to content

Commit

Permalink
auto merge of #6432 : thomaslee/rust/issue-4202-02, r=catamorphism
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed May 20, 2013
2 parents ebdb0de + e3a91f6 commit 7396f7f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,20 @@ fn encode_reexported_static_methods(ecx: @EncodeContext,
match ecx.tcx.trait_methods_cache.find(&exp.def_id) {
Some(methods) => {
match ecx.tcx.items.find(&exp.def_id.node) {
Some(&ast_map::node_item(_, path)) => {
if mod_path != *path {
Some(&ast_map::node_item(item, path)) => {
let original_name = ecx.tcx.sess.str_of(item.ident);

//
// We don't need to reexport static methods on traits
// declared in the same module as our `pub use ...` since
// that's done when we encode the trait item.
//
// The only exception is when the reexport *changes* the
// name e.g. `pub use Foo = self::Bar` -- we have
// encoded metadata for static methods relative to Bar,
// but not yet for Foo.
//
if mod_path != *path || *exp.name != *original_name {
for methods.each |&m| {
if m.explicit_self == ast::sty_static {
encode_reexported_static_method(ecx,
Expand Down
10 changes: 10 additions & 0 deletions src/test/auxiliary/mod_trait_with_static_methods_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
// except according to those terms.

pub use sub_foo::Foo;
pub use Baz = self::Bar;

pub trait Bar {
pub fn bar() -> Self;
}

impl Bar for int {
pub fn bar() -> int { 84 }
}

pub mod sub_foo {
pub trait Foo {
Expand All @@ -18,4 +27,5 @@ pub mod sub_foo {
impl Foo for int {
pub fn foo() -> int { 42 }
}

}
2 changes: 2 additions & 0 deletions src/test/run-pass/trait_with_static_methods_cross_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
extern mod mod_trait_with_static_methods_lib;

use mod_trait_with_static_methods_lib::Foo;
use mod_trait_with_static_methods_lib::Baz;

pub fn main() {
assert_eq!(42, Foo::foo());
assert_eq!(84, Baz::bar());
}

0 comments on commit 7396f7f

Please sign in to comment.