@@ -3,15 +3,14 @@ use std::rc::Rc;
3
3
use std:: sync:: Arc ;
4
4
use std:: { iter, mem} ;
5
5
6
- use rustc_ast as ast;
7
6
use rustc_ast:: mut_visit:: * ;
8
7
use rustc_ast:: ptr:: P ;
9
8
use rustc_ast:: tokenstream:: TokenStream ;
10
9
use rustc_ast:: visit:: { self , AssocCtxt , Visitor , VisitorResult , try_visit, walk_list} ;
11
10
use rustc_ast:: {
12
- AssocItemKind , AstNodeWrapper , AttrArgs , AttrStyle , AttrVec , ExprKind , ForeignItemKind ,
13
- HasAttrs , HasNodeId , Inline , ItemKind , MacStmtStyle , MetaItemInner , MetaItemKind , ModKind ,
14
- NodeId , PatKind , StmtKind , TyKind , token,
11
+ self as ast , AssocItemKind , AstNodeWrapper , AttrArgs , AttrStyle , AttrVec , DUMMY_NODE_ID ,
12
+ ExprKind , ForeignItemKind , HasAttrs , HasNodeId , Inline , ItemKind , MacStmtStyle , MetaItemInner ,
13
+ MetaItemKind , ModKind , NodeId , PatKind , StmtKind , TyKind , token,
15
14
} ;
16
15
use rustc_ast_pretty:: pprust;
17
16
use rustc_data_structures:: flat_map_in_place:: FlatMapInPlace ;
@@ -131,13 +130,9 @@ macro_rules! ast_fragments {
131
130
pub ( crate ) fn mut_visit_with<F : MutVisitor >( & mut self , vis: & mut F ) {
132
131
match self {
133
132
AstFragment :: OptExpr ( opt_expr) => {
134
- visit_clobber( opt_expr, |opt_expr| {
135
- if let Some ( expr) = opt_expr {
136
- vis. filter_map_expr( expr)
137
- } else {
138
- None
139
- }
140
- } ) ;
133
+ if let Some ( expr) = opt_expr. take( ) {
134
+ * opt_expr = vis. filter_map_expr( expr)
135
+ }
141
136
}
142
137
AstFragment :: MethodReceiverExpr ( expr) => vis. visit_method_receiver_expr( expr) ,
143
138
$( $( AstFragment :: $Kind( ast) => vis. $mut_visit_ast( ast) , ) ?) *
@@ -1782,11 +1777,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, OptExprTag> {
1782
1777
/// This struct is a hack to workaround unstable of `stmt_expr_attributes`.
1783
1778
/// It can be removed once that feature is stabilized.
1784
1779
struct MethodReceiverTag ;
1785
- impl DummyAstNode for MethodReceiverTag {
1786
- fn dummy ( ) -> MethodReceiverTag {
1787
- MethodReceiverTag
1788
- }
1789
- }
1780
+
1790
1781
impl InvocationCollectorNode for AstNodeWrapper < P < ast:: Expr > , MethodReceiverTag > {
1791
1782
type OutputTy = Self ;
1792
1783
const KIND : AstFragmentKind = AstFragmentKind :: MethodReceiverExpr ;
@@ -2135,42 +2126,39 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
2135
2126
}
2136
2127
}
2137
2128
2138
- fn visit_node < Node : InvocationCollectorNode < OutputTy = Node > + DummyAstNode > (
2129
+ fn visit_node < Node : InvocationCollectorNode < OutputTy = Node > > (
2139
2130
& mut self ,
2140
- node : & mut Node ,
2141
- ) {
2131
+ mut node : Node ,
2132
+ ) -> Node {
2142
2133
loop {
2143
- return match self . take_first_attr ( node) {
2134
+ return match self . take_first_attr ( & mut node) {
2144
2135
Some ( ( attr, pos, derives) ) => match attr. name ( ) {
2145
2136
Some ( sym:: cfg) => {
2146
2137
let span = attr. span ;
2147
- if self . expand_cfg_true ( node, attr, pos) . 0 {
2138
+ if self . expand_cfg_true ( & mut node, attr, pos) . 0 {
2148
2139
continue ;
2149
2140
}
2150
2141
2151
2142
node. expand_cfg_false ( self , pos, span) ;
2152
2143
continue ;
2153
2144
}
2154
2145
Some ( sym:: cfg_attr) => {
2155
- self . expand_cfg_attr ( node, & attr, pos) ;
2146
+ self . expand_cfg_attr ( & mut node, & attr, pos) ;
2156
2147
continue ;
2157
2148
}
2158
- _ => visit_clobber ( node, |node| {
2159
- self . collect_attr ( ( attr, pos, derives) , node. to_annotatable ( ) , Node :: KIND )
2160
- . make_ast :: < Node > ( )
2161
- } ) ,
2149
+ _ => self
2150
+ . collect_attr ( ( attr, pos, derives) , node. to_annotatable ( ) , Node :: KIND )
2151
+ . make_ast :: < Node > ( ) ,
2162
2152
} ,
2163
2153
None if node. is_mac_call ( ) => {
2164
- visit_clobber ( node, |node| {
2165
- // Do not clobber unless it's actually a macro (uncommon case).
2166
- let ( mac, attrs, _) = node. take_mac_call ( ) ;
2167
- self . check_attributes ( & attrs, & mac) ;
2168
- self . collect_bang ( mac, Node :: KIND ) . make_ast :: < Node > ( )
2169
- } )
2154
+ let ( mac, attrs, _) = node. take_mac_call ( ) ;
2155
+ self . check_attributes ( & attrs, & mac) ;
2156
+ self . collect_bang ( mac, Node :: KIND ) . make_ast :: < Node > ( )
2170
2157
}
2171
2158
None if node. delegation ( ) . is_some ( ) => unreachable ! ( ) ,
2172
2159
None => {
2173
- assign_id ! ( self , node. node_id_mut( ) , || node. walk( self ) )
2160
+ assign_id ! ( self , node. node_id_mut( ) , || node. walk( self ) ) ;
2161
+ node
2174
2162
}
2175
2163
} ;
2176
2164
}
@@ -2273,31 +2261,76 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
2273
2261
}
2274
2262
2275
2263
fn visit_crate ( & mut self , node : & mut ast:: Crate ) {
2276
- self . visit_node ( node)
2264
+ let krate = mem:: replace (
2265
+ node,
2266
+ ast:: Crate {
2267
+ attrs : Default :: default ( ) ,
2268
+ items : Default :: default ( ) ,
2269
+ spans : Default :: default ( ) ,
2270
+ id : DUMMY_NODE_ID ,
2271
+ is_placeholder : Default :: default ( ) ,
2272
+ } ,
2273
+ ) ;
2274
+ * node = self . visit_node ( krate) ;
2277
2275
}
2278
2276
2279
2277
fn visit_ty ( & mut self , node : & mut P < ast:: Ty > ) {
2280
- self . visit_node ( node)
2278
+ let ty = mem:: replace (
2279
+ node,
2280
+ P ( ast:: Ty {
2281
+ id : DUMMY_NODE_ID ,
2282
+ kind : TyKind :: Dummy ,
2283
+ span : Default :: default ( ) ,
2284
+ tokens : Default :: default ( ) ,
2285
+ } ) ,
2286
+ ) ;
2287
+ * node = self . visit_node ( ty) ;
2281
2288
}
2282
2289
2283
2290
fn visit_pat ( & mut self , node : & mut P < ast:: Pat > ) {
2284
- self . visit_node ( node)
2291
+ let pat = mem:: replace (
2292
+ node,
2293
+ P ( ast:: Pat {
2294
+ id : DUMMY_NODE_ID ,
2295
+ kind : PatKind :: Wild ,
2296
+ span : Default :: default ( ) ,
2297
+ tokens : Default :: default ( ) ,
2298
+ } ) ,
2299
+ ) ;
2300
+ * node = self . visit_node ( pat)
2285
2301
}
2286
2302
2287
2303
fn visit_expr ( & mut self , node : & mut P < ast:: Expr > ) {
2288
2304
// FIXME: Feature gating is performed inconsistently between `Expr` and `OptExpr`.
2289
2305
if let Some ( attr) = node. attrs . first ( ) {
2290
2306
self . cfg ( ) . maybe_emit_expr_attr_err ( attr) ;
2291
2307
}
2292
- self . visit_node ( node)
2308
+ let expr = mem:: replace (
2309
+ node,
2310
+ P ( ast:: Expr {
2311
+ id : DUMMY_NODE_ID ,
2312
+ kind : ExprKind :: Dummy ,
2313
+ span : Default :: default ( ) ,
2314
+ attrs : Default :: default ( ) ,
2315
+ tokens : Default :: default ( ) ,
2316
+ } ) ,
2317
+ ) ;
2318
+ * node = self . visit_node ( expr) ;
2293
2319
}
2294
2320
2295
2321
fn visit_method_receiver_expr ( & mut self , node : & mut P < ast:: Expr > ) {
2296
- visit_clobber ( node, |node| {
2297
- let mut wrapper = AstNodeWrapper :: new ( node, MethodReceiverTag ) ;
2298
- self . visit_node ( & mut wrapper) ;
2299
- wrapper. wrapped
2300
- } )
2322
+ let expr = mem:: replace (
2323
+ node,
2324
+ P ( ast:: Expr {
2325
+ id : DUMMY_NODE_ID ,
2326
+ kind : ExprKind :: Dummy ,
2327
+ span : Default :: default ( ) ,
2328
+ attrs : Default :: default ( ) ,
2329
+ tokens : Default :: default ( ) ,
2330
+ } ) ,
2331
+ ) ;
2332
+ let wrapper = AstNodeWrapper :: new ( expr, MethodReceiverTag ) ;
2333
+ * node = self . visit_node ( wrapper) . wrapped ;
2301
2334
}
2302
2335
2303
2336
fn filter_map_expr ( & mut self , node : P < ast:: Expr > ) -> Option < P < ast:: Expr > > {
0 commit comments