1
- use std:: ops:: Deref ;
1
+ use std:: ops:: { Deref , DerefMut } ;
2
2
use std:: path:: PathBuf ;
3
3
use std:: rc:: Rc ;
4
4
use std:: { iter, mem} ;
@@ -594,6 +594,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
594
594
cx : self . cx ,
595
595
invocations : Vec :: new ( ) ,
596
596
monotonic : self . monotonic ,
597
+ expr_under_let_init_else : false ,
597
598
} ;
598
599
fragment. mut_visit_with ( & mut collector) ;
599
600
fragment. add_placeholders ( extra_placeholders) ;
@@ -1771,6 +1772,7 @@ struct InvocationCollector<'a, 'b> {
1771
1772
cx : & ' a mut ExtCtxt < ' b > ,
1772
1773
invocations : Vec < ( Invocation , Option < Lrc < SyntaxExtension > > ) > ,
1773
1774
monotonic : bool ,
1775
+ expr_under_let_init_else : bool ,
1774
1776
}
1775
1777
1776
1778
impl < ' a , ' b > InvocationCollector < ' a , ' b > {
@@ -2167,6 +2169,35 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
2167
2169
self . flat_map_node ( node)
2168
2170
}
2169
2171
2172
+ fn visit_local ( & mut self , local : & mut P < rustc_ast:: Local > ) {
2173
+ let ast:: Local { id, pat, ty, kind, span, colon_sp, attrs, tokens } = local. deref_mut ( ) ;
2174
+ self . visit_id ( id) ;
2175
+ for attr in attrs. iter_mut ( ) {
2176
+ self . visit_attribute ( attr) ;
2177
+ }
2178
+ self . visit_pat ( pat) ;
2179
+ if let Some ( ty) = ty {
2180
+ self . visit_ty ( ty) ;
2181
+ }
2182
+ match kind {
2183
+ ast:: LocalKind :: Decl => { }
2184
+ ast:: LocalKind :: Init ( init) => {
2185
+ self . visit_expr ( init) ;
2186
+ }
2187
+ ast:: LocalKind :: InitElse ( init, els) => {
2188
+ self . expr_under_let_init_else = true ;
2189
+ self . visit_expr ( init) ;
2190
+ self . expr_under_let_init_else = false ;
2191
+ self . visit_block ( els) ;
2192
+ }
2193
+ }
2194
+ visit_lazy_tts ( self , tokens) ;
2195
+ if let Some ( sp) = colon_sp {
2196
+ self . visit_span ( sp) ;
2197
+ }
2198
+ self . visit_span ( span) ;
2199
+ }
2200
+
2170
2201
fn visit_crate ( & mut self , node : & mut ast:: Crate ) {
2171
2202
self . visit_node ( node)
2172
2203
}
@@ -2180,6 +2211,13 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
2180
2211
}
2181
2212
2182
2213
fn visit_expr ( & mut self , node : & mut P < ast:: Expr > ) {
2214
+ if self . expr_under_let_init_else
2215
+ && let ast:: ExprKind :: Paren ( paren) = & node. kind
2216
+ && let ast:: ExprKind :: MacCall ( mac) = & paren. kind
2217
+ && mac. args . delim == Delimiter :: Brace
2218
+ {
2219
+ self . cx . resolver . add_necessary_parens ( node. span ) ;
2220
+ }
2183
2221
// FIXME: Feature gating is performed inconsistently between `Expr` and `OptExpr`.
2184
2222
if let Some ( attr) = node. attrs . first ( ) {
2185
2223
self . cfg ( ) . maybe_emit_expr_attr_err ( attr) ;
0 commit comments