Skip to content

Commit daa89e0

Browse files
committed
rustc: Remove existing inheritance code from coherence
Inheritance will be implemented differently, hopefully simpler
1 parent 28ecef7 commit daa89e0

File tree

2 files changed

+48
-90
lines changed

2 files changed

+48
-90
lines changed

src/librustc/middle/typeck/check/vtable.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -124,46 +124,8 @@ fn lookup_vtable_covariant(vcx: &VtableContext,
124124
vcx.infcx.ty_to_str(ty),
125125
vcx.infcx.ty_to_str(trait_ty));
126126

127-
let worklist = dvec::DVec();
128-
worklist.push(trait_ty);
129-
while worklist.len() > 0 {
130-
let trait_ty = worklist.pop();
131-
let result = lookup_vtable_invariant(vcx, location_info, ty, trait_ty,
132-
allow_unsafe, is_early);
133-
if result.is_some() {
134-
return result;
135-
}
136-
137-
// Add subtraits to the worklist, if applicable.
138-
match ty::get(trait_ty).sty {
139-
ty::ty_trait(trait_id, _, _) => {
140-
let table = vcx.ccx.coherence_info.supertrait_to_subtraits;
141-
match table.find(trait_id) {
142-
None => {}
143-
Some(subtraits) => {
144-
for subtraits.each |subtrait_id| {
145-
// XXX: This is wrong; subtraits should themselves
146-
// have substs.
147-
let substs =
148-
{ self_r: None, self_ty: None, tps: ~[] };
149-
let trait_ty = ty::mk_trait(vcx.tcx(),
150-
*subtrait_id,
151-
substs,
152-
ty::vstore_box);
153-
worklist.push(trait_ty);
154-
}
155-
}
156-
}
157-
}
158-
_ => {
159-
vcx.tcx().sess.impossible_case(location_info.span,
160-
"lookup_vtable_covariant: \
161-
non-trait in worklist");
162-
}
163-
}
164-
}
165-
166-
return None;
127+
lookup_vtable_invariant(vcx, location_info, ty, trait_ty,
128+
allow_unsafe, is_early)
167129
}
168130

169131
// Look up the vtable to use when treating an item of type `t` as if it has

src/librustc/middle/typeck/coherence.rs

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ struct CoherenceInfo {
144144
// the associated trait must be imported at the call site.
145145
extension_methods: HashMap<def_id,@DVec<@Impl>>,
146146

147-
// A mapping from a supertrait to its subtraits.
148-
supertrait_to_subtraits: HashMap<def_id,@DVec<def_id>>,
149-
150147
// A mapping from an implementation ID to the method info and trait method
151148
// ID of the provided (a.k.a. default) methods in the traits that that
152149
// implementation implements.
@@ -157,7 +154,6 @@ fn CoherenceInfo() -> CoherenceInfo {
157154
CoherenceInfo {
158155
inherent_methods: HashMap(),
159156
extension_methods: HashMap(),
160-
supertrait_to_subtraits: HashMap(),
161157
provided_methods: HashMap(),
162158
}
163159
}
@@ -204,9 +200,6 @@ impl CoherenceChecker {
204200
item_class(struct_def, _) => {
205201
self.check_implementation(item, struct_def.traits);
206202
}
207-
item_trait(_, supertraits, _) => {
208-
self.register_inherited_trait(item, supertraits);
209-
}
210203
_ => {
211204
// Nothing to do.
212205
}
@@ -215,12 +208,8 @@ impl CoherenceChecker {
215208
.. *default_simple_visitor()
216209
}));
217210

218-
// Check trait coherence.
219-
for self.crate_context.coherence_info.extension_methods.each
220-
|def_id, items| {
221-
222-
self.check_implementation_coherence(def_id, items);
223-
}
211+
// Check that there are no overlapping trait instances
212+
self.check_implementation_coherence();
224213

225214
// Check whether traits with base types are in privileged scopes.
226215
self.check_privileged_scopes(crate);
@@ -377,27 +366,6 @@ impl CoherenceChecker {
377366
}
378367
}
379368

