Skip to content

Commit 28b19d4

Browse files
committed
wip2
1 parent 7573276 commit 28b19d4

File tree

1 file changed

+63
-37
lines changed

1 file changed

+63
-37
lines changed

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

Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,13 +1136,19 @@ private module MethodCallResolution {
11361136
*/
11371137
pragma[nomagic]
11381138
predicate methodInfo(
1139-
Function f, string name, int arity, ImplOrTraitItemNode i, FunctionType selfType, Type rootType,
1140-
TypePath selfRootPath, Type selfRootType
1139+
Function f, string name, int arity, ImplOrTraitItemNode i, FunctionType selfType,
1140+
TypePath rootTypePath, Type rootType, TypePath selfRootPath, Type selfRootType
11411141
) {
11421142
exists(FunctionTypePosition pos |
11431143
f = i.getASuccessor(name) and
11441144
arity = f.getParamList().getNumberOfParams() and
1145-
rootType = selfType.getTypeAt(TypePath::nil()) and
1145+
rootType = selfType.getTypeAt(rootTypePath) and
1146+
(
1147+
rootTypePath.isEmpty() and
1148+
rootType != TRefType()
1149+
or
1150+
rootTypePath = TypePath::singleton(TRefTypeParameter())
1151+
) and
11461152
selfType.appliesTo(f, pos, i) and
11471153
pos.isSelf() and
11481154
selfType.getTypeAt(selfRootPath) = selfRootType and
@@ -1157,7 +1163,7 @@ private module MethodCallResolution {
11571163
pragma[nomagic]
11581164
private predicate traitMethodInfo(string name, int arity, Trait trait) {
11591165
exists(ImplItemNode i |
1160-
methodInfo(_, name, arity, i, _, _, _, _) and
1166+
methodInfo(_, name, arity, i, _, _, _, _, _) and
11611167
trait = i.resolveTraitTy()
11621168
)
11631169
}
@@ -1167,7 +1173,7 @@ private module MethodCallResolution {
11671173
exists(string name, int arity | mc.(MethodCall).isMethodCall(name, arity) |
11681174
traitMethodInfo(name, arity, trait)
11691175
or
1170-
methodInfo(_, name, arity, trait, _, _, _, _)
1176+
methodInfo(_, name, arity, trait, _, _, _, _, _)
11711177
)
11721178
}
11731179

@@ -1196,12 +1202,12 @@ private module MethodCallResolution {
11961202
*/
11971203
pragma[inline]
11981204
private predicate methodCallCandidate(
1199-
MethodCall mc, ImplOrTraitItemNode i, FunctionType self, Type rootType, TypePath selfRootPath,
1200-
Type selfRootType
1205+
MethodCall mc, ImplOrTraitItemNode i, FunctionType self, TypePath rootTypePath, Type rootType,
1206+
TypePath selfRootPath, Type selfRootType
12011207
) {
12021208
exists(string name, int arity |
12031209
mc.isMethodCall(name, arity) and
1204-
methodInfo(_, name, arity, i, self, rootType, selfRootPath, selfRootType)
1210+
methodInfo(_, name, arity, i, self, rootTypePath, rootType, selfRootPath, selfRootType)
12051211
|
12061212
i =
12071213
any(Impl impl |
@@ -1218,11 +1224,11 @@ private module MethodCallResolution {
12181224
}
12191225

12201226
private int countmethodCallCandidate(MethodCall mc) {
1221-
result = strictcount(ImplOrTraitItemNode i | methodCallCandidate(mc, i, _, _, _, _))
1227+
result = strictcount(ImplOrTraitItemNode i | methodCallCandidate(mc, i, _, _, _, _, _))
12221228
}
12231229

12241230
private predicate countmethodCallMaxCandidate(MethodCall mc, ImplOrTraitItemNode i) {
1225-
methodCallCandidate(mc, i, _, _, _, _) and
1231+
methodCallCandidate(mc, i, _, _, _, _, _) and
12261232
countmethodCallCandidate(mc) = max(countmethodCallCandidate(_))
12271233
}
12281234

@@ -1289,9 +1295,10 @@ private module MethodCallResolution {
12891295
IsInstantiationOf<MethodCallCand, FunctionType, MethodCallIsInstantiationOfInput>::isNotInstantiationOf(MkMethodCallCand(this,
12901296
derefChainBorrow), i, _)
12911297
or
1292-
exists(Type rootType, TypePath selfRootPath, Type selfRootType |
1293-
rootType = this.getACandidateReceiverTypeAtSubstituteTraitBounds(derefChainBorrow) and
1294-
methodCallCandidate(this, i, _, rootType, selfRootPath, selfRootType) and
1298+
exists(TypePath rootTypePath, Type rootType, TypePath selfRootPath, Type selfRootType |
1299+
rootType =
1300+
this.getACandidateReceiverTypeAtSubstituteTraitBounds(rootTypePath, derefChainBorrow) and
1301+
methodCallCandidate(this, i, _, rootTypePath, rootType, selfRootPath, selfRootType) and
12951302
selfRootType !=
12961303
this.getACandidateReceiverTypeAtSubstituteTraitBounds(selfRootPath, derefChainBorrow)
12971304
)
@@ -1303,33 +1310,46 @@ private module MethodCallResolution {
13031310
*/
13041311
pragma[nomagic]
13051312
private predicate noCandidateReceiverTypeAtNoBorrow(string derefChain) {
1306-
exists(Type rootType, string derefChainBorrow |
1313+
exists(TypePath rootTypePath, Type rootType, string derefChainBorrow |
13071314
derefChainBorrow = derefChain + ";" and
13081315
not derefChain.matches("%.ref") and // no need to try a borrow if the last thing we did was a deref
1309-
rootType = this.getACandidateReceiverTypeAtSubstituteTraitBounds(derefChainBorrow)
1316+
rootType =
1317+
this.getACandidateReceiverTypeAtSubstituteTraitBounds(rootTypePath, derefChainBorrow) and
1318+
(
1319+
rootTypePath.isEmpty() and
1320+
rootType != TRefType()
1321+
or
1322+
rootTypePath = TypePath::singleton(TRefTypeParameter())
1323+
)
13101324
|
1311-
forall(ImplOrTraitItemNode i | methodCallCandidate(this, i, _, rootType, _, _) |
1325+
forall(ImplOrTraitItemNode i |
1326+
methodCallCandidate(this, i, _, rootTypePath, rootType, _, _)
1327+
|
13121328
this.isNotCandidate(i, derefChainBorrow)
13131329
)
13141330
)
13151331
}
13161332

1317-
pragma[nomagic]
1318-
private predicate hasRefCandidate(ImplOrTraitItemNode i) {
1319-
methodCallCandidate(this, i, _, TRefType(), _, _)
1320-
}
1321-
1333+
// pragma[nomagic]
1334+
// private predicate hasRefCandidate(ImplOrTraitItemNode i) {
1335+
// methodCallCandidate(this, i, _, TRefType(), _, _)
1336+
// }
13221337
/**
13231338
* Holds if the candidate receiver type represented by `derefChain;borrow` does not
13241339
* have a matching method target.
13251340
*/
13261341
pragma[nomagic]
13271342
private predicate noCandidateReceiverTypeAt(string derefChain) {
1328-
exists(string derefChainBorrow |
1343+
exists(TypePath rootTypePath, Type rootType, string derefChainBorrow |
13291344
derefChainBorrow = derefChain + ";borrow" and
1330-
this.noCandidateReceiverTypeAtNoBorrow(derefChain)
1345+
this.noCandidateReceiverTypeAtNoBorrow(derefChain) and
1346+
rootType =
1347+
this.getACandidateReceiverTypeAtSubstituteTraitBounds(rootTypePath, derefChainBorrow) and
1348+
rootTypePath = TypePath::singleton(TRefTypeParameter())
13311349
|
1332-
forall(ImplOrTraitItemNode i | this.hasRefCandidate(i) |
1350+
forall(ImplOrTraitItemNode i |
1351+
methodCallCandidate(this, i, _, rootTypePath, rootType, _, _)
1352+
|
13331353
this.isNotCandidate(i, derefChainBorrow)
13341354
)
13351355
)
@@ -1475,10 +1495,16 @@ private module MethodCallResolution {
14751495
*/
14761496
pragma[nomagic]
14771497
private predicate hasNoInherentTarget() {
1478-
exists(Type rootType, string name, int arity |
1479-
this.isMethodCall(_, TypePath::nil(), rootType, name, arity) and
1498+
exists(TypePath rootTypePath, Type rootType, string name, int arity |
1499+
this.isMethodCall(_, rootTypePath, rootType, name, arity) and
1500+
(
1501+
rootTypePath.isEmpty() and
1502+
rootType != TRefType()
1503+
or
1504+
rootTypePath = TypePath::singleton(TRefTypeParameter())
1505+
) and
14801506
forall(Impl i, FunctionType self, TypePath selfRootPath, Type selfRootType |
1481-
methodInfo(_, name, arity, i, self, rootType, selfRootPath, selfRootType) and
1507+
methodInfo(_, name, arity, i, self, rootTypePath, rootType, selfRootPath, selfRootType) and
14821508
not i.hasTrait()
14831509
|
14841510
this.isNotInherentCandidate(i)
@@ -1553,12 +1579,12 @@ private module MethodCallResolution {
15531579
) {
15541580
exists(MethodCall mc, string name, int arity, TypePath selfRootPath, Type selfRootType |
15551581
mcc.isMethodCall(mc, selfRootPath, selfRootType, name, arity) and
1556-
methodCallCandidate(mc, abs, constraint, _, selfRootPath, selfRootType)
1582+
methodCallCandidate(mc, abs, constraint, _, _, selfRootPath, selfRootType)
15571583
)
15581584
}
15591585

15601586
predicate relevantTypeMention(FunctionType constraint) {
1561-
methodInfo(_, _, _, _, constraint, _, _, _)
1587+
methodInfo(_, _, _, _, constraint, _, _, _, _)
15621588
}
15631589
}
15641590

@@ -1574,14 +1600,14 @@ private module MethodCallResolution {
15741600
MethodCallCand mcc, TypeAbstraction abs, FunctionType constraint
15751601
) {
15761602
abs = any(Impl i | not i.hasTrait()) and
1577-
exists(MethodCall mc, string name, int arity, Type rootType |
1578-
mcc.isMethodCall(mc, TypePath::nil(), rootType, name, arity) and
1579-
methodCallCandidate(mc, abs, constraint, rootType, _, _)
1603+
exists(MethodCall mc, string name, int arity, TypePath rootTypePath, Type rootType |
1604+
mcc.isMethodCall(mc, rootTypePath, rootType, name, arity) and
1605+
methodCallCandidate(mc, abs, constraint, rootTypePath, rootType, _, _)
15801606
)
15811607
}
15821608

15831609
predicate relevantTypeMention(FunctionType constraint) {
1584-
methodInfo(_, _, _, _, constraint, _, _, _)
1610+
methodInfo(_, _, _, _, constraint, _, _, _, _)
15851611
}
15861612
}
15871613
}
@@ -1892,7 +1918,7 @@ private module FunctionCallResolution {
18921918
exists(TypePath selfRootPath, Type selfRootType |
18931919
f = call.getPathResolutionResolvedFunctionOrImplementation(resolved) and
18941920
trait = call.(Call).getTrait() and
1895-
MethodCallResolution::methodInfo(f, _, _, i, self, _, selfRootPath, selfRootType) and
1921+
MethodCallResolution::methodInfo(f, _, _, i, self, _, _, selfRootPath, selfRootType) and
18961922
call.getTypeAt(selfRootPath) = selfRootType
18971923
)
18981924
}
@@ -1905,7 +1931,7 @@ private module FunctionCallResolution {
19051931
}
19061932

19071933
predicate relevantTypeMention(FunctionType constraint) {
1908-
MethodCallResolution::methodInfo(_, _, _, _, constraint, _, _, _)
1934+
MethodCallResolution::methodInfo(_, _, _, _, constraint, _, _, _, _)
19091935
}
19101936
}
19111937
}
@@ -2192,7 +2218,7 @@ private module OperationResolution {
21922218
TypeAbstraction abs, FunctionType constraint, Trait trait, string name, int arity,
21932219
TypePath selfRootPath, Type selfRootType
21942220
) {
2195-
MethodCallResolution::methodInfo(_, name, arity, abs, constraint, _, selfRootPath,
2221+
MethodCallResolution::methodInfo(_, name, arity, abs, constraint, _, _, selfRootPath,
21962222
selfRootType) and
21972223
(
21982224
trait = abs.(ImplItemNode).resolveTraitTy()
@@ -2210,7 +2236,7 @@ private module OperationResolution {
22102236
}
22112237

22122238
predicate relevantTypeMention(FunctionType constraint) {
2213-
MethodCallResolution::methodInfo(_, _, _, _, constraint, _, _, _)
2239+
MethodCallResolution::methodInfo(_, _, _, _, constraint, _, _, _, _)
22142240
}
22152241
}
22162242
}

0 commit comments

Comments
 (0)