Skip to content

Commit b055a10

Browse files
committed
auto merge of #7545 : msullivan/rust/default-methods, r=catamorphism
r?
2 parents f850777 + 7238d5a commit b055a10

File tree

6 files changed

+79
-48
lines changed

6 files changed

+79
-48
lines changed

src/librustc/middle/privacy.rs

+8
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ pub fn check_crate<'mm>(tcx: ty::ctxt,
245245
method_id: def_id,
246246
name: &ident) =
247247
|span, method_id, name| {
248+
// If the method is a default method, we need to use the def_id of
249+
// the default implementation.
250+
// Having to do this this is really unfortunate.
251+
let method_id = match tcx.provided_method_sources.find(&method_id) {
252+
None => method_id,
253+
Some(source) => source.method_id
254+
};
255+
248256
if method_id.crate == local_crate {
249257
let is_private = method_is_private(span, method_id.node);
250258
let container_id = local_method_container_id(span,

src/librustc/middle/trans/meth.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -641,16 +641,18 @@ pub fn vtable_id(ccx: @mut CrateContext,
641641
-> mono_id {
642642
match origin {
643643
&typeck::vtable_static(impl_id, ref substs, sub_vtables) => {
644+
let psubsts = param_substs {
645+
tys: copy *substs,
646+
vtables: Some(sub_vtables),
647+
self_ty: None,
648+
self_vtable: None
649+
};
650+
644651
monomorphize::make_mono_id(
645652
ccx,
646653
impl_id,
647-
*substs,
648-
if sub_vtables.is_empty() {
649-
None
650-
} else {
651-
Some(sub_vtables)
652-
},
653654
None,
655+
&psubsts,
654656
None)
655657
}
656658

src/librustc/middle/trans/monomorphize.rs

+39-27
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ pub fn monomorphic_fn(ccx: @mut CrateContext,
5050
fn_id=%s, \
5151
real_substs=%s, \
5252
vtables=%s, \
53+
self_vtable=%s, \
5354
impl_did_opt=%s, \
5455
ref_id=%?)",
5556
fn_id.repr(ccx.tcx),
5657
real_substs.repr(ccx.tcx),
5758
vtables.repr(ccx.tcx),
59+
self_vtable.repr(ccx.tcx),
5860
impl_did_opt.repr(ccx.tcx),
5961
ref_id);
6062

@@ -71,7 +73,16 @@ pub fn monomorphic_fn(ccx: @mut CrateContext,
7173
for real_substs.tps.iter().advance |s| { assert!(!ty::type_has_params(*s)); }
7274
for substs.iter().advance |s| { assert!(!ty::type_has_params(*s)); }
7375
let param_uses = type_use::type_uses_for(ccx, fn_id, substs.len());
74-
let hash_id = make_mono_id(ccx, fn_id, substs, vtables, impl_did_opt,
76+
77+
let psubsts = @param_substs {
78+
tys: substs,
79+
vtables: vtables,
80+
self_ty: real_substs.self_ty,
81+
self_vtable: self_vtable
82+
};
83+
84+
let hash_id = make_mono_id(ccx, fn_id, impl_did_opt,
85+
&*psubsts,
7586
Some(param_uses));
7687
if hash_id.params.iter().any_(
7788
|p| match *p { mono_precise(_, _) => false, _ => true }) {
@@ -80,12 +91,10 @@ pub fn monomorphic_fn(ccx: @mut CrateContext,
8091

8192
debug!("monomorphic_fn(\
8293
fn_id=%s, \
83-
vtables=%s, \
84-
substs=%s, \
94+
psubsts=%s, \
8595
hash_id=%?)",
8696
fn_id.repr(ccx.tcx),
87-
vtables.repr(ccx.tcx),
88-
substs.repr(ccx.tcx),
97+
psubsts.repr(ccx.tcx),
8998
hash_id);
9099

91100
match ccx.monomorphized.find(&hash_id) {
@@ -142,8 +151,8 @@ pub fn monomorphic_fn(ccx: @mut CrateContext,
142151
ast_map::node_struct_ctor(_, i, pt) => (pt, i.ident, i.span)
143152
};
144153

145-
let mono_ty = ty::subst_tps(ccx.tcx, substs,
146-
real_substs.self_ty, llitem_ty);
154+
let mono_ty = ty::subst_tps(ccx.tcx, psubsts.tys,
155+
psubsts.self_ty, llitem_ty);
147156
let llfty = type_of_fn_from_ty(ccx, mono_ty);
148157

149158
ccx.stats.n_monos += 1;
@@ -172,13 +181,6 @@ pub fn monomorphic_fn(ccx: @mut CrateContext,
172181
lldecl
173182
};
174183

175-
let psubsts = Some(@param_substs {
176-
tys: substs,
177-
vtables: vtables,
178-
self_ty: real_substs.self_ty,
179-
self_vtable: self_vtable
180-
});
181-
182184
let lldecl = match map_node {
183185
ast_map::node_item(i@@ast::item {
184186
node: ast::item_fn(ref decl, _, _, _, ref body),
@@ -192,7 +194,7 @@ pub fn monomorphic_fn(ccx: @mut CrateContext,
192194
body,
193195
d,
194196
no_self,
195-
psubsts,
197+
Some(psubsts),
196198
fn_id.node,
197199
[]);
198200
d
@@ -202,7 +204,7 @@ pub fn monomorphic_fn(ccx: @mut CrateContext,
202204
}
203205
ast_map::node_foreign_item(i, _, _, _) => {
204206
let d = mk_lldecl();
205-
foreign::trans_intrinsic(ccx, d, i, pt, psubsts.get(), i.attrs,
207+
foreign::trans_intrinsic(ccx, d, i, pt, psubsts, i.attrs,
206208
ref_id);
207209
d
208210
}
@@ -214,7 +216,7 @@ pub fn monomorphic_fn(ccx: @mut CrateContext,
214216
match v.node.kind {
215217
ast::tuple_variant_kind(ref args) => {
216218
trans_enum_variant(ccx, enum_item.id, v, /*bad*/copy *args,
217-
this_tv.disr_val, psubsts, d);
219+
this_tv.disr_val, Some(psubsts), d);
218220
}
219221
ast::struct_variant_kind(_) =>
220222
ccx.tcx.sess.bug("can't monomorphize struct variants"),
@@ -225,13 +227,13 @@ pub fn monomorphic_fn(ccx: @mut CrateContext,
225227
// XXX: What should the self type be here?
226228
let d = mk_lldecl();
227229
set_inline_hint_if_appr(/*bad*/copy mth.attrs, d);
228-
meth::trans_method(ccx, pt, mth, psubsts, d);
230+
meth::trans_method(ccx, pt, mth, Some(psubsts), d);
229231
d
230232
}
231233
ast_map::node_trait_method(@ast::provided(mth), _, pt) => {
232234
let d = mk_lldecl();
233235
set_inline_hint_if_appr(/*bad*/copy mth.attrs, d);
234-
meth::trans_method(ccx, /*bad*/copy *pt, mth, psubsts, d);
236+
meth::trans_method(ccx, /*bad*/copy *pt, mth, Some(psubsts), d);
235237
d
236238
}
237239
ast_map::node_struct_ctor(struct_def, _, _) => {
@@ -241,7 +243,7 @@ pub fn monomorphic_fn(ccx: @mut CrateContext,
241243
/*bad*/copy struct_def.fields,
242244
struct_def.ctor_id.expect("ast-mapped tuple struct \
243245
didn't have a ctor id"),
244-
psubsts,
246+
Some(psubsts),
245247
d);
246248
d
247249
}
@@ -320,26 +322,36 @@ pub fn normalize_for_monomorphization(tcx: ty::ctxt,
320322

321323
pub fn make_mono_id(ccx: @mut CrateContext,
322324
item: ast::def_id,
323-
substs: &[ty::t],
324-
vtables: Option<typeck::vtable_res>,
325325
impl_did_opt: Option<ast::def_id>,
326+
substs: &param_substs,
326327
param_uses: Option<@~[type_use::type_uses]>) -> mono_id {
327328
// FIXME (possibly #5801): Need a lot of type hints to get
328329
// .collect() to work.
329-
let precise_param_ids: ~[(ty::t, Option<@~[mono_id]>)] = match vtables {
330+
let substs_iter = substs.self_ty.iter().chain_(substs.tys.iter());
331+
let precise_param_ids: ~[(ty::t, Option<@~[mono_id]>)] = match substs.vtables {
330332
Some(vts) => {
331333
debug!("make_mono_id vtables=%s substs=%s",
332-
vts.repr(ccx.tcx), substs.repr(ccx.tcx));
333-
vts.iter().zip(substs.iter()).transform(|(vtable, subst)| {
334+
vts.repr(ccx.tcx), substs.tys.repr(ccx.tcx));
335+
let self_vtables = substs.self_vtable.map(|vtbl| @~[copy *vtbl]);
336+
let vts_iter = self_vtables.iter().chain_(vts.iter());
337+
vts_iter.zip(substs_iter).transform(|(vtable, subst)| {
334338
let v = vtable.map(|vt| meth::vtable_id(ccx, vt));
335339
(*subst, if !v.is_empty() { Some(@v) } else { None })
336340
}).collect()
337341
}
338-
None => substs.iter().transform(|subst| (*subst, None::<@~[mono_id]>)).collect()
342+
None => substs_iter.transform(|subst| (*subst, None::<@~[mono_id]>)).collect()
339343
};
344+
345+
340346
let param_ids = match param_uses {
341347
Some(ref uses) => {
342-
precise_param_ids.iter().zip(uses.iter()).transform(|(id, uses)| {
348+
// param_uses doesn't include a use for the self type.
349+
// We just say it is fully used.
350+
let self_use =
351+
substs.self_ty.map(|_| type_use::use_repr|type_use::use_tydesc);
352+
let uses_iter = self_use.iter().chain_(uses.iter());
353+
354+
precise_param_ids.iter().zip(uses_iter).transform(|(id, uses)| {
343355
if ccx.sess.no_monomorphic_collapse() {
344356
match copy *id {
345357
(a, b) => mono_precise(a, b)

src/test/auxiliary/trait_default_method_xc_aux.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#[allow(default_methods)];
22

3+
pub struct Something { x: int }
4+
35
pub trait A {
46
fn f(&self) -> int;
57
fn g(&self) -> int { 10 }
@@ -11,6 +13,10 @@ impl A for int {
1113
fn f(&self) -> int { 10 }
1214
}
1315

16+
impl A for Something {
17+
fn f(&self) -> int { 10 }
18+
}
19+
1420
trait B<T> {
1521
fn thing<U>(&self, x: T, y: U) -> (T, U) { (x, y) }
1622
}

src/test/run-pass/bug-7183-generics.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ fn main() {
3939
assert_eq!(Some(Some(3)).hi(), ~"something!something!hello: 3");
4040
assert_eq!(None::<int>.hi(), ~"hello - none");
4141
42-
// These fail because of a bug in monomorphization's ID generation.
43-
//assert_eq!(Some(None::<int>).hi(), ~"something!hello - none");
44-
//assert_eq!(Some(3).hi(), ~"something!hello: 3");
42+
assert_eq!(Some(None::<int>).hi(), ~"something!hello - none");
43+
assert_eq!(Some(3).hi(), ~"something!hello: 3");
4544
}

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

+16-12
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
#[allow(default_methods)];
55

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

99

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

14+
mod stuff {
15+
pub struct thing { x: int }
16+
}
1417

15-
pub struct thing { x: int }
16-
impl A for thing {
18+
impl A for stuff::thing {
1719
fn f(&self) -> int { 10 }
1820
}
1921

@@ -29,8 +31,8 @@ fn neq<T: TestEquality>(lhs: &T, rhs: &T) -> bool {
2931
}
3032

3133

32-
impl TestEquality for thing {
33-
fn test_eq(&self, rhs: &thing) -> bool {
34+
impl TestEquality for stuff::thing {
35+
fn test_eq(&self, rhs: &stuff::thing) -> bool {
3436
//self.x.test_eq(&rhs.x)
3537
eq(&self.x, &rhs.x)
3638
}
@@ -41,15 +43,17 @@ fn main () {
4143
// Some tests of random things
4244
f(0);
4345

44-
let a = thing { x: 0 };
45-
let b = thing { x: 1 };
46+
let a = stuff::thing { x: 0 };
47+
let b = stuff::thing { x: 1 };
48+
let c = Something { x: 1 };
4649

47-
//assert_eq!(0i.g(), 10);
50+
assert_eq!(0i.g(), 10);
4851
assert_eq!(a.g(), 10);
4952
assert_eq!(a.h(), 10);
53+
assert_eq!(c.h(), 10);
5054

51-
52-
//assert_eq!(0i.thing(3.14, 1), (3.14, 1));
55+
0i.thing(3.14, 1);
56+
assert_eq!(0i.thing(3.14, 1), (3.14, 1));
5357

5458
assert_eq!(g(0i, 3.14, 1), (3.14, 1));
5559
assert_eq!(g(false, 3.14, 1), (3.14, 1));
@@ -59,8 +63,8 @@ fn main () {
5963

6064

6165
// Trying out a real one
62-
//assert!(12.test_neq(&10));
63-
//assert!(!10.test_neq(&10));
66+
assert!(12.test_neq(&10));
67+
assert!(!10.test_neq(&10));
6468
assert!(a.test_neq(&b));
6569
assert!(!a.test_neq(&a));
6670

0 commit comments

Comments
 (0)