380-
fn register_inherited_trait(item: @item, supertraits: ~[@trait_ref]) {
381-
// XXX: This is wrong. We need to support substitutions; e.g.
382-
// trait Foo : Bar<int>.
383-
let supertrait_to_subtraits =
384-
self.crate_context.coherence_info.supertrait_to_subtraits;
385-
let subtrait_id = local_def(item.id);
386-
for supertraits.each |supertrait| {
387-
let supertrait_id = self.trait_ref_to_trait_def_id(*supertrait);
388-
match supertrait_to_subtraits.find(supertrait_id) {
389-
None => {
390-
let new_vec = @dvec::DVec();
391-
new_vec.push(subtrait_id);
392-
supertrait_to_subtraits.insert(supertrait_id, new_vec);
393-
}
394-
Some(existing_vec) => {
395-
existing_vec.push(subtrait_id);
396-
}
397-
}
398-
}
399-
}
400-
401369
fn add_inherent_method(base_def_id: def_id, implementation: @Impl) {
402370
let implementation_list;
403371
match self.crate_context.coherence_info.inherent_methods
@@ -432,29 +400,57 @@ impl CoherenceChecker {
432400
implementation_list.push(implementation);
433401
}
434402

435-
fn check_implementation_coherence(_trait_def_id: def_id,
436-
implementations: @DVec<@Impl>) {
403+
fn check_implementation_coherence() {
404+
let coherence_info = &self.crate_context.coherence_info;
405+
let extension_methods = &coherence_info.extension_methods;
406+
407+
for extension_methods.each_key |trait_id| {
408+
self.check_implementation_coherence_of(trait_id);
409+
}
410+
}
411+
412+
fn check_implementation_coherence_of(trait_def_id: def_id) {
437413

438414
// Unify pairs of polytypes.
439-
for range(0, implementations.len()) |i| {
440-
let implementation_a = implementations.get_elt(i);
415+
do self.iter_impls_of_trait(trait_def_id) |a| {
416+
let implementation_a = a;
441417
let polytype_a =
442418
self.get_self_type_for_implementation(implementation_a);
443-
for range(i + 1, implementations.len()) |j| {
444-
let implementation_b = implementations.get_elt(j);
445-
let polytype_b =
446-
self.get_self_type_for_implementation(implementation_b);
419+
do self.iter_impls_of_trait(trait_def_id) |b| {
420+
let implementation_b = b;
447421

448-
if self.polytypes_unify(polytype_a, polytype_b) {
449-
let session = self.crate_context.tcx.sess;
450-
session.span_err(self.span_of_impl(implementation_b),
451-
~"conflicting implementations for a \
452-
trait");
453-
session.span_note(self.span_of_impl(implementation_a),
454-
~"note conflicting implementation \
455-
here");
422+
// An impl is coherent with itself
423+
if a.did != b.did {
424+
let polytype_b = self.get_self_type_for_implementation(
425+
implementation_b);
426+
427+
if self.polytypes_unify(polytype_a, polytype_b) {
428+
let session = self.crate_context.tcx.sess;
429+
session.span_err(self.span_of_impl(implementation_b),
430+
~"conflicting implementations for a \
431+
trait");
432+
session.span_note(self.span_of_impl(implementation_a),
433+
~"note conflicting implementation \
434+
here");
435+
}
436+
}
437+
}
438+
}
439+
}
440+
441+
fn iter_impls_of_trait(trait_def_id: def_id,
442+
f: &fn(@Impl)) {
443+
444+
let coherence_info = &self.crate_context.coherence_info;
445+
let extension_methods = &coherence_info.extension_methods;
446+
447+
match extension_methods.find(trait_def_id) {
448+
Some(impls) => {
449+
for uint::range(0, impls.len()) |i| {
450+
f(impls[i]);
456451
}
457452
}
453+
None => { /* no impls? */ }
458454
}
459455
}
460456

0 commit comments

Comments
 (0)