Skip to content

Commit 4c7f9c0

Browse files
committed
Rust: Rename ContextType -> UnknownType
1 parent 79bdf89 commit 4c7f9c0

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ newtype TType =
5151
TSliceType() or
5252
TNeverType() or
5353
TPtrType() or
54-
TContextType() or
54+
TUnknownType() or
5555
TTupleTypeParameter(int arity, int i) { exists(TTuple(arity)) and i in [0 .. arity - 1] } or
5656
TTypeParamTypeParameter(TypeParam t) or
5757
TAssociatedTypeTypeParameter(TypeAlias t) { any(TraitItemNode trait).getAnAssocItem() = t } or
@@ -374,21 +374,27 @@ class PtrType extends Type, TPtrType {
374374

375375
/**
376376
* A special pseudo type used to indicate that the actual type may have to be
377-
* inferred from a context.
377+
* inferred by propagating type information back into call arguments.
378378
*
379-
* For example, a call like `Default::default()` is assigned this type, which
380-
* means that the actual type is to be inferred from the context in which the call
381-
* occurs.
379+
* For example, in
382380
*
383-
* Context types are not restricted to root types, for example in a call like
381+
* ```rust
382+
* let x = Default::default();
383+
* foo(x);
384+
* ```
385+
*
386+
* `Default::default()` is assigned this type, which allows us to infer the actual
387+
* type from the type of `foo`'s first parameter.
388+
*
389+
* Unknown types are not restricted to root types, for example in a call like
384390
* `Vec::new()` we assign this type at the type path corresponding to the type
385391
* parameter of `Vec`.
386392
*
387-
* Context types are used to restrict when type information is allowed to flow
393+
* Unknown types are used to restrict when type information is allowed to flow
388394
* into call arguments (including method call receivers), in order to avoid
389395
* combinatorial explosions.
390396
*/
391-
class ContextType extends Type, TContextType {
397+
class UnknownType extends Type, TUnknownType {
392398
override TypeParameter getPositionalTypeParameter(int i) { none() }
393399

394400
override string toString() { result = "(context typed)" }

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
859859

860860
pragma[nomagic]
861861
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
862-
// Handle constructions that use `Self{...}` syntax
862+
// Handle constructions that use `Self {...}` syntax
863863
exists(TypeMention tm, TypePath path0 |
864864
tm = this.getStructPath() and
865865
result = tm.resolveTypeAt(path0) and
@@ -872,7 +872,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
872872
* be inferred from the context.
873873
*/
874874
pragma[nomagic]
875-
predicate isContextTypedAt(DeclarationPosition pos, TypePath path) {
875+
predicate hasUnknownTypeAt(DeclarationPosition pos, TypePath path) {
876876
exists(Declaration d, TypeParameter tp |
877877
d = this.getTarget() and
878878
pos.isStructPos() and
@@ -939,8 +939,8 @@ private Type inferStructExprType0(AstNode n, boolean isReturn, TypePath path) {
939939
|
940940
result = StructExprMatching::inferAccessType(a, apos, path)
941941
or
942-
a.isContextTypedAt(apos, path) and
943-
result = TContextType()
942+
a.hasUnknownTypeAt(apos, path) and
943+
result = TUnknownType()
944944
)
945945
}
946946

@@ -1022,7 +1022,7 @@ private module ContextTyping {
10221022
* at `pos` and `path` may have to be inferred from the context.
10231023
*/
10241024
bindingset[this, i, target]
1025-
predicate isContextTypedAt(
1025+
predicate hasUnknownTypeAt(
10261026
ImplOrTraitItemNode i, Function target, FunctionPosition pos, TypePath path
10271027
) {
10281028
exists(TypeParameter tp |
@@ -1045,10 +1045,12 @@ private module ContextTyping {
10451045
}
10461046

10471047
pragma[nomagic]
1048-
private predicate isContextTyped(AstNode n, TypePath path) { inferType(n, path) = TContextType() }
1048+
private predicate hasUnknownTypeAt(AstNode n, TypePath path) {
1049+
inferType(n, path) = TUnknownType()
1050+
}
10491051

10501052
pragma[nomagic]
1051-
private predicate isContextTyped(AstNode n) { isContextTyped(n, _) }
1053+
private predicate hasUnknownType(AstNode n) { hasUnknownTypeAt(n, _) }
10521054

10531055
signature Type inferCallTypeSig(AstNode n, boolean isReturn, TypePath path);
10541056

@@ -1062,7 +1064,7 @@ private module ContextTyping {
10621064
pragma[nomagic]
10631065
private Type inferCallTypeFromContextCand(AstNode n, TypePath path, TypePath prefix) {
10641066
result = inferCallType(n, false, path) and
1065-
isContextTyped(n) and
1067+
hasUnknownType(n) and
10661068
prefix = path
10671069
or
10681070
exists(TypePath mid |
@@ -1077,7 +1079,7 @@ private module ContextTyping {
10771079
or
10781080
exists(TypePath prefix |
10791081
result = inferCallTypeFromContextCand(n, path, prefix) and
1080-
isContextTyped(n, prefix)
1082+
hasUnknownTypeAt(n, prefix)
10811083
)
10821084
}
10831085
}
@@ -1744,7 +1746,7 @@ private module MethodResolution {
17441746
Type getTypeAt(TypePath path) {
17451747
result = mc_.getACandidateReceiverTypeAtSubstituteLookupTraits(derefChain, borrow, path) and
17461748
not result = TNeverType() and
1747-
not result = TContextType()
1749+
not result = TUnknownType()
17481750
}
17491751

17501752
pragma[nomagic]
@@ -2160,9 +2162,9 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
21602162
* from the context.
21612163
*/
21622164
pragma[nomagic]
2163-
predicate isContextTypedAt(string derefChainBorrow, FunctionPosition pos, TypePath path) {
2165+
predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path) {
21642166
exists(ImplOrTraitItemNode i |
2165-
this.isContextTypedAt(i, this.getTarget(i, derefChainBorrow), pos, path)
2167+
this.hasUnknownTypeAt(i, this.getTarget(i, derefChainBorrow), pos, path)
21662168
)
21672169
}
21682170
}
@@ -2180,8 +2182,8 @@ private Type inferMethodCallType0(
21802182
(
21812183
result = MethodCallMatching::inferAccessType(a, derefChainBorrow, apos, path0)
21822184
or
2183-
a.isContextTypedAt(derefChainBorrow, apos, path0) and
2184-
result = TContextType()
2185+
a.hasUnknownTypeAt(derefChainBorrow, apos, path0) and
2186+
result = TUnknownType()
21852187
)
21862188
|
21872189
if
@@ -2669,9 +2671,9 @@ private module NonMethodCallMatchingInput implements MatchingInputSig {
26692671
* from the context.
26702672
*/
26712673
pragma[nomagic]
2672-
predicate isContextTypedAt(FunctionPosition pos, TypePath path) {
2674+
predicate hasUnknownTypeAt(FunctionPosition pos, TypePath path) {
26732675
exists(ImplOrTraitItemNode i |
2674-
this.isContextTypedAt(i,
2676+
this.hasUnknownTypeAt(i,
26752677
[
26762678
this.resolveCallTargetViaPathResolution().(NonMethodFunction),
26772679
this.resolveCallTargetViaTypeInference(i),
@@ -2705,8 +2707,8 @@ private Type inferNonMethodCallType0(AstNode n, boolean isReturn, TypePath path)
27052707
|
27062708
result = NonMethodCallMatching::inferAccessType(a, apos, path)
27072709
or
2708-
a.isContextTypedAt(apos, path) and
2709-
result = TContextType()
2710+
a.hasUnknownTypeAt(apos, path) and
2711+
result = TUnknownType()
27102712
)
27112713
}
27122714

rust/ql/lib/codeql/rust/internal/typeinference/BlanketImplementation.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ module SatisfiesBlanketConstraint<
9393
Type getTypeAt(TypePath path) {
9494
result = at.getTypeAt(blanketPath.appendInverse(path)) and
9595
not result = TNeverType() and
96-
not result = TContextType()
96+
not result = TUnknownType()
9797
}
9898

9999
string toString() { result = at.toString() + " [blanket at " + blanketPath.toString() + "]" }

rust/ql/lib/codeql/rust/internal/typeinference/FunctionType.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ module ArgIsInstantiationOf<
230230
Type getTypeAt(TypePath path) {
231231
result = substituteLookupTraits(super.getTypeAt(path)) and
232232
not result = TNeverType() and
233-
not result = TContextType()
233+
not result = TUnknownType()
234234
}
235235
}
236236

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import TypeInference
66

77
query predicate inferType(AstNode n, TypePath path, Type t) {
88
t = TypeInference::inferType(n, path) and
9-
t != TContextType() and
9+
t != TUnknownType() and
1010
n.fromSource() and
1111
not n.isFromMacroExpansion() and
1212
not n instanceof IdentPat and // avoid overlap in the output with the underlying `Name` node

0 commit comments

Comments
 (0)