22use std:: cmp:: Ordering ;
33
44use itertools:: { EitherOrBoth , Itertools } ;
5- use syntax:: ast:: {
6- self , edit:: AstNodeEdit , make, AstNode , AttrsOwner , PathSegmentKind , VisibilityOwner ,
5+ use syntax:: {
6+ ast:: { self , make, AstNode , AttrsOwner , PathSegmentKind , VisibilityOwner } ,
7+ ted,
78} ;
89
910/// What type of merges are allowed.
@@ -65,7 +66,7 @@ pub fn try_merge_trees(
6566 } else {
6667 ( lhs. split_prefix ( & lhs_prefix) , rhs. split_prefix ( & rhs_prefix) )
6768 } ;
68- recursive_merge ( & lhs, & rhs, merge)
69+ recursive_merge ( & lhs, & rhs, merge) . map ( |it| it . clone_for_update ( ) )
6970}
7071
7172/// Recursively "zips" together lhs and rhs.
@@ -78,7 +79,8 @@ fn recursive_merge(
7879 . use_tree_list ( )
7980 . into_iter ( )
8081 . flat_map ( |list| list. use_trees ( ) )
81- // we use Option here to early return from this function(this is not the same as a `filter` op)
82+ // We use Option here to early return from this function(this is not the
83+ // same as a `filter` op).
8284 . map ( |tree| match merge. is_tree_allowed ( & tree) {
8385 true => Some ( tree) ,
8486 false => None ,
@@ -111,8 +113,10 @@ fn recursive_merge(
111113 let tree_is_self = |tree : ast:: UseTree | {
112114 tree. path ( ) . as_ref ( ) . map ( path_is_self) . unwrap_or ( false )
113115 } ;
114- // check if only one of the two trees has a tree list, and whether that then contains `self` or not.
115- // If this is the case we can skip this iteration since the path without the list is already included in the other one via `self`
116+ // Check if only one of the two trees has a tree list, and
117+ // whether that then contains `self` or not. If this is the
118+ // case we can skip this iteration since the path without
119+ // the list is already included in the other one via `self`.
116120 let tree_contains_self = |tree : & ast:: UseTree | {
117121 tree. use_tree_list ( )
118122 . map ( |tree_list| tree_list. use_trees ( ) . any ( tree_is_self) )
@@ -127,9 +131,11 @@ fn recursive_merge(
127131 _ => ( ) ,
128132 }
129133
130- // glob imports arent part of the use-tree lists so we need to special handle them here as well
131- // this special handling is only required for when we merge a module import into a glob import of said module
132- // see the `merge_self_glob` or `merge_mod_into_glob` tests
134+ // Glob imports aren't part of the use-tree lists so we need
135+ // to special handle them here as well this special handling
136+ // is only required for when we merge a module import into a
137+ // glob import of said module see the `merge_self_glob` or
138+ // `merge_mod_into_glob` tests.
133139 if lhs_t. star_token ( ) . is_some ( ) || rhs_t. star_token ( ) . is_some ( ) {
134140 * lhs_t = make:: use_tree (
135141 make:: path_unqualified ( make:: path_segment_self ( ) ) ,
@@ -165,11 +171,11 @@ fn recursive_merge(
165171 }
166172 }
167173
168- Some ( if let Some ( old ) = lhs. use_tree_list ( ) {
169- lhs. replace_descendant ( old , make :: use_tree_list ( use_trees ) ) . clone_for_update ( )
170- } else {
171- lhs . clone ( )
172- } )
174+ let lhs = lhs. clone_subtree ( ) . clone_for_update ( ) ;
175+ if let Some ( old ) = lhs. use_tree_list ( ) {
176+ ted :: replace ( old . syntax ( ) , make :: use_tree_list ( use_trees ) . syntax ( ) . clone_for_update ( ) ) ;
177+ }
178+ ast :: UseTree :: cast ( lhs . syntax ( ) . clone_subtree ( ) )
173179}
174180
175181/// Traverses both paths until they differ, returning the common prefix of both.
0 commit comments