@@ -11,6 +11,7 @@ use rustc_session::lint;
1111use  rustc_span:: symbol:: { kw,  Symbol } ; 
1212use  rustc_span:: Span ; 
1313
14+ #[ instrument( level = "debug" ,  skip( tcx) ) ]  
1415pub ( super )  fn  generics_of ( tcx :  TyCtxt < ' _ > ,  def_id :  LocalDefId )  -> ty:: Generics  { 
1516    use  rustc_hir:: * ; 
1617
@@ -66,7 +67,22 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
6667        // FIXME(#43408) always enable this once `lazy_normalization` is 
6768        // stable enough and does not need a feature gate anymore. 
6869        Node :: AnonConst ( _)  => { 
69-             let  parent_def_id = tcx. hir ( ) . get_parent_item ( hir_id) ; 
70+             let  parent_did = tcx. parent ( def_id. to_def_id ( ) ) ; 
71+ 
72+             // We don't do this unconditionally because the `DefId` parent of an anon const 
73+             // might be an implicitly created closure during `async fn` desugaring. This would 
74+             // have the wrong generics. 
75+             // 
76+             // i.e. `async fn foo<'a>() { let a = [(); { 1 + 2 }]; bar().await() }` 
77+             // would implicitly have a closure in its body that would be the parent of 
78+             // the `{ 1 + 2 }` anon const. This closure's generics is simply a witness 
79+             // instead of `['a]`. 
80+             let  parent_did = if  let  DefKind :: AnonConst  = tcx. def_kind ( parent_did)  { 
81+                 parent_did
82+             }  else  { 
83+                 tcx. hir ( ) . get_parent_item ( hir_id) . to_def_id ( ) 
84+             } ; 
85+             debug ! ( ?parent_did) ; 
7086
7187            let  mut  in_param_ty = false ; 
7288            for  ( _parent,  node)  in  tcx. hir ( ) . parent_iter ( hir_id)  { 
@@ -121,7 +137,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
121137                    // 
122138                    // This has some implications for how we get the predicates available to the anon const 
123139                    // see `explicit_predicates_of` for more information on this 
124-                     let  generics = tcx. generics_of ( parent_def_id . to_def_id ( ) ) ; 
140+                     let  generics = tcx. generics_of ( parent_did ) ; 
125141                    let  param_def_idx = generics. param_def_id_to_index [ & param_id. to_def_id ( ) ] ; 
126142                    // In the above example this would be .params[..N#0] 
127143                    let  own_params = generics. params_to ( param_def_idx as  usize ,  tcx) . to_owned ( ) ; 
@@ -147,7 +163,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
147163                    // 
148164                    // Note that we do not supply the parent generics when using 
149165                    // `min_const_generics`. 
150-                     Some ( parent_def_id . to_def_id ( ) ) 
166+                     Some ( parent_did ) 
151167                } 
152168            }  else  { 
153169                let  parent_node = tcx. parent_hir_node ( hir_id) ; 
@@ -159,7 +175,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
159175                    Node :: Expr ( Expr  {  kind :  ExprKind :: Repeat ( _,  constant) ,  .. } ) 
160176                        if  constant. hir_id ( )  == hir_id =>
161177                    { 
162-                         Some ( parent_def_id . to_def_id ( ) ) 
178+                         Some ( parent_did ) 
163179                    } 
164180                    // Exclude `GlobalAsm` here which cannot have generics. 
165181                    Node :: Expr ( & Expr  {  kind :  ExprKind :: InlineAsm ( asm) ,  .. } ) 
@@ -171,7 +187,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
171187                            _ => false , 
172188                        } )  =>
173189                    { 
174-                         Some ( parent_def_id . to_def_id ( ) ) 
190+                         Some ( parent_did ) 
175191                    } 
176192                    _ => None , 
177193                } 
0 commit comments