@@ -12,37 +12,23 @@ use rustc_span::hygiene::LocalExpnId;
1212use rustc_span:: symbol:: { Symbol , kw, sym} ;
1313use tracing:: debug;
1414
15- use crate :: { ImplTraitContext , InvocationParent , PendingAnonConstInfo , Resolver } ;
15+ use crate :: { ImplTraitContext , InvocationParent , Resolver } ;
1616
1717pub ( crate ) fn collect_definitions (
1818 resolver : & mut Resolver < ' _ , ' _ > ,
1919 fragment : & AstFragment ,
2020 expansion : LocalExpnId ,
2121) {
22- let InvocationParent { parent_def, pending_anon_const_info , impl_trait_context, in_attr } =
22+ let InvocationParent { parent_def, impl_trait_context, in_attr } =
2323 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 } ;
3225 fragment. visit_with ( & mut visitor) ;
3326}
3427
3528/// Creates `DefId`s for nodes in the AST.
3629struct DefCollector < ' a , ' ra , ' tcx > {
3730 resolver : & ' a mut Resolver < ' ra , ' tcx > ,
3831 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 > ,
4632 impl_trait_context : ImplTraitContext ,
4733 in_attr : bool ,
4834 expansion : LocalExpnId ,
@@ -110,61 +96,13 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
11096
11197 fn visit_macro_invoc ( & mut self , id : NodeId ) {
11298 let id = id. placeholder_to_expn_id ( ) ;
113- let pending_anon_const_info = self . pending_anon_const_info . take ( ) ;
11499 let old_parent = self . resolver . invocation_parents . insert ( id, InvocationParent {
115100 parent_def : self . parent_def ,
116- pending_anon_const_info,
117101 impl_trait_context : self . impl_trait_context ,
118102 in_attr : self . in_attr ,
119103 } ) ;
120104 assert ! ( old_parent. is_none( ) , "parent `LocalDefId` is reset for an invocation" ) ;
121105 }
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- }
168106}
169107
170108impl < ' 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> {
376314 }
377315
378316 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) ) ;
397320 }
398321
399322 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) ;
445331 }
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+ } ;
448343
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) )
451345 }
452346
453347 fn visit_ty ( & mut self , ty : & ' a Ty ) {
0 commit comments