Skip to content

Commit 0c1d6df

Browse files
committed
wip
1 parent 6e5c15e commit 0c1d6df

File tree

3 files changed

+99
-64
lines changed

3 files changed

+99
-64
lines changed

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

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -816,9 +816,9 @@ private Type getCallExprTypeQualifier(CallExpr ce, TypePath path) {
816816
* `i`, and the type at position `pos` is `t`.
817817
*/
818818
pragma[nomagic]
819-
private predicate functionInfo(
819+
private predicate assocFunctionInfo(
820820
Function f, string name, int arity, ImplOrTraitItemNode i, FunctionTypePosition pos,
821-
FunctionType t
821+
AssocFunctionType t
822822
) {
823823
f = i.getASuccessor(name) and
824824
arity = f.getParamList().getNumberOfParams() and
@@ -836,10 +836,10 @@ private predicate functionInfo(
836836
pragma[nomagic]
837837
private predicate functionInfoBlanket(
838838
Function f, string name, int arity, ImplItemNode impl, Trait trait, FunctionTypePosition pos,
839-
FunctionType t, TypePath blanketPath, TypeParam blanketTypeParam
839+
AssocFunctionType t, TypePath blanketPath, TypeParam blanketTypeParam
840840
) {
841841
exists(TypePath blanketSelfPath |
842-
functionInfo(f, name, arity, impl, pos, t) and
842+
assocFunctionInfo(f, name, arity, impl, pos, t) and
843843
TTypeParamTypeParameter(blanketTypeParam) = t.getTypeAt(blanketPath) and
844844
blanketPath = any(string s) + blanketSelfPath and
845845
BlanketImplementation::isBlanketLike(impl, blanketSelfPath, blanketTypeParam) and
@@ -914,11 +914,11 @@ private module MethodResolution {
914914
*/
915915
pragma[nomagic]
916916
private predicate methodInfo(
917-
Method m, string name, int arity, ImplOrTraitItemNode i, FunctionType selfType,
917+
Method m, string name, int arity, ImplOrTraitItemNode i, AssocFunctionType selfType,
918918
TypePath strippedTypePath, Type strippedType
919919
) {
920920
exists(FunctionTypePosition pos |
921-
functionInfo(m, name, arity, i, pos, selfType) and
921+
assocFunctionInfo(m, name, arity, i, pos, selfType) and
922922
strippedType = selfType.getTypeAt(strippedTypePath) and
923923
isComplexRootStripped(strippedTypePath, strippedType) and
924924
pos.isSelf()
@@ -927,7 +927,7 @@ private module MethodResolution {
927927

928928
pragma[nomagic]
929929
private predicate methodInfoTypeParam(
930-
Method m, string name, int arity, ImplOrTraitItemNode i, FunctionType selfType,
930+
Method m, string name, int arity, ImplOrTraitItemNode i, AssocFunctionType selfType,
931931
TypePath strippedTypePath, TypeParam tp
932932
) {
933933
methodInfo(m, name, arity, i, selfType, strippedTypePath, TTypeParamTypeParameter(tp))
@@ -940,7 +940,7 @@ private module MethodResolution {
940940
*/
941941
pragma[inline]
942942
private predicate methodInfoNonBlanket(
943-
Method m, string name, int arity, ImplOrTraitItemNode i, FunctionType selfType,
943+
Method m, string name, int arity, ImplOrTraitItemNode i, AssocFunctionType selfType,
944944
TypePath strippedTypePath, Type strippedType
945945
) {
946946
(
@@ -960,7 +960,7 @@ private module MethodResolution {
960960
*/
961961
pragma[nomagic]
962962
private predicate methodInfoBlanket(
963-
Method m, string name, int arity, ImplItemNode impl, Trait trait, FunctionType selfType,
963+
Method m, string name, int arity, ImplItemNode impl, Trait trait, AssocFunctionType selfType,
964964
TypePath blanketPath, TypeParam blanketTypeParam
965965
) {
966966
exists(FunctionTypePosition pos |
@@ -1017,8 +1017,8 @@ private module MethodResolution {
10171017
bindingset[mc, strippedTypePath, strippedType]
10181018
pragma[inline_late]
10191019
private predicate methodCallNonBlanketCandidate(
1020-
MethodCall mc, Method m, ImplOrTraitItemNode i, FunctionType self, TypePath strippedTypePath,
1021-
Type strippedType
1020+
MethodCall mc, Method m, ImplOrTraitItemNode i, AssocFunctionType self,
1021+
TypePath strippedTypePath, Type strippedType
10221022
) {
10231023
exists(string name, int arity |
10241024
mc.hasNameAndArity(name, arity) and
@@ -1051,7 +1051,7 @@ private module MethodResolution {
10511051
bindingset[mc]
10521052
pragma[inline_late]
10531053
private predicate methodCallBlanketCandidate(
1054-
MethodCall mc, Method m, ImplItemNode impl, FunctionType self, TypePath blanketPath,
1054+
MethodCall mc, Method m, ImplItemNode impl, AssocFunctionType self, TypePath blanketPath,
10551055
TypeParam blanketTypeParam
10561056
) {
10571057
exists(string name, int arity |
@@ -1478,6 +1478,8 @@ private module MethodResolution {
14781478
MethodArgsAreInstantiationsOf::argsAreInstantiationsOf(this, _, result)
14791479
}
14801480

1481+
predicate hasNoBorrow() { not derefChainBorrow = any(string s) + ";borrow" }
1482+
14811483
string toString() { result = mc_.toString() + " [" + derefChainBorrow + "]" }
14821484

14831485
Location getLocation() { result = mc_.getLocation() }
@@ -1497,20 +1499,28 @@ private module MethodResolution {
14971499
// this is to account for codebases that use the (unstable) specialization feature
14981500
// (https://rust-lang.github.io/rfcs/1210-impl-specialization.html)
14991501
mcc.hasNoCompatibleTarget()
1502+
|
1503+
mcc.hasNoBorrow()
1504+
or
1505+
blanketPath.getHead() = TRefTypeParameter()
15001506
)
15011507
}
15021508
}
15031509

1510+
private module ReceiverSatisfiesBlanketConstraint =
1511+
BlanketImplementation::SatisfiesBlanketConstraint<MethodCallCand,
1512+
ReceiverSatisfiesBlanketConstraintInput>;
1513+
15041514
/**
15051515
* A configuration for matching the type of a receiver against the type of
15061516
* a `self` parameter.
15071517
*/
15081518
private module ReceiverIsInstantiationOfSelfParamInput implements
1509-
IsInstantiationOfInputSig<MethodCallCand, FunctionType>
1519+
IsInstantiationOfInputSig<MethodCallCand, AssocFunctionType>
15101520
{
15111521
pragma[nomagic]
15121522
additional predicate potentialInstantiationOf0(
1513-
MethodCallCand mcc, ImplOrTraitItemNode i, FunctionType selfType
1523+
MethodCallCand mcc, ImplOrTraitItemNode i, AssocFunctionType selfType
15141524
) {
15151525
exists(
15161526
MethodCall mc, Method m, string name, int arity, TypePath strippedTypePath,
@@ -1521,14 +1531,13 @@ private module MethodResolution {
15211531
methodCallNonBlanketCandidate(mc, m, i, selfType, strippedTypePath, strippedType)
15221532
or
15231533
methodCallBlanketCandidate(mc, m, i, selfType, _, _) and
1524-
BlanketImplementation::SatisfiesBlanketConstraint<MethodCallCand, ReceiverSatisfiesBlanketConstraintInput>::satisfiesBlanketConstraint(mcc,
1525-
i)
1534+
ReceiverSatisfiesBlanketConstraint::satisfiesBlanketConstraint(mcc, i)
15261535
)
15271536
}
15281537

15291538
pragma[nomagic]
15301539
predicate potentialInstantiationOf(
1531-
MethodCallCand mcc, TypeAbstraction abs, FunctionType constraint
1540+
MethodCallCand mcc, TypeAbstraction abs, AssocFunctionType constraint
15321541
) {
15331542
potentialInstantiationOf0(mcc, abs, constraint) and
15341543
if abs.(Impl).hasTrait()
@@ -1539,7 +1548,7 @@ private module MethodResolution {
15391548
else any()
15401549
}
15411550

1542-
predicate relevantTypeMention(FunctionType constraint) {
1551+
predicate relevantTypeMention(AssocFunctionType constraint) {
15431552
methodInfo(_, _, _, _, constraint, _, _)
15441553
}
15451554
}
@@ -1589,17 +1598,17 @@ private module MethodResolution {
15891598
* a `self` parameter in an inherent method.
15901599
*/
15911600
private module ReceiverIsNotInstantiationOfInherentSelfParamInput implements
1592-
IsInstantiationOfInputSig<MethodCallCand, FunctionType>
1601+
IsInstantiationOfInputSig<MethodCallCand, AssocFunctionType>
15931602
{
15941603
pragma[nomagic]
15951604
predicate potentialInstantiationOf(
1596-
MethodCallCand mcc, TypeAbstraction abs, FunctionType constraint
1605+
MethodCallCand mcc, TypeAbstraction abs, AssocFunctionType constraint
15971606
) {
15981607
ReceiverIsInstantiationOfSelfParamInput::potentialInstantiationOf0(mcc, abs, constraint) and
15991608
abs = any(Impl i | not i.hasTrait())
16001609
}
16011610

1602-
predicate relevantTypeMention(FunctionType constraint) {
1611+
predicate relevantTypeMention(AssocFunctionType constraint) {
16031612
methodInfo(_, _, _, _, constraint, _, _)
16041613
}
16051614
}
@@ -1612,7 +1621,9 @@ private module MethodResolution {
16121621
* types of parameters, when needed to disambiguate the call.
16131622
*/
16141623
private module MethodArgsAreInstantiationsOfInput implements ArgsAreInstantiationsOfInputSig {
1615-
predicate toCheck(ImplOrTraitItemNode i, Function f, FunctionTypePosition pos, FunctionType t) {
1624+
predicate toCheck(
1625+
ImplOrTraitItemNode i, Function f, FunctionTypePosition pos, AssocFunctionType t
1626+
) {
16161627
exists(TypePath path, Type t0 |
16171628
pos.isPositional() and
16181629
FunctionOverloading::functionResolutionDependsOnArgument(i, f, pos, path, t0) and
@@ -1837,7 +1848,7 @@ private module NonMethodResolution {
18371848
ImplItemNode impl, NonMethodFunction implFunction
18381849
) {
18391850
exists(TypePath path |
1840-
functionTypeAtPath(implFunction, impl, pos, path, type) and
1851+
assocFunctionTypeAtPath(implFunction, impl, pos, path, type) and
18411852
implFunction.implements(traitFunction) and
18421853
FunctionOverloading::traitTypeParameterOccurrence(trait, traitFunction, _, pos, path, _)
18431854
|
@@ -1857,7 +1868,7 @@ private module NonMethodResolution {
18571868
pragma[nomagic]
18581869
private predicate functionInfoBlanketRelevantPos(
18591870
NonMethodFunction f, string name, int arity, ImplItemNode impl, Trait trait,
1860-
FunctionTypePosition pos, FunctionType t, TypePath blanketPath, TypeParam blanketTypeParam
1871+
FunctionTypePosition pos, AssocFunctionType t, TypePath blanketPath, TypeParam blanketTypeParam
18611872
) {
18621873
functionInfoBlanket(f, name, arity, impl, trait, pos, t, blanketPath, blanketTypeParam) and
18631874
(
@@ -1895,7 +1906,7 @@ private module NonMethodResolution {
18951906
NonMethodCall fc, NonMethodFunction f, ImplItemNode impl, FunctionTypePosition pos,
18961907
TypePath blanketPath, TypeParam blanketTypeParam
18971908
) {
1898-
exists(string name, int arity, Trait trait, FunctionType t |
1909+
exists(string name, int arity, Trait trait, AssocFunctionType t |
18991910
fc.hasNameAndArity(name, arity) and
19001911
exists(getTypeAt(fc, pos, blanketPath)) and
19011912
functionInfoBlanketRelevantPos(f, name, arity, impl, trait, pos, t, blanketPath,
@@ -1937,24 +1948,28 @@ private module NonMethodResolution {
19371948
}
19381949
}
19391950

1951+
private module ArgSatisfiesBlanketConstraint =
1952+
BlanketImplementation::SatisfiesBlanketConstraint<CallAndPos, ArgSatisfiesBlanketConstraintInput>;
1953+
19401954
/**
19411955
* A configuration for matching the type of an argument against the type of
19421956
* a parameter that mentions a satisfied blanket type parameter.
19431957
*/
19441958
private module ArgIsInstantiationOfBlanketParamInput implements
1945-
IsInstantiationOfInputSig<CallAndPos, FunctionType>
1959+
IsInstantiationOfInputSig<CallAndPos, AssocFunctionType>
19461960
{
19471961
pragma[nomagic]
1948-
predicate potentialInstantiationOf(CallAndPos fcp, TypeAbstraction abs, FunctionType constraint) {
1962+
predicate potentialInstantiationOf(
1963+
CallAndPos fcp, TypeAbstraction abs, AssocFunctionType constraint
1964+
) {
19491965
exists(FunctionTypePosition pos |
1950-
BlanketImplementation::SatisfiesBlanketConstraint<CallAndPos, ArgSatisfiesBlanketConstraintInput>::satisfiesBlanketConstraint(fcp,
1951-
abs) and
1966+
ArgSatisfiesBlanketConstraint::satisfiesBlanketConstraint(fcp, abs) and
19521967
fcp = MkCallAndPos(_, pos) and
19531968
functionInfoBlanketRelevantPos(_, _, _, abs, _, pos, constraint, _, _)
19541969
)
19551970
}
19561971

1957-
predicate relevantTypeMention(FunctionType constraint) {
1972+
predicate relevantTypeMention(AssocFunctionType constraint) {
19581973
functionInfoBlanketRelevantPos(_, _, _, _, _, _, constraint, _, _)
19591974
}
19601975
}
@@ -2047,7 +2062,9 @@ private module NonMethodResolution {
20472062
}
20482063

20492064
private module NonMethodArgsAreInstantiationsOfInput implements ArgsAreInstantiationsOfInputSig {
2050-
predicate toCheck(ImplOrTraitItemNode i, Function f, FunctionTypePosition pos, FunctionType t) {
2065+
predicate toCheck(
2066+
ImplOrTraitItemNode i, Function f, FunctionTypePosition pos, AssocFunctionType t
2067+
) {
20512068
t.appliesTo(f, pos, i) and
20522069
(
20532070
exists(Type t0 |
@@ -2980,9 +2997,9 @@ private module Cached {
29802997
*/
29812998
cached
29822999
StructField resolveStructFieldExpr(FieldExpr fe) {
2983-
exists(string name, Type ty | ty = getFieldExprLookupType(fe, name) |
2984-
result = ty.(StructType).getStruct().getStructField(name) or
2985-
result = ty.(UnionType).getUnion().getStructField(name)
3000+
exists(string name, Type ty | ty = getFieldExprLookupType(fe, pragma[only_bind_into](name)) |
3001+
result = ty.(StructType).getStruct().getStructField(pragma[only_bind_into](name)) or
3002+
result = ty.(UnionType).getUnion().getStructField(pragma[only_bind_into](name))
29863003
)
29873004
}
29883005

@@ -2992,7 +3009,11 @@ private module Cached {
29923009
cached
29933010
TupleField resolveTupleFieldExpr(FieldExpr fe) {
29943011
exists(int i |
2995-
result = getTupleFieldExprLookupType(fe, i).(StructType).getStruct().getTupleField(i)
3012+
result =
3013+
getTupleFieldExprLookupType(fe, pragma[only_bind_into](i))
3014+
.(StructType)
3015+
.getStruct()
3016+
.getTupleField(pragma[only_bind_into](i))
29963017
)
29973018
}
29983019

@@ -3150,9 +3171,14 @@ private module Debug {
31503171
result = strictcount(Type t0 | t0 = inferType(n, path))
31513172
}
31523173

3174+
pragma[nomagic]
3175+
private predicate atLimit(AstNode n) {
3176+
exists(TypePath path0 | exists(inferType(n, path0)) and path0.length() >= getTypePathLimit())
3177+
}
3178+
31533179
Type debugInferTypeForNodeAtLimit(AstNode n, TypePath path) {
31543180
result = inferType(n, path) and
3155-
exists(TypePath path0 | exists(inferType(n, path0)) and path0.length() >= getTypePathLimit())
3181+
atLimit(n)
31563182
}
31573183

31583184
predicate countTypesForNodeAtLimit(AstNode n, int c) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ predicate traitTypeParameterOccurrence(
7979
TypeParameter tp
8080
) {
8181
f = trait.getASuccessor(functionName) and
82-
functionTypeAtPath(f, trait, pos, path, tp) and
82+
assocFunctionTypeAtPath(f, trait, pos, path, tp) and
8383
tp = trait.(TraitTypeAbstraction).getATypeParameter()
8484
}
8585

@@ -120,7 +120,7 @@ predicate functionResolutionDependsOnArgument(
120120
exists(TraitItemNode trait, string functionName |
121121
implHasSibling(impl, trait) and
122122
traitTypeParameterOccurrence(trait, _, functionName, pos, path, _) and
123-
functionTypeAtPath(f, impl, pos, path, type) and
123+
assocFunctionTypeAtPath(f, impl, pos, path, type) and
124124
f = impl.getASuccessor(functionName) and
125125
not pos.isReturn()
126126
)

0 commit comments

Comments
 (0)