Skip to content

Commit d5a95a8

Browse files
committed
Rust: Strengthen isNotInstantiationOf uses
1 parent f30a3b3 commit d5a95a8

File tree

5 files changed

+40
-203
lines changed

5 files changed

+40
-203
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,11 +1430,19 @@ private module MethodResolution {
14301430
* Holds if the method inside `i` with matching name and arity can be ruled
14311431
* out as a target of this call, because the candidate receiver type represented
14321432
* by `derefChain` and `borrow` is incompatible with the `self` parameter type.
1433+
*
1434+
* The types are incompatible because they disagree on a concrete type somewhere
1435+
* inside `root`.
14331436
*/
14341437
pragma[nomagic]
1435-
private predicate hasIncompatibleTarget(ImplOrTraitItemNode i, string derefChain, boolean borrow) {
1436-
ReceiverIsInstantiationOfSelfParam::argIsNotInstantiationOf(MkMethodCallCand(this, derefChain,
1437-
borrow), i, _)
1438+
private predicate hasIncompatibleTarget(
1439+
ImplOrTraitItemNode i, string derefChain, boolean borrow, Type root
1440+
) {
1441+
exists(TypePath path |
1442+
ReceiverIsInstantiationOfSelfParam::argIsNotInstantiationOf(MkMethodCallCand(this,
1443+
derefChain, borrow), i, _, path) and
1444+
path.isCons(root.getATypeParameter(), _)
1445+
)
14381446
}
14391447

14401448
/**
@@ -1448,7 +1456,7 @@ private module MethodResolution {
14481456
ImplItemNode impl, string derefChain, boolean borrow
14491457
) {
14501458
ReceiverIsNotInstantiationOfBlanketLikeSelfParam::argIsNotInstantiationOf(MkMethodCallCand(this,
1451-
derefChain, borrow), impl, _)
1459+
derefChain, borrow), impl, _, _)
14521460
or
14531461
ReceiverSatisfiesBlanketLikeConstraint::dissatisfiesBlanketConstraint(MkMethodCallCand(this,
14541462
derefChain, borrow), impl)
@@ -1479,7 +1487,7 @@ private module MethodResolution {
14791487
forall(ImplOrTraitItemNode i |
14801488
methodCallNonBlanketCandidate(this, _, i, _, strippedTypePath, strippedType)
14811489
|
1482-
this.hasIncompatibleTarget(i, derefChain, borrow)
1490+
this.hasIncompatibleTarget(i, derefChain, borrow, strippedType)
14831491
)
14841492
}
14851493

@@ -1818,7 +1826,7 @@ private module MethodResolution {
18181826
*/
18191827
pragma[nomagic]
18201828
private predicate hasIncompatibleInherentTarget(Impl impl) {
1821-
ReceiverIsNotInstantiationOfInherentSelfParam::argIsNotInstantiationOf(this, impl, _)
1829+
ReceiverIsNotInstantiationOfInherentSelfParam::argIsNotInstantiationOf(this, impl, _, _)
18221830
}
18231831

18241832
/**

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,10 @@ module ArgIsInstantiationOf<
256256
ArgSubstIsInstantiationOf::isInstantiationOf(arg, i, constraint)
257257
}
258258

259-
predicate argIsNotInstantiationOf(Arg arg, ImplOrTraitItemNode i, AssocFunctionType constraint) {
260-
ArgSubstIsInstantiationOf::isNotInstantiationOf(arg, i, constraint)
259+
predicate argIsNotInstantiationOf(
260+
Arg arg, ImplOrTraitItemNode i, AssocFunctionType constraint, TypePath path
261+
) {
262+
ArgSubstIsInstantiationOf::isNotInstantiationOf(arg, i, constraint, path)
261263
}
262264
}
263265

rust/ql/test/library-tests/type-inference/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3064,7 +3064,7 @@ mod literal_overlap {
30643064

30653065
pub fn f() -> usize {
30663066
let mut x = 0;
3067-
x = x.f(); // $ target=usizef $ SPURIOUS: target=i32f target=Reff
3067+
x = x.f(); // $ target=usizef $ SPURIOUS: target=i32f
30683068
x
30693069
}
30703070
}

0 commit comments

Comments
 (0)