@@ -73,8 +73,8 @@ enum Inserted {
7373 /// The impl was inserted as a new child in this group of children.
7474 BecameNewSibling ( Option < OverlapError > ) ,
7575
76- /// The impl should replace an existing impl X, because the impl specializes X .
77- ReplaceChild ( DefId ) ,
76+ /// The impl should replace existing impls [X1, ..], because the impl specializes X1, X2, etc .
77+ ReplaceChildren ( Vec < DefId > ) ,
7878
7979 /// The impl is a specialization of an existing child.
8080 ShouldRecurseOn ( DefId ) ,
@@ -124,6 +124,7 @@ impl<'a, 'gcx, 'tcx> Children {
124124 -> Result < Inserted , OverlapError >
125125 {
126126 let mut last_lint = None ;
127+ let mut replace_children = Vec :: new ( ) ;
127128
128129 debug ! (
129130 "insert(impl_def_id={:?}, simplified_self={:?})" ,
@@ -194,7 +195,7 @@ impl<'a, 'gcx, 'tcx> Children {
194195 debug ! ( "placing as parent of TraitRef {:?}" ,
195196 tcx. impl_trait_ref( possible_sibling) . unwrap( ) ) ;
196197
197- return Ok ( Inserted :: ReplaceChild ( possible_sibling) ) ;
198+ replace_children . push ( possible_sibling) ;
198199 } else {
199200 if !tcx. impls_are_allowed_to_overlap ( impl_def_id, possible_sibling) {
200201 traits:: overlapping_impls (
@@ -211,6 +212,10 @@ impl<'a, 'gcx, 'tcx> Children {
211212 }
212213 }
213214
215+ if !replace_children. is_empty ( ) {
216+ return Ok ( Inserted :: ReplaceChildren ( replace_children) ) ;
217+ }
218+
214219 // no overlap with any potential siblings, so add as a new sibling
215220 debug ! ( "placing as new sibling" ) ;
216221 self . insert_blindly ( tcx, impl_def_id) ;
@@ -282,7 +287,7 @@ impl<'a, 'gcx, 'tcx> Graph {
282287 last_lint = opt_lint;
283288 break ;
284289 }
285- ReplaceChild ( grand_child_to_be ) => {
290+ ReplaceChildren ( grand_children_to_be ) => {
286291 // We currently have
287292 //
288293 // P
@@ -302,17 +307,23 @@ impl<'a, 'gcx, 'tcx> Graph {
302307 let siblings = self . children
303308 . get_mut ( & parent)
304309 . unwrap ( ) ;
305- siblings. remove_existing ( tcx, grand_child_to_be) ;
310+ for & grand_child_to_be in & grand_children_to_be {
311+ siblings. remove_existing ( tcx, grand_child_to_be) ;
312+ }
306313 siblings. insert_blindly ( tcx, impl_def_id) ;
307314 }
308315
309316 // Set G's parent to N and N's parent to P
310- self . parent . insert ( grand_child_to_be, impl_def_id) ;
317+ for & grand_child_to_be in & grand_children_to_be {
318+ self . parent . insert ( grand_child_to_be, impl_def_id) ;
319+ }
311320 self . parent . insert ( impl_def_id, parent) ;
312321
313322 // Add G as N's child.
314- self . children . entry ( impl_def_id) . or_default ( )
315- . insert_blindly ( tcx, grand_child_to_be) ;
323+ for & grand_child_to_be in & grand_children_to_be {
324+ self . children . entry ( impl_def_id) . or_default ( )
325+ . insert_blindly ( tcx, grand_child_to_be) ;
326+ }
316327 break ;
317328 }
318329 ShouldRecurseOn ( new_parent) => {
0 commit comments