@@ -1306,6 +1306,8 @@ trait InvocationCollectorNode: HasAttrs + HasNodeId + Sized {
13061306 fn declared_idents ( & self ) -> Vec < Ident > {
13071307 vec ! [ ]
13081308 }
1309+
1310+ fn as_target ( & self ) -> Target ;
13091311}
13101312
13111313impl InvocationCollectorNode for Box < ast:: Item > {
@@ -1457,6 +1459,10 @@ impl InvocationCollectorNode for Box<ast::Item> {
14571459 self . kind . ident ( ) . into_iter ( ) . collect ( )
14581460 }
14591461 }
1462+
1463+ fn as_target ( & self ) -> Target {
1464+ Target :: from_ast_item ( & * self )
1465+ }
14601466}
14611467
14621468struct TraitItemTag ;
@@ -1498,6 +1504,9 @@ impl InvocationCollectorNode for AstNodeWrapper<Box<ast::AssocItem>, TraitItemTa
14981504 fn flatten_outputs ( items : impl Iterator < Item = Self :: OutputTy > ) -> Self :: OutputTy {
14991505 items. flatten ( ) . collect ( )
15001506 }
1507+ fn as_target ( & self ) -> Target {
1508+ Target :: from_assoc_item_kind ( & self . wrapped . kind , AssocCtxt :: Trait )
1509+ }
15011510}
15021511
15031512struct ImplItemTag ;
@@ -1539,6 +1548,9 @@ impl InvocationCollectorNode for AstNodeWrapper<Box<ast::AssocItem>, ImplItemTag
15391548 fn flatten_outputs ( items : impl Iterator < Item = Self :: OutputTy > ) -> Self :: OutputTy {
15401549 items. flatten ( ) . collect ( )
15411550 }
1551+ fn as_target ( & self ) -> Target {
1552+ Target :: from_assoc_item_kind ( & self . wrapped . kind , AssocCtxt :: Impl { of_trait : false } )
1553+ }
15421554}
15431555
15441556struct TraitImplItemTag ;
@@ -1580,6 +1592,9 @@ impl InvocationCollectorNode for AstNodeWrapper<Box<ast::AssocItem>, TraitImplIt
15801592 fn flatten_outputs ( items : impl Iterator < Item = Self :: OutputTy > ) -> Self :: OutputTy {
15811593 items. flatten ( ) . collect ( )
15821594 }
1595+ fn as_target ( & self ) -> Target {
1596+ Target :: from_assoc_item_kind ( & self . wrapped . kind , AssocCtxt :: Impl { of_trait : true } )
1597+ }
15831598}
15841599
15851600impl InvocationCollectorNode for Box < ast:: ForeignItem > {
@@ -1602,6 +1617,17 @@ impl InvocationCollectorNode for Box<ast::ForeignItem> {
16021617 _ => unreachable ! ( ) ,
16031618 }
16041619 }
1620+ fn as_target ( & self ) -> Target {
1621+ match & self . kind {
1622+ ForeignItemKind :: Static ( _) => Target :: ForeignStatic ,
1623+ ForeignItemKind :: Fn ( _) => Target :: ForeignFn ,
1624+ ForeignItemKind :: TyAlias ( _) => Target :: ForeignTy ,
1625+ ForeignItemKind :: MacCall ( _) => {
1626+ // Turned into another kind of foreign item during macro expansion.
1627+ unreachable ! ( ) ;
1628+ }
1629+ }
1630+ }
16051631}
16061632
16071633impl InvocationCollectorNode for ast:: Variant {
@@ -1615,6 +1641,9 @@ impl InvocationCollectorNode for ast::Variant {
16151641 fn walk_flat_map ( self , collector : & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy {
16161642 walk_flat_map_variant ( collector, self )
16171643 }
1644+ fn as_target ( & self ) -> Target {
1645+ Target :: Variant
1646+ }
16181647}
16191648
16201649impl InvocationCollectorNode for ast:: WherePredicate {
@@ -1628,6 +1657,9 @@ impl InvocationCollectorNode for ast::WherePredicate {
16281657 fn walk_flat_map ( self , collector : & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy {
16291658 walk_flat_map_where_predicate ( collector, self )
16301659 }
1660+ fn as_target ( & self ) -> Target {
1661+ Target :: WherePredicate
1662+ }
16311663}
16321664
16331665impl InvocationCollectorNode for ast:: FieldDef {
@@ -1641,6 +1673,9 @@ impl InvocationCollectorNode for ast::FieldDef {
16411673 fn walk_flat_map ( self , collector : & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy {
16421674 walk_flat_map_field_def ( collector, self )
16431675 }
1676+ fn as_target ( & self ) -> Target {
1677+ Target :: Field
1678+ }
16441679}
16451680
16461681impl InvocationCollectorNode for ast:: PatField {
@@ -1654,6 +1689,9 @@ impl InvocationCollectorNode for ast::PatField {
16541689 fn walk_flat_map ( self , collector : & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy {
16551690 walk_flat_map_pat_field ( collector, self )
16561691 }
1692+ fn as_target ( & self ) -> Target {
1693+ Target :: PatField
1694+ }
16571695}
16581696
16591697impl InvocationCollectorNode for ast:: ExprField {
@@ -1667,6 +1705,9 @@ impl InvocationCollectorNode for ast::ExprField {
16671705 fn walk_flat_map ( self , collector : & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy {
16681706 walk_flat_map_expr_field ( collector, self )
16691707 }
1708+ fn as_target ( & self ) -> Target {
1709+ Target :: ExprField
1710+ }
16701711}
16711712
16721713impl InvocationCollectorNode for ast:: Param {
@@ -1680,6 +1721,9 @@ impl InvocationCollectorNode for ast::Param {
16801721 fn walk_flat_map ( self , collector : & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy {
16811722 walk_flat_map_param ( collector, self )
16821723 }
1724+ fn as_target ( & self ) -> Target {
1725+ Target :: Param
1726+ }
16831727}
16841728
16851729impl InvocationCollectorNode for ast:: GenericParam {
@@ -1693,6 +1737,25 @@ impl InvocationCollectorNode for ast::GenericParam {
16931737 fn walk_flat_map ( self , collector : & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy {
16941738 walk_flat_map_generic_param ( collector, self )
16951739 }
1740+ fn as_target ( & self ) -> Target {
1741+ let mut has_default = false ;
1742+ Target :: GenericParam {
1743+ kind : match & self . kind {
1744+ rustc_ast:: GenericParamKind :: Lifetime => {
1745+ rustc_hir:: target:: GenericParamKind :: Lifetime
1746+ }
1747+ rustc_ast:: GenericParamKind :: Type { default } => {
1748+ has_default = default. is_some ( ) ;
1749+ rustc_hir:: target:: GenericParamKind :: Type
1750+ }
1751+ rustc_ast:: GenericParamKind :: Const { default, .. } => {
1752+ has_default = default. is_some ( ) ;
1753+ rustc_hir:: target:: GenericParamKind :: Const
1754+ }
1755+ } ,
1756+ has_default,
1757+ }
1758+ }
16961759}
16971760
16981761impl InvocationCollectorNode for ast:: Arm {
@@ -1706,6 +1769,9 @@ impl InvocationCollectorNode for ast::Arm {
17061769 fn walk_flat_map ( self , collector : & mut InvocationCollector < ' _ , ' _ > ) -> Self :: OutputTy {
17071770 walk_flat_map_arm ( collector, self )
17081771 }
1772+ fn as_target ( & self ) -> Target {
1773+ Target :: Arm
1774+ }
17091775}
17101776
17111777impl InvocationCollectorNode for ast:: Stmt {
@@ -1779,6 +1845,9 @@ impl InvocationCollectorNode for ast::Stmt {
17791845 }
17801846 }
17811847 }
1848+ fn as_target ( & self ) -> Target {
1849+ Target :: Statement
1850+ }
17821851}
17831852
17841853impl InvocationCollectorNode for ast:: Crate {
@@ -1805,6 +1874,9 @@ impl InvocationCollectorNode for ast::Crate {
18051874 // Standard prelude imports are left in the crate for backward compatibility.
18061875 self . items . truncate ( collector. cx . num_standard_library_imports ) ;
18071876 }
1877+ fn as_target ( & self ) -> Target {
1878+ Target :: Crate
1879+ }
18081880}
18091881
18101882impl InvocationCollectorNode for ast:: Ty {
@@ -1838,6 +1910,10 @@ impl InvocationCollectorNode for ast::Ty {
18381910 _ => unreachable ! ( ) ,
18391911 }
18401912 }
1913+ fn as_target ( & self ) -> Target {
1914+ // This is only used for attribute parsing, which are not allowed on types.
1915+ unreachable ! ( )
1916+ }
18411917}
18421918
18431919impl InvocationCollectorNode for ast:: Pat {
@@ -1861,6 +1937,9 @@ impl InvocationCollectorNode for ast::Pat {
18611937 _ => unreachable ! ( ) ,
18621938 }
18631939 }
1940+ fn as_target ( & self ) -> Target {
1941+ todo ! ( ) ;
1942+ }
18641943}
18651944
18661945impl InvocationCollectorNode for ast:: Expr {
@@ -1887,6 +1966,9 @@ impl InvocationCollectorNode for ast::Expr {
18871966 _ => unreachable ! ( ) ,
18881967 }
18891968 }
1969+ fn as_target ( & self ) -> Target {
1970+ Target :: Expression
1971+ }
18901972}
18911973
18921974struct OptExprTag ;
@@ -1916,6 +1998,9 @@ impl InvocationCollectorNode for AstNodeWrapper<Box<ast::Expr>, OptExprTag> {
19161998 fn pre_flat_map_node_collect_attr ( cfg : & StripUnconfigured < ' _ > , attr : & ast:: Attribute ) {
19171999 cfg. maybe_emit_expr_attr_err ( attr) ;
19182000 }
2001+ fn as_target ( & self ) -> Target {
2002+ Target :: Expression
2003+ }
19192004}
19202005
19212006/// This struct is a hack to workaround unstable of `stmt_expr_attributes`.
@@ -1947,6 +2032,9 @@ impl InvocationCollectorNode for AstNodeWrapper<ast::Expr, MethodReceiverTag> {
19472032 _ => unreachable ! ( ) ,
19482033 }
19492034 }
2035+ fn as_target ( & self ) -> Target {
2036+ Target :: Expression
2037+ }
19502038}
19512039
19522040fn build_single_delegations < ' a , Node : InvocationCollectorNode > (
@@ -2210,7 +2298,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
22102298
22112299 fn expand_cfg_true (
22122300 & mut self ,
2213- node : & mut ( impl HasAttrs + HasNodeId ) ,
2301+ node : & mut impl InvocationCollectorNode ,
22142302 attr : ast:: Attribute ,
22152303 pos : usize ,
22162304 ) -> EvalConfigResult {
@@ -2219,8 +2307,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
22192307 & attr,
22202308 attr. span ,
22212309 self . cfg ( ) . lint_node_id ,
2222- // Target doesn't matter for `cfg` parsing.
2223- Target :: Crate ,
2310+ node. as_target ( ) ,
22242311 self . cfg ( ) . features ,
22252312 ShouldEmit :: ErrorsAndLints { recovery : Recovery :: Allowed } ,
22262313 parse_cfg,
0 commit comments