@@ -12,37 +12,23 @@ use rustc_span::hygiene::LocalExpnId;
12
12
use rustc_span:: symbol:: { Symbol , kw, sym} ;
13
13
use tracing:: debug;
14
14
15
- use crate :: { ImplTraitContext , InvocationParent , PendingAnonConstInfo , Resolver } ;
15
+ use crate :: { ImplTraitContext , InvocationParent , Resolver } ;
16
16
17
17
pub ( crate ) fn collect_definitions (
18
18
resolver : & mut Resolver < ' _ , ' _ > ,
19
19
fragment : & AstFragment ,
20
20
expansion : LocalExpnId ,
21
21
) {
22
- let InvocationParent { parent_def, pending_anon_const_info , impl_trait_context, in_attr } =
22
+ let InvocationParent { parent_def, impl_trait_context, in_attr } =
23
23
resolver. invocation_parents [ & expansion] ;
24
- let mut visitor = DefCollector {
25
- resolver,
26
- parent_def,
27
- pending_anon_const_info,
28
- expansion,
29
- impl_trait_context,
30
- in_attr,
31
- } ;
24
+ let mut visitor = DefCollector { resolver, parent_def, expansion, impl_trait_context, in_attr } ;
32
25
fragment. visit_with ( & mut visitor) ;
33
26
}
34
27
35
28
/// Creates `DefId`s for nodes in the AST.
36
29
struct DefCollector < ' a , ' ra , ' tcx > {
37
30
resolver : & ' a mut Resolver < ' ra , ' tcx > ,
38
31
parent_def : LocalDefId ,
39
- /// If we have an anon const that consists of a macro invocation, e.g. `Foo<{ m!() }>`,
40
- /// we need to wait until we know what the macro expands to before we create the def for
41
- /// the anon const. That's because we lower some anon consts into `hir::ConstArgKind::Path`,
42
- /// which don't have defs.
43
- ///
44
- /// See `Self::visit_anon_const()`.
45
- pending_anon_const_info : Option < PendingAnonConstInfo > ,
46
32
impl_trait_context : ImplTraitContext ,
47
33
in_attr : bool ,
48
34
expansion : LocalExpnId ,
@@ -110,61 +96,13 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
110
96
111
97
fn visit_macro_invoc ( & mut self , id : NodeId ) {
112
98
let id = id. placeholder_to_expn_id ( ) ;
113
- let pending_anon_const_info = self . pending_anon_const_info . take ( ) ;
114
99
let old_parent = self . resolver . invocation_parents . insert ( id, InvocationParent {
115
100
parent_def : self . parent_def ,
116
- pending_anon_const_info,
117
101
impl_trait_context : self . impl_trait_context ,
118
102
in_attr : self . in_attr ,
119
103
} ) ;
120
104
assert ! ( old_parent. is_none( ) , "parent `LocalDefId` is reset for an invocation" ) ;
121
105
}
122
-
123
- /// Determines whether the const argument `AnonConst` is a simple macro call, optionally
124
- /// surrounded with braces.
125
- ///
126
- /// If this const argument *is* a trivial macro call then the id for the macro call is
127
- /// returned along with the information required to build the anon const's def if
128
- /// the macro call expands to a non-trivial expression.
129
- fn is_const_arg_trivial_macro_expansion (
130
- & self ,
131
- anon_const : & ' a AnonConst ,
132
- ) -> Option < ( PendingAnonConstInfo , NodeId ) > {
133
- anon_const. value . optionally_braced_mac_call ( false ) . map ( |( block_was_stripped, id) | {
134
- (
135
- PendingAnonConstInfo {
136
- id : anon_const. id ,
137
- span : anon_const. value . span ,
138
- block_was_stripped,
139
- } ,
140
- id,
141
- )
142
- } )
143
- }
144
-
145
- /// Determines whether the expression `const_arg_sub_expr` is a simple macro call, sometimes
146
- /// surrounded with braces if a set of braces has not already been entered. This is required
147
- /// as `{ N }` is treated as equivalent to a bare parameter `N` whereas `{{ N }}` is treated as
148
- /// a real block expression and is lowered to an anonymous constant which is not allowed to use
149
- /// generic parameters.
150
- ///
151
- /// If this expression is a trivial macro call then the id for the macro call is
152
- /// returned along with the information required to build the anon const's def if
153
- /// the macro call expands to a non-trivial expression.
154
- fn is_const_arg_sub_expr_trivial_macro_expansion (
155
- & self ,
156
- const_arg_sub_expr : & ' a Expr ,
157
- ) -> Option < ( PendingAnonConstInfo , NodeId ) > {
158
- let pending_anon = self . pending_anon_const_info . unwrap_or_else ( ||
159
- panic ! ( "Checking expr is trivial macro call without having entered anon const: `{const_arg_sub_expr:?}`" ) ,
160
- ) ;
161
-
162
- const_arg_sub_expr. optionally_braced_mac_call ( pending_anon. block_was_stripped ) . map (
163
- |( block_was_stripped, id) | {
164
- ( PendingAnonConstInfo { block_was_stripped, ..pending_anon } , id)
165
- } ,
166
- )
167
- }
168
106
}
169
107
170
108
impl < ' a , ' ra , ' tcx > visit:: Visitor < ' a > for DefCollector < ' a , ' ra , ' tcx > {
@@ -376,78 +314,34 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
376
314
}
377
315
378
316
fn visit_anon_const ( & mut self , constant : & ' a AnonConst ) {
379
- // HACK(min_generic_const_args): don't create defs for anon consts if we think they will
380
- // later be turned into ConstArgKind::Path's. because this is before resolve is done, we
381
- // may accidentally identify a construction of a unit struct as a param and not create a
382
- // def. we'll then create a def later in ast lowering in this case. the parent of nested
383
- // items will be messed up, but that's ok because there can't be any if we're just looking
384
- // for bare idents.
385
-
386
- if let Some ( ( pending_anon, macro_invoc) ) =
387
- self . is_const_arg_trivial_macro_expansion ( constant)
388
- {
389
- self . pending_anon_const_info = Some ( pending_anon) ;
390
- return self . visit_macro_invoc ( macro_invoc) ;
391
- } else if constant. value . is_potential_trivial_const_arg ( true ) {
392
- return visit:: walk_anon_const ( self , constant) ;
393
- }
394
-
395
- let def = self . create_def ( constant. id , kw:: Empty , DefKind :: AnonConst , constant. value . span ) ;
396
- self . with_parent ( def, |this| visit:: walk_anon_const ( this, constant) ) ;
317
+ let parent =
318
+ self . create_def ( constant. id , kw:: Empty , DefKind :: AnonConst , constant. value . span ) ;
319
+ self . with_parent ( parent, |this| visit:: walk_anon_const ( this, constant) ) ;
397
320
}
398
321
399
322
fn visit_expr ( & mut self , expr : & ' a Expr ) {
400
- // If we're visiting the expression of a const argument that was a macro call then
401
- // check if it is *still* unknown whether it is a trivial const arg or not. If so
402
- // recurse into the macro call and delay creating the anon const def until expansion.
403
- if self . pending_anon_const_info . is_some ( )
404
- && let Some ( ( pending_anon, macro_invoc) ) =
405
- self . is_const_arg_sub_expr_trivial_macro_expansion ( expr)
406
- {
407
- self . pending_anon_const_info = Some ( pending_anon) ;
408
- return self . visit_macro_invoc ( macro_invoc) ;
409
- }
410
-
411
- // See self.pending_anon_const_info for explanation
412
- let parent_def = self
413
- . pending_anon_const_info
414
- . take ( )
415
- // If we already stripped away a set of braces then do not do it again when determining
416
- // if the macro expanded to a trivial const arg. This arises in cases such as:
417
- // `Foo<{ bar!() }>` where `bar!()` expands to `{ N }`. This should not be considered a
418
- // trivial const argument even though `{ N }` by itself *is*.
419
- . filter ( |pending_anon| {
420
- !expr. is_potential_trivial_const_arg ( !pending_anon. block_was_stripped )
421
- } )
422
- . map ( |pending_anon| {
423
- self . create_def ( pending_anon. id , kw:: Empty , DefKind :: AnonConst , pending_anon. span )
424
- } )
425
- . unwrap_or ( self . parent_def ) ;
426
-
427
- self . with_parent ( parent_def, |this| {
428
- let parent_def = match expr. kind {
429
- ExprKind :: MacCall ( ..) => return this. visit_macro_invoc ( expr. id ) ,
430
- ExprKind :: Closure ( ..) | ExprKind :: Gen ( ..) => {
431
- this. create_def ( expr. id , kw:: Empty , DefKind :: Closure , expr. span )
432
- }
433
- ExprKind :: ConstBlock ( ref constant) => {
434
- for attr in & expr. attrs {
435
- visit:: walk_attribute ( this, attr) ;
436
- }
437
- let def = this. create_def (
438
- constant. id ,
439
- kw:: Empty ,
440
- DefKind :: InlineConst ,
441
- constant. value . span ,
442
- ) ;
443
- this. with_parent ( def, |this| visit:: walk_anon_const ( this, constant) ) ;
444
- return ;
323
+ let parent_def = match expr. kind {
324
+ ExprKind :: MacCall ( ..) => return self . visit_macro_invoc ( expr. id ) ,
325
+ ExprKind :: Closure ( ..) | ExprKind :: Gen ( ..) => {
326
+ self . create_def ( expr. id , kw:: Empty , DefKind :: Closure , expr. span )
327
+ }
328
+ ExprKind :: ConstBlock ( ref constant) => {
329
+ for attr in & expr. attrs {
330
+ visit:: walk_attribute ( self , attr) ;
445
331
}
446
- _ => this. parent_def ,
447
- } ;
332
+ let def = self . create_def (
333
+ constant. id ,
334
+ kw:: Empty ,
335
+ DefKind :: InlineConst ,
336
+ constant. value . span ,
337
+ ) ;
338
+ self . with_parent ( def, |this| visit:: walk_anon_const ( this, constant) ) ;
339
+ return ;
340
+ }
341
+ _ => self . parent_def ,
342
+ } ;
448
343
449
- this. with_parent ( parent_def, |this| visit:: walk_expr ( this, expr) )
450
- } )
344
+ self . with_parent ( parent_def, |this| visit:: walk_expr ( this, expr) )
451
345
}
452
346
453
347
fn visit_ty ( & mut self , ty : & ' a Ty ) {
0 commit comments