Skip to content

Commit 7ca96ed

Browse files
committed
rewrite the comments
1 parent 0ffa2da commit 7ca96ed

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

Diff for: compiler/rustc_typeck/src/collect/type_of.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,28 @@ 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.
32+
// This match arm is for when the def_id appears in a GAT whose
33+
// path can't be resolved without typechecking e.g.
3334
//
3435
// trait Foo {
35-
// type Assoc<const N1: usize>;
36+
// type Assoc<const N: usize>;
3637
// 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<_>
3938
// }
40-
// We didnt write <Self as Foo>::Assoc so the Self::Assoc<_> is lowered to QPath::TypeRelative.
39+
//
40+
// In the above code we would call this query with the def_id of 3 and
41+
// the parent_node we match on would be the hir node for Self::Assoc<3>
42+
//
43+
// `Self::Assoc<3>` cant be resolved without typchecking here as we
44+
// didnt write <Self as Foo>::Assoc<3>. If we did then another match
45+
// arm would handle this.
46+
//
4147
// I believe this match arm is only needed for GAT but I am not 100% sure - BoxyUwU
4248
Node::Ty(hir_ty @ Ty { kind: TyKind::Path(QPath::TypeRelative(_, segment)), .. }) => {
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-
// For the code example above, this item would be the Foo trait.
49+
// Find the Item containing the associated type so we can create an ItemCtxt.
50+
// Using the ItemCtxt convert the HIR for the unresolved assoc type into a
51+
// ty which is a fully resolved projection.
52+
// For the code example above, this would mean converting Self::Assoc<3>
53+
// into a ty::Projection(<Self as Foo>::Assoc<3>)
4654
let item_hir_id = tcx
4755
.hir()
4856
.parent_iter(hir_id)
@@ -52,11 +60,12 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
5260
.unwrap();
5361
let item_did = tcx.hir().local_def_id(item_hir_id).to_def_id();
5462
let item_ctxt = &ItemCtxt::new(tcx, item_did) as &dyn crate::astconv::AstConv<'_>;
55-
56-
// This ty will be the actual associated type so that we can
57-
// go through its generics to find which param our def_id corresponds to.
58-
// For the code example above, this ty would be the Assoc<const N1: usize>.
5963
let ty = item_ctxt.ast_ty_to_ty(hir_ty);
64+
65+
// Iterate through the generics of the projection to find the one that corresponds to
66+
// the def_id that this query was called with. We filter to only const args here as a
67+
// precaution for if it's ever allowed to elide lifetimes in GAT's. It currently isn't
68+
// but it can't hurt to be safe ^^
6069
if let ty::Projection(projection) = ty.kind() {
6170
let generics = tcx.generics_of(projection.item_def_id);
6271

0 commit comments

Comments
 (0)