Skip to content

Commit 186f6fa

Browse files
committed
Get cross crate static default methods working. Closes #7569.
1 parent 1bbb434 commit 186f6fa

File tree

8 files changed

+40
-31
lines changed

8 files changed

+40
-31
lines changed

src/librustc/metadata/encoder.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ fn encode_info_for_method(ecx: &EncodeContext,
791791
if len > 0u || should_inline {
792792
(ecx.encode_inlined_item)(
793793
ecx, ebml_w, impl_path,
794-
ii_method(local_def(parent_id), m));
794+
ii_method(local_def(parent_id), false, m));
795795
} else {
796796
encode_symbol(ecx, ebml_w, m.id);
797797
}
@@ -1123,21 +1123,16 @@ fn encode_info_for_item(ecx: &EncodeContext,
11231123
}
11241124

11251125
provided(m) => {
1126-
// This is obviously a bogus assert but I don't think this
1127-
// ever worked before anyhow...near as I can tell, before
1128-
// we would emit two items.
1129-
if method_ty.explicit_self == sty_static {
1130-
tcx.sess.span_unimpl(
1131-
item.span,
1132-
fmt!("Method %s is both provided and static",
1133-
token::ident_to_str(&method_ty.ident)));
1126+
// If this is a static method, we've already encoded
1127+
// this.
1128+
if method_ty.explicit_self != sty_static {
1129+
encode_type_param_bounds(ebml_w, ecx,
1130+
&m.generics.ty_params);
11341131
}
1135-
encode_type_param_bounds(ebml_w, ecx,
1136-
&m.generics.ty_params);
11371132
encode_method_sort(ebml_w, 'p');
11381133
(ecx.encode_inlined_item)(
11391134
ecx, ebml_w, path,
1140-
ii_method(local_def(item.id), m));
1135+
ii_method(local_def(item.id), true, m));
11411136
}
11421137
}
11431138

src/librustc/middle/astencode.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ fn simplify_ast(ii: &ast::inlined_item) -> ast::inlined_item {
319319
match *ii {
320320
//hack: we're not dropping items
321321
ast::ii_item(i) => ast::ii_item(fld.fold_item(i).get()),
322-
ast::ii_method(d, m) => ast::ii_method(d, fld.fold_method(m)),
322+
ast::ii_method(d, is_provided, m) =>
323+
ast::ii_method(d, is_provided, fld.fold_method(m)),
323324
ast::ii_foreign(i) => ast::ii_foreign(fld.fold_foreign_item(i))
324325
}
325326
}
@@ -340,7 +341,8 @@ fn renumber_ast(xcx: @ExtendedDecodeContext, ii: ast::inlined_item)
340341

341342
match ii {
342343
ast::ii_item(i) => ast::ii_item(fld.fold_item(i).get()),
343-
ast::ii_method(d, m) => ast::ii_method(xcx.tr_def_id(d), fld.fold_method(m)),
344+
ast::ii_method(d, is_provided, m) =>
345+
ast::ii_method(xcx.tr_def_id(d), is_provided, fld.fold_method(m)),
344346
ast::ii_foreign(i) => ast::ii_foreign(fld.fold_foreign_item(i)),
345347
}
346348
}

