Skip to content

Commit 4c3768f

Browse files
committed
Rust: Add comments for type inference
1 parent 422d9e1 commit 4c3768f

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ private module Cached {
903903
}
904904

905905
/**
906-
* Gets a method that the method call `mce` infers to, if any.
906+
* Gets a method that the method call `mce` resolves to, if any.
907907
*/
908908
cached
909909
Function resolveMethodCallExpr(MethodCallExpr mce) {
@@ -917,7 +917,7 @@ private module Cached {
917917
}
918918

919919
/**
920-
* Gets the record field that the field expression `fe` infers to, if any.
920+
* Gets the record field that the field expression `fe` resolves to, if any.
921921
*/
922922
cached
923923
RecordField resolveRecordFieldExpr(FieldExpr fe) {
@@ -933,7 +933,7 @@ private module Cached {
933933
}
934934

935935
/**
936-
* Gets the tuple field that the field expression `fe` infers to, if any.
936+
* Gets the tuple field that the field expression `fe` resolves to, if any.
937937
*/
938938
cached
939939
TupleField resolveTupleFieldExpr(FieldExpr fe) {

shared/typeinference/codeql/typeinference/internal/TypeInference.qll

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
651651
*
652652
* class Sub<T4> : Mid<C<T4>> { }
653653
*
654-
* new Sub<int>().Method();
654+
* new Sub<int>().Method(); // Note: `Sub<int>` is a subtype of `Base<C<C<int>>>`
655655
* // ^^^^^^^^^^^^^^^^^^^^^^^ `a`
656656
* ```
657657
*
@@ -675,6 +675,11 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
675675
)
676676
}
677677

678+
/**
679+
* Holds if for `a` and corresponding `target`, the type parameter `tp` is
680+
* matched by a type argument at the access with type `t` and type path
681+
* `path`.
682+
*/
678683
pragma[nomagic]
679684
private predicate explicitTypeMatch(
680685
Access a, Declaration target, TypePath path, Type t, TypeParameter tp
@@ -687,17 +692,23 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
687692
private predicate implicitTypeMatch(
688693
Access a, Declaration target, TypePath path, Type t, TypeParameter tp
689694
) {
695+
// We can get the type of `tp` from one of the access positions
690696
directTypeMatch(a, target, path, t, tp)
691697
or
698+
// We can get the type of `tp` by going up the type hiearchy
692699
baseTypeMatch(a, target, path, t, tp)
693700
}
694701

695702
pragma[inline]
696703
private predicate typeMatch(
697704
Access a, Declaration target, TypePath path, Type t, TypeParameter tp
698705
) {
706+
// A type given at the access corresponds directly to the type parameter
707+
// at the target.
699708
explicitTypeMatch(a, target, path, t, tp)
700709
or
710+
// No explicit type argument, so we deduce the parameter from other
711+
// information
701712
implicitTypeMatch(a, target, path, t, tp)
702713
}
703714

@@ -742,12 +753,14 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
742753
pragma[nomagic]
743754
Type inferAccessType(Access a, AccessPosition apos, TypePath path) {
744755
exists(DeclarationPosition dpos | accessDeclarationPositionMatch(apos, dpos) |
756+
// A suffix of `path` leads to a type parameter in the target
745757
exists(Declaration target, TypePath prefix, TypeParameter tp, TypePath suffix |
746758
tp = target.getDeclaredType(pragma[only_bind_into](dpos), prefix) and
747759
path = prefix.append(suffix) and
748760
typeMatch(a, target, suffix, result, tp)
749761
)
750762
or
763+
// `path` corresponds directly to a concrete type in the declaration
751764
exists(Declaration target |
752765
result = target.getDeclaredType(pragma[only_bind_into](dpos), path) and
753766
target = a.getTarget() and

0 commit comments

Comments
 (0)