@@ -144,9 +144,6 @@ struct CoherenceInfo {
144
144
// the associated trait must be imported at the call site.
145
145
extension_methods : HashMap < def_id , @DVec < @Impl > > ,
146
146
147
- // A mapping from a supertrait to its subtraits.
148
- supertrait_to_subtraits : HashMap < def_id , @DVec < def_id > > ,
149
-
150
147
// A mapping from an implementation ID to the method info and trait method
151
148
// ID of the provided (a.k.a. default) methods in the traits that that
152
149
// implementation implements.
@@ -157,7 +154,6 @@ fn CoherenceInfo() -> CoherenceInfo {
157
154
CoherenceInfo {
158
155
inherent_methods : HashMap ( ) ,
159
156
extension_methods : HashMap ( ) ,
160
- supertrait_to_subtraits : HashMap ( ) ,
161
157
provided_methods : HashMap ( ) ,
162
158
}
163
159
}
@@ -204,9 +200,6 @@ impl CoherenceChecker {
204
200
item_class( struct_def, _) => {
205
201
self . check_implementation ( item, struct_def. traits ) ;
206
202
}
207
- item_trait( _, supertraits, _) => {
208
- self . register_inherited_trait ( item, supertraits) ;
209
- }
210
203
_ => {
211
204
// Nothing to do.
212
205
}
@@ -215,12 +208,8 @@ impl CoherenceChecker {
215
208
.. * default_simple_visitor ( )
216
209
} ) ) ;
217
210
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 ( ) ;
224
213
225
214
// Check whether traits with base types are in privileged scopes.
226
215
self . check_privileged_scopes ( crate ) ;
@@ -377,27 +366,6 @@ impl CoherenceChecker {
377
366
}
378
367
}
379
368
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
-
401
369
fn add_inherent_method ( base_def_id : def_id , implementation : @Impl ) {
402
370
let implementation_list;
403
371
match self . crate_context . coherence_info . inherent_methods
@@ -432,29 +400,57 @@ impl CoherenceChecker {
432
400
implementation_list. push ( implementation) ;
433
401
}
434
402
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 ) {
437
413
438
414
// 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 ;
441
417
let polytype_a =
442
418
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;
447
421
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] ) ;
456
451
}
457
452
}
453
+ None => { /* no impls? */ }
458
454
}
459
455
}
460
456
0 commit comments