Skip to content

Commit fddb5c0

Browse files
committed
wip8
1 parent 9a87116 commit fddb5c0

File tree

2 files changed

+55
-23
lines changed

2 files changed

+55
-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: 42 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]
@@ -2408,7 +2426,7 @@ private module MethodResolution {
24082426
pragma[nomagic]
24092427
Function resolveCallTarget(ImplOrTraitItemNode i) {
24102428
result = this.resolveCallTargetCand(i) and
2411-
not FunctionOverloading::functionResolutionDependsOnArgument(i, result, _, _)
2429+
not functionResolutionDependsOnArgument(i, result, _, _)
24122430
or
24132431
MethodArgsAreInstantiationsOf::argsAreInstantiationsOf(mc_, i, result)
24142432
}
@@ -2569,11 +2587,22 @@ private module MethodResolution {
25692587
) {
25702588
mc = Debug::getRelevantLocatable() and
25712589
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)
2590+
// methodCallNonBlanketCandidate(mc, f, _, pos, i, selfType, strippedTypePath, strippedType)
2591+
mc.hasNameAndArity(name, arity) and
2592+
exists(FunctionPosition selfPos |
2593+
assocFunctionInfoNonBlanket(f, name, arity, selfPos, pos, i, selfType, strippedTypePath,
2594+
strippedType)
2595+
// |
2596+
// i =
2597+
// any(Impl impl |
2598+
// not impl.hasTrait()
2599+
// or
2600+
// methodCallVisibleImplTraitCandidate(mc, impl)
2601+
// )
2602+
// or
2603+
// methodCallVisibleTraitCandidate(mc, i)
2604+
// or
2605+
// i.(ImplItemNode).resolveTraitTy() = mc.getTrait()
25772606
)
25782607
}
25792608

@@ -2650,7 +2679,7 @@ private module MethodResolution {
26502679
*/
26512680
private module MethodArgsAreInstantiationsOfInput implements ArgsAreInstantiationsOfInputSig {
26522681
predicate toCheck(ImplOrTraitItemNode i, Function f, TypeParameter traitTp, FunctionPosition pos) {
2653-
FunctionOverloading::functionResolutionDependsOnArgument(i, f, traitTp, pos)
2682+
functionResolutionDependsOnArgument(i, f, traitTp, pos)
26542683
}
26552684

26562685
final private class MethodCallFinal = MethodCall;
@@ -4116,8 +4145,8 @@ private module Debug {
41164145
Locatable getRelevantLocatable() {
41174146
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
41184147
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
4119-
filepath.matches("%/overloading.rs") and
4120-
startline = 399
4148+
filepath.matches("%/main.rs") and
4149+
startline = 93
41214150
)
41224151
}
41234152

0 commit comments

Comments
 (0)