Skip to content

Commit 2c4337a

Browse files
committed
Comments :3
1 parent 1d9ac3c commit 2c4337a

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

compiler/rustc_typeck/src/collect/type_of.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,32 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
2929
let parent_node = tcx.hir().get(parent_node_id);
3030

3131
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
3242
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
3446
.hir()
3547
.parent_iter(hir_id)
3648
.filter(|(_, node)| matches!(node, Node::Item(_)))
3749
.map(|(id, _)| id)
3850
.next()
3951
.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();
4253
let item_ctxt = &ItemCtxt::new(tcx, item_did) as &dyn crate::astconv::AstConv<'_>;
43-
let ty = item_ctxt.ast_ty_to_ty(hir_ty);
4454

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);
4558
if let ty::Projection(projection) = ty.kind() {
4659
let generics = tcx.generics_of(projection.item_def_id);
4760

@@ -65,6 +78,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
6578
.map(|param| param.def_id);
6679
}
6780

81+
// I dont think it's possible to reach this but I'm not 100% sure - BoxyUwU
6882
tcx.sess.delay_span_bug(
6983
tcx.def_span(def_id),
7084
"unexpected non-GAT usage of an anon const",

0 commit comments

Comments
 (0)