Skip to content

Commit 46b0090

Browse files
committed
wip2
1 parent 6e85cab commit 46b0090

File tree

3 files changed

+102
-59
lines changed

3 files changed

+102
-59
lines changed

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

Lines changed: 65 additions & 31 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 |
@@ -1221,6 +1221,7 @@ private module MethodResolution {
12211221
*/
12221222
pragma[nomagic]
12231223
Type getACandidateReceiverTypeAt(TypePath path, string derefChainBorrow) {
1224+
// this = Debug::getRelevantLocatable() and
12241225
exists(string derefChain |
12251226
result = this.getACandidateReceiverTypeAtNoBorrow(path, derefChain) and
12261227
derefChainBorrow = derefChain + ";"
@@ -1478,6 +1479,8 @@ private module MethodResolution {
14781479
MethodArgsAreInstantiationsOf::argsAreInstantiationsOf(this, _, result)
14791480
}
14801481

1482+
predicate hasNoBorrow() { not derefChainBorrow = any(string s) + ";borrow" }
1483+
14811484
string toString() { result = mc_.toString() + " [" + derefChainBorrow + "]" }
14821485

14831486
Location getLocation() { result = mc_.getLocation() }
@@ -1491,12 +1494,17 @@ private module MethodResolution {
14911494
MethodCallCand mcc, ImplItemNode impl, TypePath blanketPath, TypeParam blanketTypeParam
14921495
) {
14931496
exists(MethodCall mc, string name, int arity |
1497+
// mc = Debug::getRelevantLocatable() and
14941498
mcc.hasSignature(mc, _, _, name, arity) and
14951499
methodCallBlanketCandidate(mc, _, impl, _, blanketPath, blanketTypeParam) and
14961500
// Only apply blanket implementations when no other implementations are possible;
14971501
// this is to account for codebases that use the (unstable) specialization feature
14981502
// (https://rust-lang.github.io/rfcs/1210-impl-specialization.html)
14991503
mcc.hasNoCompatibleTarget()
1504+
|
1505+
mcc.hasNoBorrow()
1506+
or
1507+
blanketPath.getHead() = TRefTypeParameter()
15001508
)
15011509
}
15021510
}
@@ -1506,11 +1514,11 @@ private module MethodResolution {
15061514
* a `self` parameter.
15071515
*/
15081516
private module ReceiverIsInstantiationOfSelfParamInput implements
1509-
IsInstantiationOfInputSig<MethodCallCand, FunctionType>
1517+
IsInstantiationOfInputSig<MethodCallCand, AssocFunctionType>
15101518
{
15111519
pragma[nomagic]
15121520
additional predicate potentialInstantiationOf0(
1513-
MethodCallCand mcc, ImplOrTraitItemNode i, FunctionType selfType
1521+
MethodCallCand mcc, ImplOrTraitItemNode i, AssocFunctionType selfType
15141522
) {
15151523
exists(
15161524
MethodCall mc, Method m, string name, int arity, TypePath strippedTypePath,
@@ -1528,7 +1536,7 @@ private module MethodResolution {
15281536

15291537
pragma[nomagic]
15301538
predicate potentialInstantiationOf(
1531-
MethodCallCand mcc, TypeAbstraction abs, FunctionType constraint
1539+
MethodCallCand mcc, TypeAbstraction abs, AssocFunctionType constraint
15321540
) {
15331541
potentialInstantiationOf0(mcc, abs, constraint) and
15341542
if abs.(Impl).hasTrait()
@@ -1539,7 +1547,7 @@ private module MethodResolution {
15391547
else any()
15401548
}
15411549

1542-
predicate relevantTypeMention(FunctionType constraint) {
1550+
predicate relevantTypeMention(AssocFunctionType constraint) {
15431551
methodInfo(_, _, _, _, constraint, _, _)
15441552
}
15451553
}
@@ -1589,17 +1597,17 @@ private module MethodResolution {
15891597
* a `self` parameter in an inherent method.
15901598
*/
15911599
private module ReceiverIsNotInstantiationOfInherentSelfParamInput implements
1592-
IsInstantiationOfInputSig<MethodCallCand, FunctionType>
1600+
IsInstantiationOfInputSig<MethodCallCand, AssocFunctionType>
15931601
{
15941602
pragma[nomagic]
15951603
predicate potentialInstantiationOf(
1596-
MethodCallCand mcc, TypeAbstraction abs, FunctionType constraint
1604+
MethodCallCand mcc, TypeAbstraction abs, AssocFunctionType constraint
15971605
) {
15981606
ReceiverIsInstantiationOfSelfParamInput::potentialInstantiationOf0(mcc, abs, constraint) and
15991607
abs = any(Impl i | not i.hasTrait())
16001608
}
16011609

1602-
predicate relevantTypeMention(FunctionType constraint) {
1610+
predicate relevantTypeMention(AssocFunctionType constraint) {
16031611
methodInfo(_, _, _, _, constraint, _, _)
16041612
}
16051613
}
@@ -1612,7 +1620,9 @@ private module MethodResolution {
16121620
* types of parameters, when needed to disambiguate the call.
16131621
*/
16141622
private module MethodArgsAreInstantiationsOfInput implements ArgsAreInstantiationsOfInputSig {
1615-
predicate toCheck(ImplOrTraitItemNode i, Function f, FunctionTypePosition pos, FunctionType t) {
1623+
predicate toCheck(
1624+
ImplOrTraitItemNode i, Function f, FunctionTypePosition pos, AssocFunctionType t
1625+
) {
16161626
exists(TypePath path, Type t0 |
16171627
pos.isPositional() and
16181628
FunctionOverloading::functionResolutionDependsOnArgument(i, f, pos, path, t0) and
@@ -1774,8 +1784,9 @@ private Type inferMethodCallType0(
17741784
) {
17751785
exists(TypePath path0 |
17761786
n = a.getNodeAt(apos) and
1777-
result = MethodCallMatching::inferAccessType(a, derefChainBorrow, apos, path0)
1787+
result = MethodCallMatching::inferAccessType(a, derefChainBorrow, apos, path0) //and
17781788
|
1789+
// not apos.isSelf()
17791790
if
17801791
// index expression `x[i]` desugars to `*x.index(i)`, so we must account for
17811792
// the implicit deref
@@ -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,
@@ -1942,10 +1953,12 @@ private module NonMethodResolution {
19421953
* a parameter that mentions a satisfied blanket type parameter.
19431954
*/
19441955
private module ArgIsInstantiationOfBlanketParamInput implements
1945-
IsInstantiationOfInputSig<CallAndPos, FunctionType>
1956+
IsInstantiationOfInputSig<CallAndPos, AssocFunctionType>
19461957
{
19471958
pragma[nomagic]
1948-
predicate potentialInstantiationOf(CallAndPos fcp, TypeAbstraction abs, FunctionType constraint) {
1959+
predicate potentialInstantiationOf(
1960+
CallAndPos fcp, TypeAbstraction abs, AssocFunctionType constraint
1961+
) {
19491962
exists(FunctionTypePosition pos |
19501963
BlanketImplementation::SatisfiesBlanketConstraint<CallAndPos, ArgSatisfiesBlanketConstraintInput>::satisfiesBlanketConstraint(fcp,
19511964
abs) and
@@ -1954,7 +1967,7 @@ private module NonMethodResolution {
19541967
)
19551968
}
19561969

1957-
predicate relevantTypeMention(FunctionType constraint) {
1970+
predicate relevantTypeMention(AssocFunctionType constraint) {
19581971
functionInfoBlanketRelevantPos(_, _, _, _, _, _, constraint, _, _)
19591972
}
19601973
}
@@ -2047,7 +2060,9 @@ private module NonMethodResolution {
20472060
}
20482061

20492062
private module NonMethodArgsAreInstantiationsOfInput implements ArgsAreInstantiationsOfInputSig {
2050-
predicate toCheck(ImplOrTraitItemNode i, Function f, FunctionTypePosition pos, FunctionType t) {
2063+
predicate toCheck(
2064+
ImplOrTraitItemNode i, Function f, FunctionTypePosition pos, AssocFunctionType t
2065+
) {
20512066
t.appliesTo(f, pos, i) and
20522067
(
20532068
exists(Type t0 |
@@ -3041,6 +3056,11 @@ private module Cached {
30413056
*/
30423057
cached
30433058
Type inferType(AstNode n, TypePath path) {
3059+
// exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
3060+
// n.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
3061+
// filepath.matches("%/broadcast.rs") and
3062+
// startline = [198 .. 230]
3063+
// ) and
30443064
Stages::TypeInferenceStage::ref() and
30453065
result = CertainTypeInference::inferCertainType(n, path)
30463066
or
@@ -3101,8 +3121,8 @@ private module Debug {
31013121
Locatable getRelevantLocatable() {
31023122
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
31033123
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
3104-
filepath.matches("%/sqlx.rs") and
3105-
startline = [56 .. 60]
3124+
filepath.matches("%/broadcast.rs") and
3125+
startline = [199, 203, 204]
31063126
)
31073127
}
31083128

@@ -3116,6 +3136,15 @@ private module Debug {
31163136
result = resolveCallTarget(c)
31173137
}
31183138

3139+
Type debugResolvedCallTargetType(Call c, Function f, FunctionTypePosition pos, TypePath path) {
3140+
exists(AssocFunctionType aft |
3141+
c = getRelevantLocatable() and
3142+
f = resolveCallTarget(c) and
3143+
aft.appliesTo(f, pos, _) and
3144+
result = aft.getTypeAt(path)
3145+
)
3146+
}
3147+
31193148
predicate debugConditionSatisfiesConstraint(
31203149
TypeAbstraction abs, TypeMention condition, TypeMention constraint
31213150
) {
@@ -3154,9 +3183,14 @@ private module Debug {
31543183
result = strictcount(Type t0 | t0 = inferType(n, path))
31553184
}
31563185

3186+
pragma[nomagic]
3187+
private predicate atLimit(AstNode n) {
3188+
exists(TypePath path0 | exists(inferType(n, path0)) and path0.length() >= getTypePathLimit())
3189+
}
3190+
31573191
Type debugInferTypeForNodeAtLimit(AstNode n, TypePath path) {
31583192
result = inferType(n, path) and
3159-
exists(TypePath path0 | exists(inferType(n, path0)) and path0.length() >= getTypePathLimit())
3193+
atLimit(n)
31603194
}
31613195

31623196
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)