1
1
use crate :: base:: * ;
2
2
use crate :: config:: StripUnconfigured ;
3
3
use crate :: errors:: {
4
- IncompleteParse , RecursionLimitReached , RemoveExprNotSupported , RemoveNodeNotSupported ,
5
- UnsupportedKeyValue , WrongFragmentKind ,
4
+ EmptyDelegationList , IncompleteParse , RecursionLimitReached , RemoveExprNotSupported ,
5
+ RemoveNodeNotSupported , UnsupportedKeyValue , WrongFragmentKind ,
6
6
} ;
7
7
use crate :: hygiene:: SyntaxContext ;
8
8
use crate :: mbe:: diagnostics:: annotate_err_with_kind;
@@ -15,8 +15,8 @@ use rustc_ast::ptr::P;
15
15
use rustc_ast:: token:: { self , Delimiter } ;
16
16
use rustc_ast:: tokenstream:: TokenStream ;
17
17
use 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 } ;
20
20
use rustc_ast:: { Inline , ItemKind , MacStmtStyle , MetaItemKind , ModKind } ;
21
21
use rustc_ast:: { NestedMetaItem , NodeId , PatKind , StmtKind , TyKind } ;
22
22
use rustc_ast_pretty:: pprust;
@@ -1061,7 +1061,7 @@ trait InvocationCollectorNode: HasAttrs + HasNodeId + Sized {
1061
1061
fn wrap_flat_map_node_noop_flat_map (
1062
1062
node : Self ,
1063
1063
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 ,
1065
1065
) -> Result < Self :: OutputTy , Self > {
1066
1066
Ok ( noop_flat_map ( node, collector) )
1067
1067
}
@@ -1105,8 +1105,19 @@ impl InvocationCollectorNode for P<ast::Item> {
1105
1105
fn wrap_flat_map_node_noop_flat_map (
1106
1106
mut node : Self ,
1107
1107
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 ,
1109
1109
) -> 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
+
1110
1121
if !matches ! ( node. kind, ItemKind :: Mod ( ..) ) {
1111
1122
return Ok ( noop_flat_map ( node, collector) ) ;
1112
1123
}
@@ -1230,6 +1241,24 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, TraitItemTag>
1230
1241
_ => unreachable ! ( ) ,
1231
1242
}
1232
1243
}
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
+ }
1233
1262
}
1234
1263
1235
1264
struct ImplItemTag ;
@@ -1255,6 +1284,24 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::AssocItem>, ImplItemTag>
1255
1284
_ => unreachable ! ( ) ,
1256
1285
}
1257
1286
}
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
+ }
1258
1305
}
1259
1306
1260
1307
impl InvocationCollectorNode for P < ast:: ForeignItem > {
@@ -1773,6 +1820,48 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
1773
1820
} ) ;
1774
1821
}
1775
1822
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
+
1776
1865
fn flat_map_node < Node : InvocationCollectorNode < OutputTy : Default > > (
1777
1866
& mut self ,
1778
1867
mut node : Node ,
0 commit comments