@@ -11,6 +11,7 @@ use rustc_session::lint;
11
11
use rustc_span:: symbol:: { kw, Symbol } ;
12
12
use rustc_span:: Span ;
13
13
14
+ #[ instrument( level = "debug" , skip( tcx) ) ]
14
15
pub ( super ) fn generics_of ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> ty:: Generics {
15
16
use rustc_hir:: * ;
16
17
@@ -66,7 +67,22 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
66
67
// FIXME(#43408) always enable this once `lazy_normalization` is
67
68
// stable enough and does not need a feature gate anymore.
68
69
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) ;
70
86
71
87
let mut in_param_ty = false ;
72
88
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 {
121
137
//
122
138
// This has some implications for how we get the predicates available to the anon const
123
139
// 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 ) ;
125
141
let param_def_idx = generics. param_def_id_to_index [ & param_id. to_def_id ( ) ] ;
126
142
// In the above example this would be .params[..N#0]
127
143
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 {
147
163
//
148
164
// Note that we do not supply the parent generics when using
149
165
// `min_const_generics`.
150
- Some ( parent_def_id . to_def_id ( ) )
166
+ Some ( parent_did )
151
167
}
152
168
} else {
153
169
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 {
159
175
Node :: Expr ( Expr { kind : ExprKind :: Repeat ( _, constant) , .. } )
160
176
if constant. hir_id ( ) == hir_id =>
161
177
{
162
- Some ( parent_def_id . to_def_id ( ) )
178
+ Some ( parent_did )
163
179
}
164
180
// Exclude `GlobalAsm` here which cannot have generics.
165
181
Node :: Expr ( & Expr { kind : ExprKind :: InlineAsm ( asm) , .. } )
@@ -171,7 +187,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
171
187
_ => false ,
172
188
} ) =>
173
189
{
174
- Some ( parent_def_id . to_def_id ( ) )
190
+ Some ( parent_did )
175
191
}
176
192
_ => None ,
177
193
}
0 commit comments