@@ -3,7 +3,7 @@ use std::rc::Rc;
33use std:: sync:: Arc ;
44use std:: { iter, mem} ;
55
6- use rustc_ast as ast;
6+ use rustc_ast:: { self as ast, DUMMY_NODE_ID } ;
77use rustc_ast:: mut_visit:: * ;
88use rustc_ast:: ptr:: P ;
99use rustc_ast:: tokenstream:: TokenStream ;
@@ -131,13 +131,9 @@ macro_rules! ast_fragments {
131131 pub ( crate ) fn mut_visit_with<F : MutVisitor >( & mut self , vis: & mut F ) {
132132 match self {
133133 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- } ) ;
134+ if let Some ( expr) = opt_expr. take( ) {
135+ * opt_expr = vis. filter_map_expr( expr)
136+ }
141137 }
142138 AstFragment :: MethodReceiverExpr ( expr) => vis. visit_method_receiver_expr( expr) ,
143139 $( $( AstFragment :: $Kind( ast) => vis. $mut_visit_ast( ast) , ) ?) *
@@ -1782,11 +1778,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, OptExprTag> {
17821778/// This struct is a hack to workaround unstable of `stmt_expr_attributes`.
17831779/// It can be removed once that feature is stabilized.
17841780struct MethodReceiverTag ;
1785- impl DummyAstNode for MethodReceiverTag {
1786- fn dummy ( ) -> MethodReceiverTag {
1787- MethodReceiverTag
1788- }
1789- }
1781+
17901782impl InvocationCollectorNode for AstNodeWrapper < P < ast:: Expr > , MethodReceiverTag > {
17911783 type OutputTy = Self ;
17921784 const KIND : AstFragmentKind = AstFragmentKind :: MethodReceiverExpr ;
@@ -2135,42 +2127,40 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
21352127 }
21362128 }
21372129
2138- fn visit_node < Node : InvocationCollectorNode < OutputTy = Node > + DummyAstNode > (
2130+ fn visit_node < Node : InvocationCollectorNode < OutputTy = Node > > (
21392131 & mut self ,
2140- node : & mut Node ,
2141- ) {
2132+ mut node : Node ,
2133+ ) -> Node {
21422134 loop {
2143- return match self . take_first_attr ( node) {
2135+ return match self . take_first_attr ( & mut node) {
21442136 Some ( ( attr, pos, derives) ) => match attr. name ( ) {
21452137 Some ( sym:: cfg) => {
21462138 let span = attr. span ;
2147- if self . expand_cfg_true ( node, attr, pos) . 0 {
2139+ if self . expand_cfg_true ( & mut node, attr, pos) . 0 {
21482140 continue ;
21492141 }
21502142
21512143 node. expand_cfg_false ( self , pos, span) ;
21522144 continue ;
21532145 }
21542146 Some ( sym:: cfg_attr) => {
2155- self . expand_cfg_attr ( node, & attr, pos) ;
2147+ self . expand_cfg_attr ( & mut node, & attr, pos) ;
21562148 continue ;
21572149 }
2158- _ => visit_clobber ( node , |node| {
2150+ _ => {
21592151 self . collect_attr ( ( attr, pos, derives) , node. to_annotatable ( ) , Node :: KIND )
2160- . make_ast :: < Node > ( )
2161- } ) ,
2152+ . make_ast :: < Node > ( )
2153+ }
21622154 } ,
21632155 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- } )
2156+ let ( mac, attrs, _) = node. take_mac_call ( ) ;
2157+ self . check_attributes ( & attrs, & mac) ;
2158+ self . collect_bang ( mac, Node :: KIND ) . make_ast :: < Node > ( )
21702159 }
21712160 None if node. delegation ( ) . is_some ( ) => unreachable ! ( ) ,
21722161 None => {
2173- assign_id ! ( self , node. node_id_mut( ) , || node. walk( self ) )
2162+ assign_id ! ( self , node. node_id_mut( ) , || node. walk( self ) ) ;
2163+ node
21742164 }
21752165 } ;
21762166 }
@@ -2273,31 +2263,61 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
22732263 }
22742264
22752265 fn visit_crate ( & mut self , node : & mut ast:: Crate ) {
2276- self . visit_node ( node)
2266+ let krate = mem:: replace ( node, 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+ * node = self . visit_node ( krate) ;
22772274 }
22782275
22792276 fn visit_ty ( & mut self , node : & mut P < ast:: Ty > ) {
2280- self . visit_node ( node)
2277+ let ty = mem:: replace ( node, P ( ast:: Ty {
2278+ id : DUMMY_NODE_ID ,
2279+ kind : TyKind :: Dummy ,
2280+ span : Default :: default ( ) ,
2281+ tokens : Default :: default ( ) ,
2282+ } ) ) ;
2283+ * node = self . visit_node ( ty) ;
22812284 }
22822285
22832286 fn visit_pat ( & mut self , node : & mut P < ast:: Pat > ) {
2284- self . visit_node ( node)
2287+ let pat = mem:: replace ( node, P ( ast:: Pat {
2288+ id : DUMMY_NODE_ID ,
2289+ kind : PatKind :: Wild ,
2290+ span : Default :: default ( ) ,
2291+ tokens : Default :: default ( ) ,
2292+ } ) ) ;
2293+ * node = self . visit_node ( pat)
22852294 }
22862295
22872296 fn visit_expr ( & mut self , node : & mut P < ast:: Expr > ) {
22882297 // FIXME: Feature gating is performed inconsistently between `Expr` and `OptExpr`.
22892298 if let Some ( attr) = node. attrs . first ( ) {
22902299 self . cfg ( ) . maybe_emit_expr_attr_err ( attr) ;
22912300 }
2292- self . visit_node ( node)
2301+ let expr = mem:: replace ( node, P ( ast:: Expr {
2302+ id : DUMMY_NODE_ID ,
2303+ kind : ExprKind :: Dummy ,
2304+ span : Default :: default ( ) ,
2305+ attrs : Default :: default ( ) ,
2306+ tokens : Default :: default ( ) ,
2307+ } ) ) ;
2308+ * node = self . visit_node ( expr) ;
22932309 }
22942310
22952311 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- } )
2312+ let expr = mem:: replace ( node, P ( ast:: Expr {
2313+ id : DUMMY_NODE_ID ,
2314+ kind : ExprKind :: Dummy ,
2315+ span : Default :: default ( ) ,
2316+ attrs : Default :: default ( ) ,
2317+ tokens : Default :: default ( ) ,
2318+ } ) ) ;
2319+ let wrapper = AstNodeWrapper :: new ( expr, MethodReceiverTag ) ;
2320+ * node = self . visit_node ( wrapper) . wrapped ;
23012321 }
23022322
23032323 fn filter_map_expr ( & mut self , node : P < ast:: Expr > ) -> Option < P < ast:: Expr > > {
0 commit comments