Skip to content

Commit f2e71fd

Browse files
committed
dbg
1 parent 63d9b71 commit f2e71fd

File tree

5 files changed

+100
-56
lines changed

5 files changed

+100
-56
lines changed

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

Lines changed: 97 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,9 +1331,7 @@ private module MethodResolution {
13311331
* parameter type, or because the blanket constraint is not satisfied.
13321332
*/
13331333
pragma[nomagic]
1334-
private predicate hasIncompatibleBlanketLikeTarget(
1335-
ImplItemNode impl, string derefChain, boolean borrow
1336-
) {
1334+
predicate hasIncompatibleBlanketLikeTarget(ImplItemNode impl, string derefChain, boolean borrow) {
13371335
ReceiverIsNotInstantiationOfBlanketLikeSelfParam::argIsNotInstantiationOf(MkMethodCallCand(this,
13381336
derefChain, borrow), impl, _)
13391337
or
@@ -1675,8 +1673,13 @@ private module MethodResolution {
16751673
* ruled out as a candidate for this call.
16761674
*/
16771675
pragma[nomagic]
1678-
private predicate hasIncompatibleInherentTarget(Impl impl) {
1679-
ReceiverIsNotInstantiationOfInherentSelfParam::argIsNotInstantiationOf(this, impl, _)
1676+
predicate hasIncompatibleImplTarget(Impl impl) {
1677+
ReceiverIsInstantiationOfSelfParam::argIsNotInstantiationOf(this, impl, _)
1678+
}
1679+
1680+
pragma[nomagic]
1681+
predicate hasIncompatibleBlanketLikeTarget(ImplItemNode impl) {
1682+
mc_.hasIncompatibleBlanketLikeTarget(impl, derefChain, borrow)
16801683
}
16811684

16821685
/**
@@ -1691,7 +1694,7 @@ private module MethodResolution {
16911694
methodInfoNonBlanket(_, name, arity, i, _, strippedTypePath, strippedType) and
16921695
not i.hasTrait()
16931696
|
1694-
this.hasIncompatibleInherentTarget(i)
1697+
this.hasIncompatibleImplTarget(i)
16951698
)
16961699
)
16971700
}
@@ -1771,27 +1774,108 @@ private module MethodResolution {
17711774
IsInstantiationOfInputSig<MethodCallCand, AssocFunctionType>
17721775
{
17731776
pragma[nomagic]
1774-
additional predicate potentialInstantiationOf0(
1777+
private predicate methodInfoImplTrait(
1778+
Method m, string name, int arity, ImplItemNode impl, TraitItemNode trait,
1779+
TypePath strippedTypePath, Type strippedType
1780+
) {
1781+
methodInfo(m, name, arity, impl, _, strippedTypePath, strippedType) and
1782+
trait = impl.resolveTraitTy()
1783+
}
1784+
1785+
pragma[nomagic]
1786+
private predicate methodInfoTypeParamImplTrait(
1787+
Method m, string name, int arity, ImplItemNode impl, TraitItemNode trait,
1788+
TypePath strippedTypePath, TypeParam tp
1789+
) {
1790+
methodInfoImplTrait(m, name, arity, impl, trait, strippedTypePath, TTypeParamTypeParameter(tp))
1791+
}
1792+
1793+
pragma[inline]
1794+
private predicate methodInfoNonBlanketImplTrait(
1795+
string name, int arity, ImplItemNode impl, TraitItemNode trait, TypePath strippedTypePath,
1796+
Type strippedType
1797+
) {
1798+
exists(Method m |
1799+
methodInfoImplTrait(m, name, arity, impl, trait, strippedTypePath, strippedType) or
1800+
methodInfoTypeParamImplTrait(m, name, arity, impl, trait, strippedTypePath, _)
1801+
|
1802+
m = impl.getAnAssocItem() and
1803+
not BlanketImplementation::isBlanketLike(impl, _, _)
1804+
)
1805+
}
1806+
1807+
pragma[nomagic]
1808+
private predicate methodInfoBlanketLikeImplTrait(
1809+
string name, int arity, ImplItemNode impl, Trait trait
1810+
) {
1811+
exists(Method m |
1812+
methodInfoBlanketLike(m, name, arity, impl, trait, _, _, _) and
1813+
m = impl.getAnAssocItem()
1814+
)
1815+
}
1816+
1817+
pragma[nomagic]
1818+
private predicate potentialInstantiationOf0(
1819+
MethodCallCand mcc, ImplOrTraitItemNode i, AssocFunctionType selfType, MethodCall mc,
1820+
Method m, string name, int arity, TypePath strippedTypePath, Type strippedType
1821+
) {
1822+
mcc.hasSignature(mc, strippedTypePath, strippedType, name, arity) and
1823+
(
1824+
methodCallNonBlanketCandidate(mc, m, i, selfType, strippedTypePath, strippedType)
1825+
or
1826+
methodCallBlanketLikeCandidate(mc, m, i, selfType, _, _) and
1827+
ReceiverSatisfiesBlanketLikeConstraint::satisfiesBlanketConstraint(mcc, i)
1828+
)
1829+
}
1830+
1831+
pragma[nomagic]
1832+
private predicate potentialInstantiationOf1(
1833+
MethodCallCand mcc, ImplItemNode impl, TraitItemNode trait, AssocFunctionType selfType,
1834+
MethodCall mc, Method m, string name, int arity, TypePath strippedTypePath, Type strippedType
1835+
) {
1836+
potentialInstantiationOf0(mcc, impl, selfType, mc, m, name, arity, strippedTypePath,
1837+
strippedType) and
1838+
m = trait.getAnAssocItem()
1839+
}
1840+
1841+
pragma[nomagic]
1842+
private predicate potentialInstantiationOf1(
17751843
MethodCallCand mcc, ImplOrTraitItemNode i, AssocFunctionType selfType
17761844
) {
17771845
exists(
17781846
MethodCall mc, Method m, string name, int arity, TypePath strippedTypePath,
17791847
Type strippedType
17801848
|
1781-
mcc.hasSignature(mc, strippedTypePath, strippedType, name, arity)
1849+
potentialInstantiationOf0(mcc, i, selfType, mc, m, name, arity, strippedTypePath,
1850+
strippedType) and
1851+
not (
1852+
i instanceof ImplItemNode and
1853+
m = any(TraitItemNode trait).getAnAssocItem()
1854+
)
1855+
)
1856+
or
1857+
exists(
1858+
MethodCall mc, TraitItemNode trait, Method m, string name, int arity,
1859+
TypePath strippedTypePath, Type strippedType
17821860
|
1783-
methodCallNonBlanketCandidate(mc, m, i, selfType, strippedTypePath, strippedType)
1784-
or
1785-
methodCallBlanketLikeCandidate(mc, m, i, selfType, _, _) and
1786-
ReceiverSatisfiesBlanketLikeConstraint::satisfiesBlanketConstraint(mcc, i)
1861+
potentialInstantiationOf1(mcc, i, trait, selfType, mc, m, name, arity, strippedTypePath,
1862+
strippedType) and
1863+
forall(ImplItemNode impl |
1864+
methodInfoNonBlanketImplTrait(name, arity, impl, trait, strippedTypePath, strippedType)
1865+
|
1866+
mcc.hasIncompatibleImplTarget(impl)
1867+
) and
1868+
forall(ImplItemNode impl | methodInfoBlanketLikeImplTrait(name, arity, impl, trait) |
1869+
mcc.hasIncompatibleBlanketLikeTarget(impl)
1870+
)
17871871
)
17881872
}
17891873

17901874
pragma[nomagic]
17911875
predicate potentialInstantiationOf(
17921876
MethodCallCand mcc, TypeAbstraction abs, AssocFunctionType constraint
17931877
) {
1794-
potentialInstantiationOf0(mcc, abs, constraint) and
1878+
potentialInstantiationOf1(mcc, abs, constraint) and
17951879
if abs.(Impl).hasTrait()
17961880
then
17971881
// inherent methods take precedence over trait methods, so only allow
@@ -1871,25 +1955,6 @@ private module MethodResolution {
18711955
IsInstantiationOf<MethodCallCallExpr, TypeMentionTypeTree,
18721956
TypeQualifierIsInstantiationOfImplSelfInput>;
18731957

1874-
/**
1875-
* A configuration for anti-matching the type of a receiver against the type of
1876-
* a `self` parameter in an inherent method.
1877-
*/
1878-
private module ReceiverIsNotInstantiationOfInherentSelfParamInput implements
1879-
IsInstantiationOfInputSig<MethodCallCand, AssocFunctionType>
1880-
{
1881-
pragma[nomagic]
1882-
predicate potentialInstantiationOf(
1883-
MethodCallCand mcc, TypeAbstraction abs, AssocFunctionType constraint
1884-
) {
1885-
ReceiverIsInstantiationOfSelfParamInput::potentialInstantiationOf0(mcc, abs, constraint) and
1886-
abs = any(Impl i | not i.hasTrait())
1887-
}
1888-
}
1889-
1890-
private module ReceiverIsNotInstantiationOfInherentSelfParam =
1891-
ArgIsInstantiationOf<MethodCallCand, ReceiverIsNotInstantiationOfInherentSelfParamInput>;
1892-
18931958
/**
18941959
* A configuration for matching the types of positional arguments against the
18951960
* types of parameters, when needed to disambiguate the call.

rust/ql/test/library-tests/dataflow/sources/CONSISTENCY/PathResolutionConsistency.expected

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
multipleCallTargets
2-
| test.rs:31:22:31:72 | ... .read_to_string(...) |

rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ multipleCallTargets
55
| dereference.rs:184:17:184:30 | ... .foo() |
66
| dereference.rs:186:17:186:25 | S.bar(...) |
77
| dereference.rs:187:17:187:29 | S.bar(...) |
8-
| main.rs:589:9:589:14 | S4.m() |
98
| main.rs:590:9:590:18 | ...::m(...) |
10-
| main.rs:591:9:591:20 | ... .m() |
11-
| main.rs:592:9:592:24 | ...::m(...) |
129
| main.rs:2497:13:2497:31 | ...::from(...) |
1310
| main.rs:2498:13:2498:31 | ...::from(...) |
1411
| main.rs:2499:13:2499:31 | ...::from(...) |

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,10 +586,10 @@ mod impl_overlap {
586586
println!("{:?}", w.m(x)); // $ target=S3<T>::m
587587
println!("{:?}", S3::m(&w, x)); // $ target=S3<T>::m
588588

589-
S4.m(); // $ target=<S4_as_MyTrait2>::m $ SPURIOUS: target=MyTrait1::m
589+
S4.m(); // $ target=<S4_as_MyTrait2>::m
590590
S4::m(&S4); // $ target=<S4_as_MyTrait2>::m $ SPURIOUS: target=MyTrait1::m
591-
S5(0i32).m(); // $ target=<S5<i32>_as_MyTrait2>::m $ SPURIOUS: target=MyTrait1::m
592-
S5::m(&S5(0i32)); // $ target=<S5<i32>_as_MyTrait2>::m $ SPURIOUS: target=MyTrait1::m
591+
S5(0i32).m(); // $ target=<S5<i32>_as_MyTrait2>::m
592+
S5::m(&S5(0i32)); // $ target=<S5<i32>_as_MyTrait2>::m
593593
S5(true).m(); // $ target=MyTrait1::m
594594
S5::m(&S5(true)); // $ target=MyTrait1::m
595595
}

0 commit comments

Comments
 (0)