@@ -18,6 +18,7 @@ use super::{bad_placeholder, is_suggestable_infer_ty};
18
18
/// Computes the relevant generic parameter for a potential generic const argument.
19
19
///
20
20
/// This should be called using the query `tcx.opt_const_param_of`.
21
+ #[ instrument( level = "debug" , skip( tcx) ) ]
21
22
pub ( super ) fn opt_const_param_of ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> Option < DefId > {
22
23
// FIXME(generic_arg_infer): allow for returning DefIds of inference of
23
24
// GenericArg::Infer below. This may require a change where GenericArg::Infer has some flag
@@ -29,7 +30,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
29
30
let parent_node_id = tcx. hir ( ) . get_parent_node ( hir_id) ;
30
31
let parent_node = tcx. hir ( ) . get ( parent_node_id) ;
31
32
32
- match parent_node {
33
+ let ( generics , arg_idx ) = match parent_node {
33
34
// This match arm is for when the def_id appears in a GAT whose
34
35
// path can't be resolved without typechecking e.g.
35
36
//
@@ -75,27 +76,22 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
75
76
. and_then ( |args| {
76
77
args. args
77
78
. iter ( )
78
- . filter ( |arg| arg. is_const ( ) )
79
+ . filter ( |arg| ! matches ! ( arg, GenericArg :: Lifetime ( _ ) ) )
79
80
. position ( |arg| arg. id ( ) == hir_id)
80
81
} )
81
82
. unwrap_or_else ( || {
82
83
bug ! ( "no arg matching AnonConst in segment" ) ;
83
84
} ) ;
84
85
85
- return generics
86
- . params
87
- . iter ( )
88
- . filter ( |param| matches ! ( param. kind, ty:: GenericParamDefKind :: Const { .. } ) )
89
- . nth ( arg_index)
90
- . map ( |param| param. def_id ) ;
86
+ ( generics, arg_index)
87
+ } else {
88
+ // I dont think it's possible to reach this but I'm not 100% sure - BoxyUwU
89
+ tcx. sess . delay_span_bug (
90
+ tcx. def_span ( def_id) ,
91
+ "unexpected non-GAT usage of an anon const" ,
92
+ ) ;
93
+ return None ;
91
94
}
92
-
93
- // I dont think it's possible to reach this but I'm not 100% sure - BoxyUwU
94
- tcx. sess . delay_span_bug (
95
- tcx. def_span ( def_id) ,
96
- "unexpected non-GAT usage of an anon const" ,
97
- ) ;
98
- return None ;
99
95
}
100
96
Node :: Expr ( & Expr {
101
97
kind :
@@ -113,19 +109,14 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
113
109
. and_then ( |args| {
114
110
args. args
115
111
. iter ( )
116
- . filter ( |arg| arg. is_const ( ) )
112
+ . filter ( |arg| ! matches ! ( arg, GenericArg :: Lifetime ( _ ) ) )
117
113
. position ( |arg| arg. id ( ) == hir_id)
118
114
} )
119
115
. unwrap_or_else ( || {
120
116
bug ! ( "no arg matching AnonConst in segment" ) ;
121
117
} ) ;
122
118
123
- tcx. generics_of ( type_dependent_def)
124
- . params
125
- . iter ( )
126
- . filter ( |param| matches ! ( param. kind, ty:: GenericParamDefKind :: Const { .. } ) )
127
- . nth ( idx)
128
- . map ( |param| param. def_id )
119
+ ( tcx. generics_of ( type_dependent_def) , idx)
129
120
}
130
121
131
122
Node :: Ty ( & Ty { kind : TyKind :: Path ( _) , .. } )
@@ -178,7 +169,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
178
169
. filter_map ( |seg| seg. args . map ( |args| ( args. args , seg) ) )
179
170
. find_map ( |( args, seg) | {
180
171
args. iter ( )
181
- . filter ( |arg| arg. is_const ( ) )
172
+ . filter ( |arg| ! matches ! ( arg, GenericArg :: Lifetime ( _ ) ) )
182
173
. position ( |arg| arg. id ( ) == hir_id)
183
174
. map ( |index| ( index, seg) )
184
175
} ) ;
@@ -238,15 +229,29 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
238
229
}
239
230
} ;
240
231
241
- generics
242
- . params
243
- . iter ( )
244
- . filter ( |param| matches ! ( param. kind, ty:: GenericParamDefKind :: Const { .. } ) )
245
- . nth ( arg_index)
246
- . map ( |param| param. def_id )
232
+ ( generics, arg_index)
247
233
}
248
- _ => None ,
249
- }
234
+ _ => return None ,
235
+ } ;
236
+
237
+ debug ! ( ?parent_node) ;
238
+ debug ! ( ?generics) ;
239
+ debug ! ( ?arg_idx) ;
240
+ generics
241
+ . params
242
+ . iter ( )
243
+ . filter ( |param| !matches ! ( param. kind, ty:: GenericParamDefKind :: Lifetime { .. } ) )
244
+ . nth ( match generics. has_self && generics. parent . is_none ( ) {
245
+ true => arg_idx + 1 ,
246
+ false => arg_idx,
247
+ } )
248
+ . and_then ( |param| match param. kind {
249
+ ty:: GenericParamDefKind :: Const { .. } => {
250
+ debug ! ( ?param) ;
251
+ Some ( param. def_id )
252
+ }
253
+ _ => None ,
254
+ } )
250
255
} else {
251
256
None
252
257
}
0 commit comments