@@ -29,19 +29,32 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
29
29
let parent_node = tcx. hir ( ) . get ( parent_node_id) ;
30
30
31
31
match parent_node {
32
+ // This matches on types who's paths couldn't be resolved without typeck'ing e.g.
33
+ //
34
+ // trait Foo {
35
+ // type Assoc<const N1: usize>;;
36
+ // fn foo() -> Self::Assoc<3>;
37
+ // // note: if the def_id argument is the 3 then in this example
38
+ // // parent_node would be the node for Self::Assoc<_>
39
+ // }
40
+ // We didnt write <Self as Foo>::Assoc so the Self::Assoc<_> is lowered to QPath::TypeRelative.
41
+ // I believe this match arm is only needed for GAT but I am not 100% sure - BoxyUwU
32
42
Node :: Ty ( hir_ty @ Ty { kind : TyKind :: Path ( QPath :: TypeRelative ( _, segment) ) , .. } ) => {
33
- let id = tcx
43
+ // Walk up from the parent_node to find an item so that
44
+ // we can resolve the relative path to an actual associated type
45
+ let item_hir_id = tcx
34
46
. hir ( )
35
47
. parent_iter ( hir_id)
36
48
. filter ( |( _, node) | matches ! ( node, Node :: Item ( _) ) )
37
49
. map ( |( id, _) | id)
38
50
. next ( )
39
51
. unwrap ( ) ;
40
-
41
- let item_did = tcx. hir ( ) . local_def_id ( id) . to_def_id ( ) ;
52
+ let item_did = tcx. hir ( ) . local_def_id ( item_hir_id) . to_def_id ( ) ;
42
53
let item_ctxt = & ItemCtxt :: new ( tcx, item_did) as & dyn crate :: astconv:: AstConv < ' _ > ;
43
- let ty = item_ctxt. ast_ty_to_ty ( hir_ty) ;
44
54
55
+ // This ty will be the actual associated type so that we can
56
+ // go through its generics to find which param our def_id corresponds to
57
+ let ty = item_ctxt. ast_ty_to_ty ( hir_ty) ;
45
58
if let ty:: Projection ( projection) = ty. kind ( ) {
46
59
let generics = tcx. generics_of ( projection. item_def_id ) ;
47
60
@@ -65,6 +78,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
65
78
. map ( |param| param. def_id ) ;
66
79
}
67
80
81
+ // I dont think it's possible to reach this but I'm not 100% sure - BoxyUwU
68
82
tcx. sess . delay_span_bug (
69
83
tcx. def_span ( def_id) ,
70
84
"unexpected non-GAT usage of an anon const" ,
0 commit comments