Skip to content

Commit 18a47f9

Browse files
committed
auto merge of #6384 : thomaslee/rust/issue-4202, r=catamorphism
This fixes the issue described in #4202. From what I understood of the code, when we reexport a trait in a submodule using e.g. "pub use foo::SomeTrait", we were not previously making an effort to reexport the static methods on that trait. I'm new to the Rust code base (and the Rust language itself) so my approach may not be kosher, but this patch works by changing the encoder to include the static methods associated with traits. I couldn't see any tests for this area of the code, so I didn't really have any examples to go by. If tests are needed, I'm happy to work through that if I can get some assistance to do so.
2 parents d43d3e5 + 1e241ce commit 18a47f9

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

src/librustc/metadata/encoder.rs

+42
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,47 @@ fn encode_path(ecx: @EncodeContext,
363363
ebml_w.end_tag();
364364
}
365365

366+
fn encode_reexported_static_method(ecx: @EncodeContext,
367+
ebml_w: &mut writer::Encoder,
368+
exp: &middle::resolve::Export2,
369+
m: @ty::method) {
370+
debug!("(encode static trait method) reexport '%s::%s'",
371+
*exp.name, *ecx.tcx.sess.str_of(m.ident));
372+
ebml_w.start_tag(tag_items_data_item_reexport);
373+
ebml_w.start_tag(tag_items_data_item_reexport_def_id);
374+
ebml_w.wr_str(def_to_str(m.def_id));
375+
ebml_w.end_tag();
376+
ebml_w.start_tag(tag_items_data_item_reexport_name);
377+
ebml_w.wr_str(*exp.name + "::" + *ecx.tcx.sess.str_of(m.ident));
378+
ebml_w.end_tag();
379+
ebml_w.end_tag();
380+
}
381+
382+
fn encode_reexported_static_methods(ecx: @EncodeContext,
383+
ebml_w: &mut writer::Encoder,
384+
mod_path: &[ast_map::path_elt],
385+
exp: &middle::resolve::Export2) {
386+
match ecx.tcx.trait_methods_cache.find(&exp.def_id) {
387+
Some(methods) => {
388+
match ecx.tcx.items.find(&exp.def_id.node) {
389+
Some(&ast_map::node_item(_, path)) => {
390+
if mod_path != *path {
391+
for methods.each |&m| {
392+
if m.self_ty == ast::sty_static {
393+
encode_reexported_static_method(ecx,
394+
ebml_w,
395+
exp, m);
396+
}
397+
}
398+
}
399+
}
400+
_ => {}
401+
}
402+
}
403+
_ => {}
404+
}
405+
}
406+
366407
fn encode_info_for_mod(ecx: @EncodeContext,
367408
ebml_w: &mut writer::Encoder,
368409
md: &_mod,
@@ -413,6 +454,7 @@ fn encode_info_for_mod(ecx: @EncodeContext,
413454
ebml_w.wr_str(*exp.name);
414455
ebml_w.end_tag();
415456
ebml_w.end_tag();
457+
encode_reexported_static_methods(ecx, ebml_w, path, exp);
416458
}
417459
}
418460
None => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2012 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 use sub_foo::Foo;
12+
13+
pub mod sub_foo {
14+
pub trait Foo {
15+
pub fn foo() -> Self;
16+
}
17+
18+
impl Foo for int {
19+
pub fn foo() -> int { 42 }
20+
}
21+
}
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2012 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+
// xfail-fast
12+
// aux-build:mod_trait_with_static_methods_lib.rs
13+
extern mod mod_trait_with_static_methods_lib;
14+
15+
use mod_trait_with_static_methods_lib::Foo;
16+
17+
pub fn main() {
18+
assert!(42 == Foo::foo());
19+
}

0 commit comments

Comments
 (0)