11use crate :: base:: * ;
22use crate :: config:: StripUnconfigured ;
33use crate :: errors:: {
4- IncompleteParse , RecursionLimitReached , RemoveExprNotSupported , RemoveNodeNotSupported ,
5- UnsupportedKeyValue , WrongFragmentKind ,
4+ EmptyDelegationList , IncompleteParse , RecursionLimitReached , RemoveExprNotSupported ,
5+ RemoveNodeNotSupported , UnsupportedKeyValue , WrongFragmentKind ,
66} ;
77use crate :: hygiene:: SyntaxContext ;
88use crate :: mbe:: diagnostics:: annotate_err_with_kind;
@@ -15,8 +15,8 @@ use rustc_ast::ptr::P;
1515use rustc_ast:: token:: { self , Delimiter } ;
1616use rustc_ast:: tokenstream:: TokenStream ;
1717use rustc_ast:: visit:: { self , try_visit, walk_list, AssocCtxt , Visitor , VisitorResult } ;
18- use rustc_ast:: { AssocItemKind , AstNodeWrapper , AttrArgs , AttrStyle , AttrVec , ExprKind } ;
19- use rustc_ast:: { ForeignItemKind , HasAttrs , HasNodeId } ;
18+ use rustc_ast:: { AssocItemKind , AstNodeWrapper , AttrArgs , AttrStyle , AttrVec , DelegationKind } ;
19+ use rustc_ast:: { ExprKind , ForeignItemKind , HasAttrs , HasNodeId } ;
2020use rustc_ast:: { Inline , ItemKind , MacStmtStyle , MetaItemKind , ModKind } ;
2121use rustc_ast:: { NestedMetaItem , NodeId , PatKind , StmtKind , TyKind } ;
2222use rustc_ast_pretty:: pprust;
@@ -1061,7 +1061,7 @@ trait InvocationCollectorNode: HasAttrs + HasNodeId + Sized {
10611061 fn wrap_flat_map_node_noop_flat_map (
10621062 node : Self ,
10631063 collector : & mut InvocationCollector < ' _ , ' _ > ,
1064- noop_flat_map : impl FnOnce ( Self , & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy ,
1064+ mut noop_flat_map : impl FnMut ( Self , & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy ,
10651065 ) -> Result < Self :: OutputTy , Self > {
10661066 Ok ( noop_flat_map ( node, collector) )
10671067 }
@@ -1105,8 +1105,19 @@ impl InvocationCollectorNode for P<ast::Item> {
11051105 fn wrap_flat_map_node_noop_flat_map (
11061106 mut node : Self ,
11071107 collector : & mut InvocationCollector < ' _ , ' _ > ,
1108- noop_flat_map : impl FnOnce ( Self , & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy ,
1108+ mut noop_flat_map : impl FnMut ( Self , & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy ,
11091109 ) -> Result < Self :: OutputTy , Self > {
1110+ if let ItemKind :: Delegation ( deleg) = & node. kind
1111+ && let DelegationKind :: List ( ..) = deleg. kind
1112+ {
1113+ return Ok ( collector. expand_delegation_list (
1114+ & node,
1115+ deleg,
1116+ ItemKind :: Delegation ,
1117+ |item, collector| noop_flat_map ( item, collector) ,
1118+ ) ) ;
1119+ }
1120+
11101121 if !matches ! ( node. kind, ItemKind :: Mod ( ..) ) {
11111122 return Ok ( noop_flat_map ( node, collector) ) ;
11121123 }
@@ -1230,6 +1241,24 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, TraitItemTag>
12301241 _ => unreachable ! ( ) ,
12311242 }
12321243 }
1244+ fn wrap_flat_map_node_noop_flat_map (
1245+ node : Self ,
1246+ collector : & mut InvocationCollector < ' _ , ' _ > ,
1247+ mut noop_flat_map : impl FnMut ( Self , & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy ,
1248+ ) -> Result < Self :: OutputTy , Self > {
1249+ if let AssocItemKind :: Delegation ( deleg) = & node. wrapped . kind
1250+ && let DelegationKind :: List ( ..) = deleg. kind
1251+ {
1252+ return Ok ( collector. expand_delegation_list (
1253+ & node. wrapped ,
1254+ deleg,
1255+ AssocItemKind :: Delegation ,
1256+ |item, collector| noop_flat_map ( AstNodeWrapper :: new ( item, TraitItemTag ) , collector) ,
1257+ ) ) ;
1258+ }
1259+
1260+ Ok ( noop_flat_map ( node, collector) )
1261+ }
12331262}
12341263
12351264struct ImplItemTag ;
@@ -1255,6 +1284,24 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, ImplItemTag>
12551284 _ => unreachable ! ( ) ,
12561285 }
12571286 }
1287+ fn wrap_flat_map_node_noop_flat_map (
1288+ node : Self ,
1289+ collector : & mut InvocationCollector < ' _ , ' _ > ,
1290+ mut noop_flat_map : impl FnMut ( Self , & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy ,
1291+ ) -> Result < Self :: OutputTy , Self > {
1292+ if let AssocItemKind :: Delegation ( deleg) = & node. wrapped . kind
1293+ && let DelegationKind :: List ( ..) = deleg. kind
1294+ {
1295+ return Ok ( collector. expand_delegation_list (
1296+ & node. wrapped ,
1297+ deleg,
1298+ AssocItemKind :: Delegation ,
1299+ |item, collector| noop_flat_map ( AstNodeWrapper :: new ( item, ImplItemTag ) , collector) ,
1300+ ) ) ;
1301+ }
1302+
1303+ Ok ( noop_flat_map ( node, collector) )
1304+ }
12581305}
12591306
12601307impl InvocationCollectorNode for P < ast:: ForeignItem > {
@@ -1773,6 +1820,48 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
17731820 } ) ;
17741821 }
17751822
1823+ fn expand_delegation_list < K : ' static > (
1824+ & mut self ,
1825+ item : & ast:: Item < K > ,
1826+ deleg : & ast:: Delegation ,
1827+ kind_delegation : impl Copy + FnOnce ( Box < ast:: Delegation > ) -> K ,
1828+ mut noop_flat_map : impl FnMut ( P < ast:: Item < K > > , & mut Self ) -> SmallVec < [ P < ast:: Item < K > > ; 1 ] > ,
1829+ ) -> SmallVec < [ P < ast:: Item < K > > ; 1 ] > {
1830+ assert_eq ! ( item. id, ast:: DUMMY_NODE_ID ) ;
1831+ let DelegationKind :: List ( suffixes) = & deleg. kind else { unreachable ! ( ) } ;
1832+
1833+ if suffixes. is_empty ( ) {
1834+ // Report an error for now, to avoid keeping stem for resolution and stability checks.
1835+ self . cx . dcx ( ) . emit_err ( EmptyDelegationList { span : item. span } ) ;
1836+ }
1837+
1838+ suffixes
1839+ . iter ( )
1840+ . flat_map ( |& ident| {
1841+ let mut path = deleg. path . clone ( ) ;
1842+ path. segments . push ( ast:: PathSegment { ident, id : ast:: DUMMY_NODE_ID , args : None } ) ;
1843+
1844+ let item = ast:: Item {
1845+ attrs : item. attrs . clone ( ) ,
1846+ id : item. id ,
1847+ span : item. span ,
1848+ vis : item. vis . clone ( ) ,
1849+ ident,
1850+ kind : kind_delegation ( Box :: new ( ast:: Delegation {
1851+ id : ast:: DUMMY_NODE_ID ,
1852+ qself : deleg. qself . clone ( ) ,
1853+ path,
1854+ kind : DelegationKind :: Single ,
1855+ body : deleg. body . clone ( ) ,
1856+ } ) ) ,
1857+ tokens : item. tokens . clone ( ) ,
1858+ } ;
1859+
1860+ noop_flat_map ( P ( item) , self )
1861+ } )
1862+ . collect ( )
1863+ }
1864+
17761865 fn flat_map_node < Node : InvocationCollectorNode < OutputTy : Default > > (
17771866 & mut self ,
17781867 mut node : Node ,
0 commit comments