Skip to content

Commit ffc879c

Browse files
committed
Major rework of how calls to self and super methods work.
Eliminates method_super, method_self, and vtable_self, merging all of them into the param cases. Cloes #4396. Closes #7301.
1 parent 79f8a7f commit ffc879c

File tree

11 files changed

+120
-292
lines changed

11 files changed

+120
-292
lines changed

src/librustc/middle/astencode.rs

+2-22
Original file line numberDiff line numberDiff line change
@@ -590,12 +590,6 @@ impl tr for method_origin {
590590
typeck::method_trait(did, m, vstore) => {
591591
typeck::method_trait(did.tr(xcx), m, vstore)
592592
}
593-
typeck::method_self(did, m) => {
594-
typeck::method_self(did.tr(xcx), m)
595-
}
596-
typeck::method_super(trait_did, m) => {
597-
typeck::method_super(trait_did.tr(xcx), m)
598-
}
599593
}
600594
}
601595
}
@@ -645,20 +639,13 @@ pub fn encode_vtable_origin(ecx: &e::EncodeContext,
645639
typeck::vtable_param(pn, bn) => {
646640
do ebml_w.emit_enum_variant("vtable_param", 1u, 2u) |ebml_w| {
647641
do ebml_w.emit_enum_variant_arg(0u) |ebml_w| {
648-
ebml_w.emit_uint(pn);
642+
pn.encode(ebml_w);
649643
}
650644
do ebml_w.emit_enum_variant_arg(1u) |ebml_w| {
651645
ebml_w.emit_uint(bn);
652646
}
653647
}
654648
}
655-
typeck::vtable_self(def_id) => {
656-
do ebml_w.emit_enum_variant("vtable_self", 2u, 1u) |ebml_w| {
657-
do ebml_w.emit_enum_variant_arg(0u) |ebml_w| {
658-
ebml_w.emit_def_id(def_id)
659-
}
660-
}
661-
}
662649
}
663650
}
664651
}
@@ -715,20 +702,13 @@ impl vtable_decoder_helpers for reader::Decoder {
715702
1 => {
716703
typeck::vtable_param(
717704
do this.read_enum_variant_arg(0u) |this| {
718-
this.read_uint()
705+
Decodable::decode(this)
719706
},
720707
do this.read_enum_variant_arg(1u) |this| {
721708
this.read_uint()
722709
}
723710
)
724711
}
725-
2 => {
726-
typeck::vtable_self(
727-
do this.read_enum_variant_arg(0u) |this| {
728-
this.read_def_id_noxcx(cdata)
729-
}
730-
)
731-
}
732712
// hard to avoid - user input
733713
_ => fail!("bad enum variant")
734714
}

src/librustc/middle/privacy.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
use metadata::csearch;
1616
use middle::ty::{ty_struct, ty_enum};
1717
use middle::ty;
18-
use middle::typeck::{method_map, method_origin, method_param, method_self};
19-
use middle::typeck::{method_super};
18+
use middle::typeck::{method_map, method_origin, method_param};
2019
use middle::typeck::{method_static, method_trait};
2120