src/librustc/middle/trans/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub fn maybe_instantiate_inline(ccx: @mut CrateContext, fn_id: ast::def_id,
8888
ccx.sess.bug("maybe_get_item_ast returned a found_parent \
8989
with a non-item parent");
9090
}
91-
csearch::found(ast::ii_method(impl_did, mth)) => {
91+
csearch::found(ast::ii_method(impl_did, _is_provided, mth)) => {
9292
ccx.stats.n_inlines += 1;
9393
ccx.external.insert(fn_id, Some(mth.id));
9494
// If this is a default method, we can't look up the

src/libsyntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ pub enum foreign_item_ {
10331033
#[deriving(Eq, Encodable, Decodable,IterBytes)]
10341034
pub enum inlined_item {
10351035
ii_item(@item),
1036-
ii_method(def_id /* impl id */, @method),
1036+
ii_method(def_id /* impl id */, bool /* is provided */, @method),
10371037
ii_foreign(@foreign_item),
10381038
}
10391039

src/libsyntax/ast_map.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ pub fn map_decoded_item(diag: @span_handler,
165165
i.vis, // Wrong but OK
166166
@path));
167167
}
168-
ii_method(impl_did, m) => {
169-
map_method(impl_did, @path, m, cx);
168+
ii_method(impl_did, is_provided, m) => {
169+
map_method(impl_did, @path, m, is_provided, cx);
170170
}
171171
}
172172

@@ -207,8 +207,11 @@ pub fn map_pat(pat: @pat, (cx,v): (@mut Ctx, visit::vt<@mut Ctx>)) {
207207
}
208208

209209
pub fn map_method(impl_did: def_id, impl_path: @path,
210-
m: @method, cx: @mut Ctx) {
211-
cx.map.insert(m.id, node_method(m, impl_did, impl_path));
210+
m: @method, is_provided: bool, cx: @mut Ctx) {
211+
let entry = if is_provided {
212+
node_trait_method(@provided(m), impl_did, impl_path)
213+
} else { node_method(m, impl_did, impl_path) };
214+
cx.map.insert(m.id, entry);
212215
cx.map.insert(m.self_id, node_local(special_idents::self_));
213216
}
214217

@@ -219,7 +222,7 @@ pub fn map_item(i: @item, (cx, v): (@mut Ctx, visit::vt<@mut Ctx>)) {
219222
item_impl(_, _, _, ref ms) => {
220223
let impl_did = ast_util::local_def(i.id);
221224
for ms.iter().advance |m| {
222-
map_method(impl_did, extend(cx, i.ident), *m, cx);
225+
map_method(impl_did, extend(cx, i.ident), *m, false, cx);
223226
}
224227
}
225228
item_enum(ref enum_definition, _) => {

src/libsyntax/ast_util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,23 +298,23 @@ impl inlined_item_utils for inlined_item {
298298
match *self {
299299
ii_item(i) => /* FIXME (#2543) */ copy i.ident,
300300
ii_foreign(i) => /* FIXME (#2543) */ copy i.ident,
301-
ii_method(_, m) => /* FIXME (#2543) */ copy m.ident,
301+
ii_method(_, _, m) => /* FIXME (#2543) */ copy m.ident,
302302
}
303303
}
304304

305305
fn id(&self) -> ast::node_id {
306306
match *self {
307307
ii_item(i) => i.id,
308308
ii_foreign(i) => i.id,
309-
ii_method(_, m) => m.id,
309+
ii_method(_, _, m) => m.id,
310310
}
311311
}
312312

313313
fn accept<E: Copy>(&self, e: E, v: visit::vt<E>) {
314314
match *self {
315315
ii_item(i) => (v.visit_item)(i, (e, v)),
316316
ii_foreign(i) => (v.visit_foreign_item)(i, (e, v)),
317-
ii_method(_, m) => visit::visit_method_helper(m, (e, v)),
317+
ii_method(_, _, m) => visit::visit_method_helper(m, (e, v)),
318318
}
319319
}
320320
}

src/test/auxiliary/trait_default_method_xc_aux.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pub struct Something { x: int }
55
pub trait A {
66
fn f(&self) -> int;
77
fn g(&self) -> int { 10 }
8-
fn h(&self) -> int { 10 }
8+
fn h(&self) -> int { 11 }
9+
fn lurr(x: &Self, y: &Self) -> int { x.g() + y.h() }
910
}
1011

1112

@@ -19,6 +20,7 @@ impl A for Something {
1920

2021
trait B<T> {
2122
fn thing<U>(&self, x: T, y: U) -> (T, U) { (x, y) }
23+
fn staticthing<U>(z: &Self, x: T, y: U) -> (T, U) { (x, y) }
2224
}
2325

2426
impl<T> B<T> for int { }

src/test/run-pass/trait-default-method-xc.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
#[allow(default_methods)];
55

66
extern mod aux(name = "trait_default_method_xc_aux");
7-
use aux::{A, B, TestEquality, Something};
8-
7+
use aux::{A, TestEquality, Something};
8+
use aux::B;
99

1010
fn f<T: aux::A>(i: T) {
1111
assert_eq!(i.g(), 10);
1212
}
1313

14+
fn welp<T>(i: int, x: &T) -> int {
15+
i.g()
16+
}
17+
1418
mod stuff {
1519
pub struct thing { x: int }
1620
}
@@ -43,23 +47,26 @@ fn main () {
4347
// Some tests of random things
4448
f(0);
4549

50+
assert_eq!(A::lurr(&0, &1), 21);
51+
4652
let a = stuff::thing { x: 0 };
4753
let b = stuff::thing { x: 1 };
4854
let c = Something { x: 1 };
4955

5056
assert_eq!(0i.g(), 10);
5157
assert_eq!(a.g(), 10);
52-
assert_eq!(a.h(), 10);
53-
assert_eq!(c.h(), 10);
58+
assert_eq!(a.h(), 11);
59+
assert_eq!(c.h(), 11);
5460

55-
0i.thing(3.14, 1);
5661
assert_eq!(0i.thing(3.14, 1), (3.14, 1));
62+
assert_eq!(B::staticthing(&0i, 3.14, 1), (3.14, 1));
63+
assert_eq!(B::staticthing::<float, int, int>(&0i, 3.14, 1), (3.14, 1));
5764

5865
assert_eq!(g(0i, 3.14, 1), (3.14, 1));
5966
assert_eq!(g(false, 3.14, 1), (3.14, 1));
6067

6168
let obj = @0i as @A;
62-
assert_eq!(obj.h(), 10);
69+
assert_eq!(obj.h(), 11);
6370

6471

6572
// Trying out a real one

0 commit comments

Comments
 (0)