Skip to content

Commit 825997d

Browse files
committed
wip8
1 parent e5c17d3 commit 825997d

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,19 @@ private predicate hasFirstNonTrivialTraitBound(TypeParamItemNode tp, Trait trait
4141
*/
4242
pragma[nomagic]
4343
predicate isBlanketLike(ImplItemNode i, TypePath blanketSelfPath, TypeParam blanketTypeParam) {
44-
blanketTypeParam = i.getBlanketImplementationTypeParam() and
45-
blanketSelfPath.isEmpty()
46-
or
47-
exists(TypeMention tm, Type root, TypeParameter tp |
48-
tm = i.(Impl).getSelfTy() and
49-
complexSelfRoot(root, tp) and
50-
tm.getType() = root and
51-
tm.getTypeAt(blanketSelfPath) = TTypeParamTypeParameter(blanketTypeParam) and
52-
blanketSelfPath = TypePath::singleton(tp) and
53-
hasFirstNonTrivialTraitBound(blanketTypeParam, _)
44+
i.(Impl).hasTrait() and
45+
(
46+
blanketTypeParam = i.getBlanketImplementationTypeParam() and
47+
blanketSelfPath.isEmpty()
48+
or
49+
exists(TypeMention tm, Type root, TypeParameter tp |
50+
tm = i.(Impl).getSelfTy() and
51+
complexSelfRoot(root, tp) and
52+
tm.getType() = root and
53+
tm.getTypeAt(blanketSelfPath) = TTypeParamTypeParameter(blanketTypeParam) and
54+
blanketSelfPath = TypePath::singleton(tp) and
55+
hasFirstNonTrivialTraitBound(blanketTypeParam, _)
56+
)
5457
)
5558
}
5659

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

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,21 @@ private class BorrowKind extends TBorrowKind {
13691369
* [1]: https://doc.rust-lang.org/reference/expressions/method-call-expr.html#r-expr.method.candidate-receivers
13701370
*/
13711371
private module MethodResolution {
1372+
pragma[nomagic]
1373+
private predicate functionResolutionDependsOnArgument(
1374+
ImplItemNode impl, Function f, TypeParameter tp, FunctionPosition pos
1375+
) {
1376+
FunctionOverloading::functionResolutionDependsOnArgument(impl, f, tp, pos)
1377+
// or
1378+
// // For inherent implementations of generic types, we also need to check the type being
1379+
// // implemented. We arbitrarily choose the first type parameter of the type being implemented
1380+
// // to represent this case.
1381+
// f = impl.getAnAssocItem() and
1382+
// not impl.(Impl).hasTrait() and
1383+
// tp = TTypeParamTypeParameter(impl.resolveSelfTy().getTypeParam(0)) and
1384+
// pos.isSelf()
1385+
}
1386+
13721387
pragma[nomagic]
13731388
private predicate traitFunctionResolutionDependsOnArgument0(
13741389
// todo: copied
@@ -1381,7 +1396,7 @@ private module MethodResolution {
13811396
(
13821397
traitTp = TSelfTypeParameter(trait)
13831398
or
1384-
FunctionOverloading::functionResolutionDependsOnArgument(impl, implFunction, traitTp, pos)
1399+
functionResolutionDependsOnArgument(impl, implFunction, traitTp, pos)
13851400
)
13861401
}
13871402

@@ -1593,7 +1608,8 @@ private module MethodResolution {
15931608
exists(string name, int arity |
15941609
mc.hasNameAndArity(name, arity) and
15951610
assocFunctionInfoNonBlanket(f, name, arity, selfPos, pos, i, self, strippedTypePath,
1596-
strippedType)
1611+
strippedType) and
1612+
if mc.hasReceiver() then f instanceof Method else any()
15971613
|
15981614
i =
15991615
any(Impl impl |
@@ -1628,7 +1644,8 @@ private module MethodResolution {
16281644
exists(string name, int arity |
16291645
mc.hasNameAndArity(name, arity) and
16301646
assocFunctionSelfInfoBlanketLike(f, name, arity, selfPos, pos, impl, _, self, blanketPath,
1631-
blanketTypeParam)
1647+
blanketTypeParam) and
1648+
if mc.hasReceiver() then f instanceof Method else any()
16321649
|
16331650
methodCallVisibleImplTraitCandidate(mc, impl)
16341651
or
@@ -2201,7 +2218,8 @@ private module MethodResolution {
22012218
exists(getCallExprPathQualifier(this)) and
22022219
// even if a method cannot be resolved by path resolution, it may still
22032220
// be possible to resolve a blanket implementation (so not `forex`)
2204-
forall(ItemNode i | i = CallExprImpl::getResolvedFunction(this) | i instanceof Function)
2221+
forall(ItemNode i | i = CallExprImpl::getResolvedFunction(this) | i instanceof Function) and
2222+
not exists(this.(NonMethodResolution::NonMethodCall).resolveCallTargetViaPathResolution())
22052223
}
22062224

22072225
pragma[nomagic]
@@ -2303,6 +2321,7 @@ private module MethodResolution {
23032321
MethodCall mc, FunctionPosition selfPos, FunctionPosition pos, DerefChain derefChain,
23042322
BorrowKind borrow
23052323
) {
2324+
// todo: restrict to relevant positions
23062325
exists(mc.getACandidateReceiverTypeAt(selfPos, derefChain, borrow, _)) and
23072326
if mc.hasReceiver()
23082327
then
@@ -2408,7 +2427,7 @@ private module MethodResolution {
24082427
pragma[nomagic]
24092428
Function resolveCallTarget(ImplOrTraitItemNode i) {
24102429
result = this.resolveCallTargetCand(i) and
2411-
not FunctionOverloading::functionResolutionDependsOnArgument(i, result, _, _)
2430+
not functionResolutionDependsOnArgument(i, result, _, _)
24122431
or
24132432
MethodArgsAreInstantiationsOf::argsAreInstantiationsOf(mc_, i, result)
24142433
}
@@ -2569,11 +2588,22 @@ private module MethodResolution {
25692588
) {
25702589
mc = Debug::getRelevantLocatable() and
25712590
mcc.hasSignature(mc, pos, strippedTypePath, strippedType, name, arity) and
2572-
(
2573-
methodCallNonBlanketCandidate(mc, f, _, pos, i, selfType, strippedTypePath, strippedType)
2574-
or
2575-
methodCallBlanketLikeCandidate(mc, f, _, pos, i, selfType, _, _) and
2576-
ReceiverSatisfiesBlanketLikeConstraint::satisfiesBlanketConstraint(mcc, i)
2591+
// methodCallNonBlanketCandidate(mc, f, _, pos, i, selfType, strippedTypePath, strippedType)
2592+
mc.hasNameAndArity(name, arity) and
2593+
exists(FunctionPosition selfPos |
2594+
assocFunctionInfoNonBlanket(f, name, arity, selfPos, pos, i, selfType, strippedTypePath,
2595+
strippedType)
2596+
// |
2597+
// i =
2598+
// any(Impl impl |
2599+
// not impl.hasTrait()
2600+
// or
2601+
// methodCallVisibleImplTraitCandidate(mc, impl)
2602+
// )
2603+
// or
2604+
// methodCallVisibleTraitCandidate(mc, i)
2605+
// or
2606+
// i.(ImplItemNode).resolveTraitTy() = mc.getTrait()
25772607
)
25782608
}
25792609

@@ -2650,7 +2680,7 @@ private module MethodResolution {
26502680
*/
26512681
private module MethodArgsAreInstantiationsOfInput implements ArgsAreInstantiationsOfInputSig {
26522682
predicate toCheck(ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos) {
2653-
FunctionOverloading::functionResolutionDependsOnArgument(i, f, traitTp, pos)
2683+
functionResolutionDependsOnArgument(i, f, traitTp, pos)
26542684
}
26552685

26562686
final private class MethodCallFinal = MethodCall;
@@ -4116,8 +4146,8 @@ private module Debug {
41164146
Locatable getRelevantLocatable() {
41174147
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
41184148
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
4119-
filepath.matches("%/overloading.rs") and
4120-
startline = 399
4149+
filepath.matches("%/main.rs") and
4150+
startline = 93
41214151
)
41224152
}
41234153

0 commit comments

Comments
 (0)