2221
use std::util::ignore;
@@ -291,9 +290,7 @@ pub fn check_crate<'mm>(tcx: ty::ctxt,
291290
method_num: method_num,
292291
_
293292
}) |
294-
method_trait(trait_id, method_num, _) |
295-
method_self(trait_id, method_num) |
296-
method_super(trait_id, method_num) => {
293+
method_trait(trait_id, method_num, _) => {
297294
if trait_id.crate == local_crate {
298295
match tcx.items.find(&trait_id.node) {
299296
Some(&node_item(item, _)) => {

src/librustc/middle/trans/common.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -1096,32 +1096,25 @@ pub fn resolve_vtable_under_param_substs(tcx: ty::ctxt,
10961096
}
10971097
}
10981098
}
1099-
typeck::vtable_self(_trait_id) => {
1100-
match param_substs {
1101-
Some(@param_substs
1102-
{self_vtables: Some(self_vtables), _}) => {
1103-
self_vtables[0].clone()
1104-
}
1105-
_ => {
1106-
tcx.sess.bug(fmt!(
1107-
"resolve_vtable_in_fn_ctxt: asked to lookup but \
1108-
no self_vtable in the fn_ctxt!"))
1109-
}
1110-
}
1111-
}
11121099
}
11131100
}
11141101

11151102
pub fn find_vtable(tcx: ty::ctxt,
11161103
ps: &param_substs,
1117-
n_param: uint,
1104+
n_param: typeck::param_index,
11181105
n_bound: uint)
11191106
-> typeck::vtable_origin {
1120-
debug!("find_vtable(n_param=%u, n_bound=%u, ps=%s)",
1107+
debug!("find_vtable(n_param=%?, n_bound=%u, ps=%s)",
11211108
n_param, n_bound, ps.repr(tcx));
11221109

1123-
let tables = ps.vtables.expect("vtables missing where they are needed");
1124-
let param_bounds = tables[n_param];
1110+
let param_bounds = match n_param {
1111+
typeck::param_self => ps.self_vtables.expect("self vtables missing"),
1112+
typeck::param_numbered(n) => {
1113+
let tables = ps.vtables
1114+
.expect("vtables missing where they are needed");
1115+
tables[n]
1116+
}
1117+
};
11251118
param_bounds[n_bound].clone()
11261119
}
11271120

src/librustc/middle/trans/meth.rs

+3-60
Original file line numberDiff line numberDiff line change
@@ -147,46 +147,13 @@ pub fn trans_method_callee(bcx: @mut Block,
147147
mentry: typeck::method_map_entry)
148148
-> Callee {
149149
let _icx = push_ctxt("impl::trans_method_callee");
150-
let tcx = bcx.tcx();
151150

152151
debug!("trans_method_callee(callee_id=%?, this=%s, mentry=%s)",
153152
callee_id,
154153
bcx.expr_to_str(this),
155154
mentry.repr(bcx.tcx()));
156155

157-
// Replace method_self with method_static here.
158-
let mut origin = mentry.origin;
159-
match origin {
160-
typeck::method_super(trait_id, method_index) => {
161-
// <self_ty> is the self type for this method call
162-
let self_ty = node_id_type(bcx, this.id);
163-
// <impl_id> is the ID of the implementation of
164-
// trait <trait_id> for type <self_ty>
165-
let impl_id = ty::bogus_get_impl_id_from_ty(tcx, trait_id, self_ty);
166-
// Get the supertrait's methods
167-
let supertrait_method_def_ids = ty::trait_method_def_ids(tcx, trait_id);
168-
// Make sure to fail with a readable error message if
169-
// there's some internal error here
170-
if !(method_index < supertrait_method_def_ids.len()) {
171-
tcx.sess.bug("trans_method_callee: supertrait method \
172-
index is out of bounds");
173-
}
174-
// Get the method name using the method index in the origin
175-
let method_name =
176-
ty::method(tcx, supertrait_method_def_ids[method_index]).ident;
177-
// Now that we know the impl ID, we can look up the method
178-
// ID from its name
179-
origin = typeck::method_static(
180-
method_with_name(bcx.ccx(), impl_id, method_name));
181-
}
182-
typeck::method_self(*) |
183-
typeck::method_static(*) | typeck::method_param(*) |
184-
typeck::method_trait(*) => {}
185-
}
186-
187-
debug!("origin=%?", origin);
188-
189-
match origin {
156+
match mentry.origin {
190157
typeck::method_static(did) => {
191158
let callee_fn = callee::trans_fn_ref(bcx, did, callee_id);
192159
let mut temp_cleanups = ~[];
@@ -210,7 +177,8 @@ pub fn trans_method_callee(bcx: @mut Block,
210177
}) => {
211178
match bcx.fcx.param_substs {
212179
Some(substs) => {
213-
let vtbl = find_vtable(bcx.tcx(), substs, p, b);
180+
let vtbl = find_vtable(bcx.tcx(), substs,
181+
p, b);
214182
trans_monomorphized_callee(bcx, callee_id, this, mentry,
215183
trait_id, off, vtbl)
216184
}
@@ -219,25 +187,6 @@ pub fn trans_method_callee(bcx: @mut Block,
219187
}
220188
}
221189

222-
typeck::method_self(trait_id, method_index) => {
223-
match bcx.fcx.param_substs {
224-
Some(@param_substs
225-
{self_vtables: Some(vtbls), _}) => {
226-
let vtbl = vtbls[0].clone();
227-
trans_monomorphized_callee(bcx,
228-
callee_id,
229-
this,
230-
mentry,
231-
trait_id,
232-
method_index,
233-
vtbl)
234-
}
235-
_ => {
236-
fail!("trans_method_callee: missing self_vtable")
237-
}
238-
}
239-
}
240-
241190
typeck::method_trait(_, off, store) => {
242191
trans_trait_callee(bcx,
243192
callee_id,
@@ -246,9 +195,6 @@ pub fn trans_method_callee(bcx: @mut Block,
246195
store,
247196
mentry.explicit_self)
248197
}
249-
typeck::method_super(*) => {
250-
fail!("method_super should have been handled above")
251-
}
252198
}
253199
}
254200

@@ -403,9 +349,6 @@ pub fn trans_monomorphized_callee(bcx: @mut Block,
403349
typeck::vtable_param(*) => {
404350
fail!("vtable_param left in monomorphized function's vtable substs");
405351
}
406-
typeck::vtable_self(*) => {
407-
fail!("vtable_self left in monomorphized function's vtable substs");
408-
}
409352
};
410353

411354
}

src/librustc/middle/trans/type_use.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,12 @@ pub fn mark_for_method_call(cx: &Context, e_id: node_id, callee_id: node_id) {
274274
opt_static_did = Some(did);
275275
}
276276
typeck::method_param(typeck::method_param {
277-
param_num: param,
277+
param_num: typeck::param_numbered(param),
278278
_
279279
}) => {
280280
cx.uses[param] |= use_tydesc;
281281
}
282-
typeck::method_trait(*) | typeck::method_self(*)
283-
| typeck::method_super(*) => (),
282+
_ => (),
284283
}
285284
}
286285
}

src/librustc/middle/ty.rs

+1-28
Original file line numberDiff line numberDiff line change
@@ -3065,9 +3065,7 @@ pub fn method_call_type_param_defs(tcx: ctxt,
30653065
typeck::method_param(typeck::method_param {
30663066
trait_id: trt_id,
30673067
method_num: n_mth, _}) |
3068-
typeck::method_trait(trt_id, n_mth, _) |
3069-
typeck::method_self(trt_id, n_mth) |
3070-
typeck::method_super(trt_id, n_mth) => {
3068+
typeck::method_trait(trt_id, n_mth, _) => {
30713069
// ...trait methods bounds, in contrast, include only the
30723070
// method bounds, so we must preprend the tps from the
30733071
// trait itself. This ought to be harmonized.
@@ -4401,31 +4399,6 @@ pub fn count_traits_and_supertraits(tcx: ctxt,
44014399
return total;
44024400
}
44034401

4404-
// Given a trait and a type, returns the impl of that type.
4405-
// This is broken, of course, by parametric impls. This used to use
4406-
// a table specifically for this mapping, but I removed that table.
4407-
// This is only used when calling a supertrait method from a default method,
4408-
// and should go away once I fix how that works. -sully
4409-
pub fn bogus_get_impl_id_from_ty(tcx: ctxt,
4410-
trait_id: def_id, self_ty: t) -> def_id {
4411-
match tcx.trait_impls.find(&trait_id) {
4412-
Some(ty_to_impl) => {
4413-
for ty_to_impl.iter().advance |imp| {
4414-
let impl_ty = tcx.tcache.get_copy(&imp.did);
4415-
if impl_ty.ty == self_ty { return imp.did; }
4416-
}
4417-
// try autoderef!
4418-
match deref(tcx, self_ty, false) {
4419-
Some(some_ty) =>
4420-
bogus_get_impl_id_from_ty(tcx, trait_id, some_ty.ty),
4421-
None => tcx.sess.bug("get_impl_id: no impl of trait for \
4422-
this type")
4423-
}
4424-
},
4425-
None => tcx.sess.bug("get_impl_id: trait isn't in trait_impls")
4426-
}
4427-
}
4428-
44294402
pub fn get_tydesc_ty(tcx: ctxt) -> Result<t, ~str> {
44304403
do tcx.lang_items.require(TyDescStructLangItem).map |tydesc_lang_item| {
44314404
tcx.intrinsic_defs.find_copy(tydesc_lang_item)

0 commit comments

Comments
 (0)