Skip to content

Commit 78280af

Browse files
committed
Rust: Use 'infer' instead of 'resolve' in type inference library
1 parent 2394f2f commit 78280af

File tree

5 files changed

+72
-72
lines changed

5 files changed

+72
-72
lines changed

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

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private TypeMention getTypeAnnotation(AstNode n) {
119119

120120
/** Gets the type of `n`, which has an explicit type annotation. */
121121
pragma[nomagic]
122-
private Type resolveAnnotatedType(AstNode n, TypePath path) {
122+
private Type inferAnnotatedType(AstNode n, TypePath path) {
123123
result = getTypeAnnotation(n).resolveTypeAt(path)
124124
}
125125

@@ -159,8 +159,8 @@ private predicate typeSymmetry(AstNode n1, TypePath path1, AstNode n2, TypePath
159159
}
160160

161161
pragma[nomagic]
162-
private Type resolveTypeSymmetry(AstNode n, TypePath path) {
163-
exists(AstNode n2, TypePath path2 | result = resolveType(n2, path2) |
162+
private Type inferTypeSymmetry(AstNode n, TypePath path) {
163+
exists(AstNode n2, TypePath path2 | result = inferType(n2, path2) |
164164
typeSymmetry(n, path, n2, path2)
165165
or
166166
typeSymmetry(n2, path2, n, path)
@@ -193,12 +193,12 @@ private Type getRefAdjustImplicitSelfType(SelfParam self, TypePath suffix, Type
193193
}
194194

195195
pragma[nomagic]
196-
private Type resolveImplSelfType(Impl i, TypePath path) {
196+
private Type inferImplSelfType(Impl i, TypePath path) {
197197
result = i.getSelfTy().(TypeReprMention).resolveTypeAt(path)
198198
}
199199

200200
pragma[nomagic]
201-
private Type resolveTraitSelfType(Trait t, TypePath path) {
201+
private Type inferTraitSelfType(Trait t, TypePath path) {
202202
result = TTrait(t) and
203203
path.isEmpty()
204204
or
@@ -208,15 +208,15 @@ private Type resolveTraitSelfType(Trait t, TypePath path) {
208208

209209
/** Gets the type at `path` of the implicitly typed `self` parameter. */
210210
pragma[nomagic]
211-
private Type resolveImplicitSelfType(SelfParam self, TypePath path) {
211+
private Type inferImplicitSelfType(SelfParam self, TypePath path) {
212212
exists(ImplOrTraitItemNode i, Function f, TypePath suffix, Type t |
213213
f = i.getAnAssocItem() and
214214
self = f.getParamList().getSelfParam() and
215215
result = getRefAdjustImplicitSelfType(self, suffix, t, path)
216216
|
217-
t = resolveImplSelfType(i, suffix)
217+
t = inferImplSelfType(i, suffix)
218218
or
219-
t = resolveTraitSelfType(i, suffix)
219+
t = inferTraitSelfType(i, suffix)
220220
)
221221
}
222222

@@ -327,8 +327,8 @@ private module RecordExprMatchingInput implements MatchingInputSig {
327327
apos.isRecordPos()
328328
}
329329

330-
Type getResolvedType(AccessPosition apos, TypePath path) {
331-
result = resolveType(this.getNodeAt(apos), path)
330+
Type getInferredType(AccessPosition apos, TypePath path) {
331+
result = inferType(this.getNodeAt(apos), path)
332332
}
333333

334334
Declaration getTarget() { result = resolvePath(this.getPath()) }
@@ -346,15 +346,15 @@ private module RecordExprMatching = Matching<RecordExprMatchingInput>;
346346
* a field expression of a record expression.
347347
*/
348348
pragma[nomagic]
349-
private Type resolveRecordExprType(AstNode n, TypePath path) {
349+
private Type inferRecordExprType(AstNode n, TypePath path) {
350350
exists(RecordExprMatchingInput::Access a, RecordExprMatchingInput::AccessPosition apos |
351351
n = a.getNodeAt(apos) and
352-
result = RecordExprMatching::resolveAccessType(a, apos, path)
352+
result = RecordExprMatching::inferAccessType(a, apos, path)
353353
)
354354
}
355355

356356
pragma[nomagic]
357-
private Type resolvePathExprType(PathExpr pe, TypePath path) {
357+
private Type inferPathExprType(PathExpr pe, TypePath path) {
358358
// nullary struct/variant constructors
359359
not exists(CallExpr ce | pe = ce.getFunction()) and
360360
path.isEmpty() and
@@ -466,7 +466,7 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
466466
}
467467

468468
pragma[nomagic]
469-
private Type resolveAnnotatedTypeInclSelf(AstNode n, TypePath path) {
469+
private Type inferAnnotatedTypeInclSelf(AstNode n, TypePath path) {
470470
result = getTypeAnnotation(n).resolveTypeAtInclSelf(path)
471471
}
472472

@@ -477,18 +477,18 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
477477
exists(Param p, int i, boolean inMethod |
478478
paramPos(this.getParamList(), p, i, inMethod) and
479479
dpos = TPositionalDeclarationPosition(i, inMethod) and
480-
result = resolveAnnotatedTypeInclSelf(p.getPat(), path)
480+
result = inferAnnotatedTypeInclSelf(p.getPat(), path)
481481
)
482482
or
483483
exists(SelfParam self |
484484
self = pragma[only_bind_into](this.getParamList().getSelfParam()) and
485485
dpos.isSelf()
486486
|
487487
// `self` parameter with type annotation
488-
result = resolveAnnotatedTypeInclSelf(self, path)
488+
result = inferAnnotatedTypeInclSelf(self, path)
489489
or
490490
// `self` parameter without type annotation
491-
result = resolveImplicitSelfType(self, path)
491+
result = inferImplicitSelfType(self, path)
492492
or
493493
// `self` parameter without type annotation should also have the special `Self` type
494494
result = getRefAdjustImplicitSelfType(self, TypePath::nil(), TSelfTypeParameter(), path)
@@ -559,8 +559,8 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
559559
apos = TReturnAccessPosition()
560560
}
561561

562-
Type getResolvedType(AccessPosition apos, TypePath path) {
563-
result = resolveType(this.getNodeAt(apos), path)
562+
Type getInferredType(AccessPosition apos, TypePath path) {
563+
result = inferType(this.getNodeAt(apos), path)
564564
}
565565

566566
Declaration getTarget() {
@@ -644,9 +644,9 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
644644
}
645645

646646
pragma[nomagic]
647-
additional Type resolveReceiverType(AstNode n) {
647+
additional Type inferReceiverType(AstNode n) {
648648
exists(Access a, AccessPosition apos |
649-
result = resolveType(n) and
649+
result = inferType(n) and
650650
n = a.getNodeAt(apos) and
651651
apos.isSelf()
652652
)
@@ -660,17 +660,17 @@ private module CallExprBaseMatching = Matching<CallExprBaseMatchingInput>;
660660
* argument/receiver of a call.
661661
*/
662662
pragma[nomagic]
663-
private Type resolveCallExprBaseType(AstNode n, TypePath path) {
663+
private Type inferCallExprBaseType(AstNode n, TypePath path) {
664664
exists(
665665
CallExprBaseMatchingInput::Access a, CallExprBaseMatchingInput::AccessPosition apos,
666666
TypePath path0
667667
|
668668
n = a.getNodeAt(apos) and
669-
result = CallExprBaseMatching::resolveAccessType(a, apos, path0)
669+
result = CallExprBaseMatching::inferAccessType(a, apos, path0)
670670
|
671671
if apos.isSelf()
672672
then
673-
exists(Type receiverType | receiverType = CallExprBaseMatchingInput::resolveReceiverType(n) |
673+
exists(Type receiverType | receiverType = CallExprBaseMatchingInput::inferReceiverType(n) |
674674
if receiverType = TRefType()
675675
then
676676
path = path0 and
@@ -758,8 +758,8 @@ private module FieldExprMatchingInput implements MatchingInputSig {
758758
apos.isField()
759759
}
760760

761-
Type getResolvedType(AccessPosition apos, TypePath path) {
762-
result = resolveType(this.getNodeAt(apos), path)
761+
Type getInferredType(AccessPosition apos, TypePath path) {
762+
result = inferType(this.getNodeAt(apos), path)
763763
}
764764

765765
Declaration getTarget() {
@@ -795,9 +795,9 @@ private module FieldExprMatchingInput implements MatchingInputSig {
795795
}
796796

797797
pragma[nomagic]
798-
additional Type resolveReceiverType(AstNode n) {
798+
additional Type inferReceiverType(AstNode n) {
799799
exists(Access a, AccessPosition apos |
800-
result = resolveType(n) and
800+
result = inferType(n) and
801801
n = a.getNodeAt(apos) and
802802
apos.isSelf()
803803
)
@@ -811,16 +811,16 @@ private module FieldExprMatching = Matching<FieldExprMatchingInput>;
811811
* the receiver of field expression call.
812812
*/
813813
pragma[nomagic]
814-
private Type resolveFieldExprType(AstNode n, TypePath path) {
814+
private Type inferFieldExprType(AstNode n, TypePath path) {
815815
exists(
816816
FieldExprMatchingInput::Access a, FieldExprMatchingInput::AccessPosition apos, TypePath path0
817817
|
818818
n = a.getNodeAt(apos) and
819-
result = FieldExprMatching::resolveAccessType(a, apos, path0)
819+
result = FieldExprMatching::inferAccessType(a, apos, path0)
820820
|
821821
if apos.isSelf()
822822
then
823-
exists(Type receiverType | receiverType = FieldExprMatchingInput::resolveReceiverType(n) |
823+
exists(Type receiverType | receiverType = FieldExprMatchingInput::inferReceiverType(n) |
824824
if receiverType = TRefType()
825825
then
826826
// adjust for implicit deref
@@ -838,14 +838,14 @@ private Type resolveFieldExprType(AstNode n, TypePath path) {
838838
* `& x` or an expression `x` inside a reference expression `& x`.
839839
*/
840840
pragma[nomagic]
841-
private Type resolveRefExprType(Expr e, TypePath path) {
841+
private Type inferRefExprType(Expr e, TypePath path) {
842842
exists(RefExpr re |
843843
e = re and
844844
path.isEmpty() and
845845
result = TRefType()
846846
or
847847
e = re and
848-
exists(TypePath exprPath | result = resolveType(re.getExpr(), exprPath) |
848+
exists(TypePath exprPath | result = inferType(re.getExpr(), exprPath) |
849849
if exprPath.startsWith(TRefTypeParameter(), _)
850850
then
851851
// `&x` simply means `x` when `x` already has reference type
@@ -858,9 +858,9 @@ private Type resolveRefExprType(Expr e, TypePath path) {
858858
or
859859
e = re.getExpr() and
860860
exists(TypePath exprPath, TypePath refPath, Type exprType |
861-
result = resolveType(re, exprPath) and
861+
result = inferType(re, exprPath) and
862862
exprPath = TypePath::cons(TRefTypeParameter(), refPath) and
863-
exprType = resolveType(e)
863+
exprType = inferType(e)
864864
|
865865
if exprType = TRefType()
866866
then
@@ -878,11 +878,11 @@ private module Cached {
878878
pragma[inline]
879879
private Type getLookupType(AstNode n) {
880880
exists(Type t |
881-
t = resolveType(n) and
881+
t = inferType(n) and
882882
if t = TRefType()
883883
then
884884
// for reference types, lookup members in the type being referenced
885-
result = resolveType(n, TypePath::singleton(TRefTypeParameter()))
885+
result = inferType(n, TypePath::singleton(TRefTypeParameter()))
886886
else result = t
887887
)
888888
}
@@ -894,7 +894,7 @@ private module Cached {
894894
}
895895

896896
/**
897-
* Gets a method that the method call `mce` resolves to, if any.
897+
* Gets a method that the method call `mce` infers to, if any.
898898
*/
899899
cached
900900
Function resolveMethodCallExpr(MethodCallExpr mce) {
@@ -908,7 +908,7 @@ private module Cached {
908908
}
909909

910910
/**
911-
* Gets the record field that the field expression `fe` resolves to, if any.
911+
* Gets the record field that the field expression `fe` infers to, if any.
912912
*/
913913
cached
914914
RecordField resolveRecordFieldExpr(FieldExpr fe) {
@@ -924,15 +924,15 @@ private module Cached {
924924
}
925925

926926
/**
927-
* Gets the tuple field that the field expression `fe` resolves to, if any.
927+
* Gets the tuple field that the field expression `fe` infers to, if any.
928928
*/
929929
cached
930930
TupleField resolveTupleFieldExpr(FieldExpr fe) {
931931
exists(int i | result = getTupleFieldExprLookupType(fe, i).getTupleField(i))
932932
}
933933

934934
/**
935-
* Gets a type at `path` that `n` resolves to, if any.
935+
* Gets a type at `path` that `n` infers to, if any.
936936
*
937937
* The type inference implementation works by computing all possible types, so
938938
* the result is not necessarily unique. For example, in
@@ -971,29 +971,29 @@ private module Cached {
971971
* 5. `x.bar()` has type `&MyTrait` (via 2 and 4).
972972
*/
973973
cached
974-
Type resolveType(AstNode n, TypePath path) {
974+
Type inferType(AstNode n, TypePath path) {
975975
Stages::TypeInference::backref() and
976-
result = resolveAnnotatedType(n, path)
976+
result = inferAnnotatedType(n, path)
977977
or
978-
result = resolveTypeSymmetry(n, path)
978+
result = inferTypeSymmetry(n, path)
979979
or
980-
result = resolveImplicitSelfType(n, path)
980+
result = inferImplicitSelfType(n, path)
981981
or
982-
result = resolveRecordExprType(n, path)
982+
result = inferRecordExprType(n, path)
983983
or
984-
result = resolvePathExprType(n, path)
984+
result = inferPathExprType(n, path)
985985
or
986-
result = resolveCallExprBaseType(n, path)
986+
result = inferCallExprBaseType(n, path)
987987
or
988-
result = resolveFieldExprType(n, path)
988+
result = inferFieldExprType(n, path)
989989
or
990-
result = resolveRefExprType(n, path)
990+
result = inferRefExprType(n, path)
991991
}
992992
}
993993

994994
import Cached
995995

996996
/**
997-
* Gets a type that `n` resolves to, if any.
997+
* Gets a type that `n` infers to, if any.
998998
*/
999-
Type resolveType(AstNode n) { result = resolveType(n, TypePath::nil()) }
999+
Type inferType(AstNode n) { result = inferType(n, TypePath::nil()) }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ module Stages {
121121
or
122122
exists(Type t)
123123
or
124-
exists(resolveType(_))
124+
exists(inferType(_))
125125
}
126126
}
127127

rust/ql/test/library-tests/type-inference/type-inference.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
resolveType
1+
inferType
22
| main.rs:5:19:5:22 | SelfParam | | main.rs:2:5:2:21 | struct Foo |
33
| main.rs:5:33:7:9 | { ... } | | main.rs:2:5:2:21 | struct Foo |
44
| main.rs:6:13:6:16 | self | | main.rs:2:5:2:21 | struct Foo |

rust/ql/test/library-tests/type-inference/type-inference.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import codeql.rust.elements.internal.TypeInference as TypeInference
33
import TypeInference
44
import utils.test.InlineExpectationsTest
55

6-
query predicate resolveType(AstNode n, TypePath path, Type t) {
7-
t = TypeInference::resolveType(n, path)
6+
query predicate inferType(AstNode n, TypePath path, Type t) {
7+
t = TypeInference::inferType(n, path)
88
}
99

1010
query predicate resolveMethodCallExpr(MethodCallExpr mce, Function f) {

0 commit comments

Comments
 (0)