@@ -3,15 +3,14 @@ use std::rc::Rc;
33use std:: sync:: Arc ;
44use std:: { iter, mem} ;
55
6- use rustc_ast as ast;
76use rustc_ast:: mut_visit:: * ;
87use rustc_ast:: ptr:: P ;
98use rustc_ast:: tokenstream:: TokenStream ;
109use rustc_ast:: visit:: { self , AssocCtxt , Visitor , VisitorResult , try_visit, walk_list} ;
1110use 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,
1514} ;
1615use rustc_ast_pretty:: pprust;
1716use rustc_data_structures:: flat_map_in_place:: FlatMapInPlace ;
@@ -131,13 +130,9 @@ macro_rules! ast_fragments {
131130 pub ( crate ) fn mut_visit_with<F : MutVisitor >( & mut self , vis: & mut F ) {
132131 match self {
133132 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+ }
141136 }
142137 AstFragment :: MethodReceiverExpr ( expr) => vis. visit_method_receiver_expr( expr) ,
143138 $( $( AstFragment :: $Kind( ast) => vis. $mut_visit_ast( ast) , ) ?) *
@@ -1782,11 +1777,7 @@ impl InvocationCollectorNode for AstNodeWrapper<P<ast::Expr>, OptExprTag> {
17821777/// This struct is a hack to workaround unstable of `stmt_expr_attributes`.
17831778/// It can be removed once that feature is stabilized.
17841779struct MethodReceiverTag ;
1785- impl DummyAstNode for MethodReceiverTag {
1786- fn dummy ( ) -> MethodReceiverTag {
1787- MethodReceiverTag
1788- }
1789- }
1780+
17901781impl InvocationCollectorNode for AstNodeWrapper < P < ast:: Expr > , MethodReceiverTag > {
17911782 type OutputTy = Self ;
17921783 const KIND : AstFragmentKind = AstFragmentKind :: MethodReceiverExpr ;
@@ -2135,42 +2126,39 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
21352126 }
21362127 }
21372128
2138- fn visit_node < Node : InvocationCollectorNode < OutputTy = Node > + DummyAstNode > (
2129+ fn visit_node < Node : InvocationCollectorNode < OutputTy = Node > > (
21392130 & mut self ,
2140- node : & mut Node ,
2141- ) {
2131+ mut node : Node ,
2132+ ) -> Node {
21422133 loop {
2143- return match self . take_first_attr ( node) {
2134+ return match self . take_first_attr ( & mut node) {
21442135 Some ( ( attr, pos, derives) ) => match attr. name ( ) {
21452136 Some ( sym:: cfg) => {
21462137 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 {
21482139 continue ;
21492140 }
21502141
21512142 node. expand_cfg_false ( self , pos, span) ;
21522143 continue ;
21532144 }
21542145 Some ( sym:: cfg_attr) => {
2155- self . expand_cfg_attr ( node, & attr, pos) ;
2146+ self . expand_cfg_attr ( & mut node, & attr, pos) ;
21562147 continue ;
21572148 }
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 > ( ) ,
21622152 } ,
21632153 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 > ( )
21702157 }
21712158 None if node. delegation ( ) . is_some ( ) => unreachable ! ( ) ,
21722159 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
21742162 }
21752163 } ;
21762164 }
@@ -2273,31 +2261,64 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
22732261 }
22742262
22752263 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) ;
22772275 }
22782276
22792277 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) ;
22812288 }
22822289
22832290 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)
22852301 }
22862302
22872303 fn visit_expr ( & mut self , node : & mut P < ast:: Expr > ) {
22882304 // FIXME: Feature gating is performed inconsistently between `Expr` and `OptExpr`.
22892305 if let Some ( attr) = node. attrs . first ( ) {
22902306 self . cfg ( ) . maybe_emit_expr_attr_err ( attr) ;
22912307 }
2292- self . visit_node ( node)
2308+ let expr = mem:: replace (
2309+ node,
2310+ ast:: Expr :: dummy ( ) ,
2311+ ) ;
2312+ * node = self . visit_node ( expr) ;
22932313 }
22942314
22952315 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- } )
2316+ let expr = mem:: replace (
2317+ node,
2318+ ast:: Expr :: dummy ( ) ,
2319+ ) ;
2320+ let wrapper = AstNodeWrapper :: new ( expr, MethodReceiverTag ) ;
2321+ * node = self . visit_node ( wrapper) . wrapped ;
23012322 }
23022323
23032324 fn filter_map_expr ( & mut self , node : P < ast:: Expr > ) -> Option < P < ast:: Expr > > {
0 commit comments