Skip to content

Commit 5550da4

Browse files
committed
Address review comments (ii)
1 parent 41602d3 commit 5550da4

File tree

2 files changed

+45
-47
lines changed

2 files changed

+45
-47
lines changed

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

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,14 +1107,14 @@ private module MethodResolution {
11071107
* Same as `getACandidateReceiverTypeAt`, but without borrows.
11081108
*/
11091109
pragma[nomagic]
1110-
private Type getACandidateReceiverTypeAtNoBorrow(TypePath path, string derefChain) {
1110+
private Type getACandidateReceiverTypeAtNoBorrow(string derefChain, TypePath path) {
11111111
result = this.getReceiverTypeAt(path) and
11121112
derefChain = ""
11131113
or
11141114
this.supportsAutoDerefAndBorrow() and
11151115
exists(TypePath path0, Type t0, string derefChain0 |
11161116
this.hasNoCompatibleTargetBorrow(derefChain0) and
1117-
t0 = this.getACandidateReceiverTypeAtNoBorrow(path0, derefChain0)
1117+
t0 = this.getACandidateReceiverTypeAtNoBorrow(derefChain0, path0)
11181118
|
11191119
path0.isCons(TRefTypeParameter(), path) and
11201120
result = t0 and
@@ -1149,21 +1149,21 @@ private module MethodResolution {
11491149
*/
11501150
pragma[nomagic]
11511151
Type getACandidateReceiverTypeAtSubstituteLookupTraits(
1152-
TypePath path, string derefChain, boolean borrow
1152+
string derefChain, boolean borrow, TypePath path
11531153
) {
1154-
result = substituteLookupTraits(this.getACandidateReceiverTypeAt(path, derefChain, borrow))
1154+
result = substituteLookupTraits(this.getACandidateReceiverTypeAt(derefChain, borrow, path))
11551155
}
11561156

11571157
pragma[nomagic]
1158-
private Type getComplexStrippedType(TypePath strippedTypePath, string derefChain, boolean borrow) {
1158+
private Type getComplexStrippedType(string derefChain, boolean borrow, TypePath strippedTypePath) {
11591159
result =
1160-
this.getACandidateReceiverTypeAtSubstituteLookupTraits(strippedTypePath, derefChain, borrow) and
1160+
this.getACandidateReceiverTypeAtSubstituteLookupTraits(derefChain, borrow, strippedTypePath) and
11611161
isComplexRootStripped(strippedTypePath, result)
11621162
}
11631163

11641164
bindingset[strippedTypePath, strippedType, derefChain, borrow]
11651165
private predicate hasNoCompatibleTargetCheck(
1166-
TypePath strippedTypePath, Type strippedType, string derefChain, boolean borrow
1166+
string derefChain, boolean borrow, TypePath strippedTypePath, Type strippedType
11671167
) {
11681168
// todo: also check that all blanket implementation candidates are incompatible
11691169
forall(ImplOrTraitItemNode i |
@@ -1190,8 +1190,8 @@ private module MethodResolution {
11901190
) and
11911191
exists(TypePath strippedTypePath, Type strippedType |
11921192
not derefChain.matches("%.ref") and // no need to try a borrow if the last thing we did was a deref
1193-
strippedType = this.getComplexStrippedType(strippedTypePath, derefChain, false) and
1194-
this.hasNoCompatibleTargetCheck(strippedTypePath, strippedType, derefChain, false)
1193+
strippedType = this.getComplexStrippedType(derefChain, false, strippedTypePath) and
1194+
this.hasNoCompatibleTargetCheck(derefChain, false, strippedTypePath, strippedType)
11951195
)
11961196
}
11971197

@@ -1203,8 +1203,8 @@ private module MethodResolution {
12031203
predicate hasNoCompatibleTargetBorrow(string derefChain) {
12041204
exists(TypePath strippedTypePath, Type strippedType |
12051205
this.hasNoCompatibleTargetNoBorrow(derefChain) and
1206-
strippedType = this.getComplexStrippedType(strippedTypePath, derefChain, true) and
1207-
this.hasNoCompatibleTargetCheck(strippedTypePath, strippedType, derefChain, true)
1206+
strippedType = this.getComplexStrippedType(derefChain, true, strippedTypePath) and
1207+
this.hasNoCompatibleTargetCheck(derefChain, true, strippedTypePath, strippedType)
12081208
)
12091209
}
12101210

@@ -1221,8 +1221,8 @@ private module MethodResolution {
12211221
* [1]: https://doc.rust-lang.org/reference/expressions/method-call-expr.html#r-expr.method.candidate-receivers
12221222
*/
12231223
pragma[nomagic]
1224-
Type getACandidateReceiverTypeAt(TypePath path, string derefChain, boolean borrow) {
1225-
result = this.getACandidateReceiverTypeAtNoBorrow(path, derefChain) and
1224+
Type getACandidateReceiverTypeAt(string derefChain, boolean borrow, TypePath path) {
1225+
result = this.getACandidateReceiverTypeAtNoBorrow(derefChain, path) and
12261226
borrow = false
12271227
or
12281228
this.supportsAutoDerefAndBorrow() and
@@ -1233,7 +1233,7 @@ private module MethodResolution {
12331233
result = TRefType()
12341234
or
12351235
exists(TypePath suffix |
1236-
result = this.getACandidateReceiverTypeAtNoBorrow(suffix, derefChain) and
1236+
result = this.getACandidateReceiverTypeAtNoBorrow(derefChain, suffix) and
12371237
path = TypePath::cons(TRefTypeParameter(), suffix)
12381238
)
12391239
)
@@ -1381,7 +1381,7 @@ private module MethodResolution {
13811381

13821382
private newtype TMethodCallCand =
13831383
MkMethodCallCand(MethodCall mc, string derefChain, boolean borrow) {
1384-
exists(mc.getACandidateReceiverTypeAt(_, derefChain, borrow))
1384+
exists(mc.getACandidateReceiverTypeAt(derefChain, borrow, _))
13851385
}
13861386

13871387
/** A method call tagged with a candidate receiver type. */
@@ -1395,7 +1395,7 @@ private module MethodResolution {
13951395
MethodCall getMethodCall() { result = mc_ }
13961396

13971397
Type getTypeAt(TypePath path) {
1398-
result = mc_.getACandidateReceiverTypeAtSubstituteLookupTraits(path, derefChain, borrow)
1398+
result = mc_.getACandidateReceiverTypeAtSubstituteLookupTraits(derefChain, borrow, path)
13991399
}
14001400

14011401
pragma[nomagic]
@@ -1418,7 +1418,7 @@ private module MethodResolution {
14181418
}
14191419

14201420
/**
1421-
* Holds if the inherent method inside `i` with matching name and arity can be
1421+
* Holds if the inherent method inside `impl` with matching name and arity can be
14221422
* ruled out as a candidate for this call.
14231423
*/
14241424
pragma[nomagic]
@@ -1471,10 +1471,7 @@ private module MethodResolution {
14711471
Method resolveCallTarget() {
14721472
exists(ImplOrTraitItemNode i |
14731473
result = this.resolveCallTargetCand(i) and
1474-
not exists(FunctionPosition pos |
1475-
FunctionOverloading::functionResolutionDependsOnArgument(i, _, pos, _, _) and
1476-
pos.isPosition()
1477-
)
1474+
not FunctionOverloading::functionResolutionDependsOnArgument(i, _, _, _, _)
14781475
)
14791476
or
14801477
MethodArgsAreInstantiationsOf::argsAreInstantiationsOf(this, _, result)
@@ -1627,7 +1624,6 @@ private module MethodResolution {
16271624
private module MethodArgsAreInstantiationsOfInput implements ArgsAreInstantiationsOfInputSig {
16281625
predicate toCheck(ImplOrTraitItemNode i, Function f, FunctionPosition pos, AssocFunctionType t) {
16291626
exists(TypePath path, Type t0 |
1630-
pos.isPosition() and
16311627
FunctionOverloading::functionResolutionDependsOnArgument(i, f, pos, path, t0) and
16321628
t.appliesTo(f, pos, i) and
16331629
// for now, we do not handle ambiguous targets when one of the types it iself
@@ -1746,7 +1742,7 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
17461742

17471743
pragma[nomagic]
17481744
private Type getInferredSelfType(string derefChain, boolean borrow, TypePath path) {
1749-
result = this.getACandidateReceiverTypeAt(path, derefChain, borrow)
1745+
result = this.getACandidateReceiverTypeAt(derefChain, borrow, path)
17501746
}
17511747

17521748
pragma[nomagic]
@@ -1850,14 +1846,14 @@ private module NonMethodResolution {
18501846
/**
18511847
* Holds if the associated function `implFunction` at `impl` implements
18521848
* `traitFunction`, which belongs to `trait`, and resolving the function
1853-
* `implFunction` requires inspecting the type of applied _arguments_ at
1854-
* position `pos` in order to determine whether it is the correct resolution.
1849+
* `implFunction` requires inspecting the type at position `pos` in order
1850+
* to determine whether it is the correct resolution.
18551851
*
18561852
* `type` is the type at `pos` of `implFunction` which mathces a type parameter of
18571853
* `traitFunction` at `pos`.
18581854
*/
18591855
pragma[nomagic]
1860-
private predicate traitFunctionDependsOnArgument(
1856+
private predicate traitFunctionDependsOnPos(
18611857
TraitItemNode trait, NonMethodFunction traitFunction, FunctionPosition pos, Type type,
18621858
ImplItemNode impl, NonMethodFunction implFunction
18631859
) {
@@ -1866,16 +1862,17 @@ private module NonMethodResolution {
18661862
implFunction.implements(traitFunction) and
18671863
FunctionOverloading::traitTypeParameterOccurrence(trait, traitFunction, _, pos, path, _)
18681864
|
1869-
not pos.isReturn()
1870-
or
1871-
// We only check that the context of the call provides relevant type information
1872-
// when no argument can
1873-
not exists(FunctionPosition pos0 |
1874-
FunctionOverloading::traitTypeParameterOccurrence(trait, traitFunction, _, pos0, _, _) or
1875-
FunctionOverloading::functionResolutionDependsOnArgument(impl, implFunction, pos0, _, _)
1876-
|
1877-
not pos0.isReturn()
1878-
)
1865+
if pos.isReturn()
1866+
then
1867+
// We only check that the context of the call provides relevant type information
1868+
// when no argument can
1869+
not exists(FunctionPosition pos0 |
1870+
FunctionOverloading::traitTypeParameterOccurrence(trait, traitFunction, _, pos0, _, _) and
1871+
not pos0.isReturn()
1872+
or
1873+
FunctionOverloading::functionResolutionDependsOnArgument(impl, implFunction, pos0, _, _)
1874+
)
1875+
else any()
18791876
)
18801877
}
18811878

@@ -1886,14 +1883,15 @@ private module NonMethodResolution {
18861883
) {
18871884
functionInfoBlanket(f, name, arity, impl, trait, pos, t, blanketPath, blanketTypeParam) and
18881885
(
1889-
not pos.isReturn()
1890-
or
1891-
// We only check that the context of the call provides relevant type information
1892-
// when no argument can
1893-
not exists(FunctionPosition pos0 |
1894-
functionInfoBlanket(f, name, arity, impl, trait, pos0, _, _, _) and
1895-
not pos0.isReturn()
1896-
)
1886+
if pos.isReturn()
1887+
then
1888+
// We only check that the context of the call provides relevant type information
1889+
// when no argument can
1890+
not exists(FunctionPosition pos0 |
1891+
functionInfoBlanket(f, name, arity, impl, trait, pos0, _, _, _) and
1892+
not pos0.isReturn()
1893+
)
1894+
else any()
18971895
)
18981896
}
18991897

@@ -2084,7 +2082,7 @@ private module NonMethodResolution {
20842082
|
20852083
FunctionOverloading::functionResolutionDependsOnArgument(i, f, pos, _, t0)
20862084
or
2087-
traitFunctionDependsOnArgument(_, _, pos, t0, i, f)
2085+
traitFunctionDependsOnPos(_, _, pos, t0, i, f)
20882086
)
20892087
or
20902088
// match against the trait function itself
@@ -2105,7 +2103,7 @@ private module NonMethodResolution {
21052103
or
21062104
exists(TraitItemNode trait, NonMethodFunction resolved, ImplItemNode i1, Function f1 |
21072105
this.hasTraitResolved(trait, resolved) and
2108-
traitFunctionDependsOnArgument(trait, resolved, _, _, i1, f1)
2106+
traitFunctionDependsOnPos(trait, resolved, _, _, i1, f1)
21092107
|
21102108
f = f1 and
21112109
i = i1

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@ predicate functionResolutionDependsOnArgument(
122122
traitTypeParameterOccurrence(trait, _, functionName, pos, path, _) and
123123
assocFunctionTypeAt(f, impl, pos, path, type) and
124124
f = impl.getASuccessor(functionName) and
125-
not pos.isReturn()
125+
pos.isPosition()
126126
)
127127
}

0 commit comments

Comments
 (0)