Skip to content

Commit 40f17d0

Browse files
committed
dbg2
1 parent f2e71fd commit 40f17d0

File tree

3 files changed

+47
-71
lines changed

3 files changed

+47
-71
lines changed

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

Lines changed: 17 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,18 @@ private module MethodResolution {
12861286

12871287
private Type getReceiverTypeAt(TypePath path) {
12881288
result = this.getArgumentTypeAt(any(ArgumentPosition pos | pos.isSelf()), path)
1289+
or
1290+
exists(Function f, SelfParam self, TypePath prefix, TypePath suffix |
1291+
f = CallExprImpl::getResolvedFunction(this) and
1292+
self = f.getSelfParam() and
1293+
result = getCallExprTypeQualifier(this, suffix) and
1294+
path = prefix.append(suffix)
1295+
|
1296+
prefix = getSelfParamSelfPath(self)
1297+
or
1298+
not self.hasTypeRepr() and
1299+
if self.isRef() then prefix = TypePath::singleton(TRefTypeParameter()) else prefix.isEmpty()
1300+
)
12891301
}
12901302

12911303
/**
@@ -1557,20 +1569,6 @@ private module MethodResolution {
15571569
forall(ItemNode i | i = CallExprImpl::getResolvedFunction(this) | i instanceof Method)
15581570
}
15591571

1560-
/**
1561-
* Holds if this call has a type qualifier, and we are able to resolve,
1562-
* using path resolution, the method to a member of `impl`.
1563-
*
1564-
* When this is the case, we still want to check that the type qualifier
1565-
* is an instance of the type being implemented, which is done in
1566-
* `TypeQualifierIsInstantiationOfImplSelfInput`.
1567-
*/
1568-
pragma[nomagic]
1569-
predicate hasTypeQualifiedCandidate(ImplItemNode impl) {
1570-
exists(getCallExprTypeQualifier(this, _)) and
1571-
CallExprImpl::getResolvedFunction(this) = impl.getASuccessor(_)
1572-
}
1573-
15741572
pragma[nomagic]
15751573
override predicate hasNameAndArity(string name, int arity) {
15761574
name = CallExprImpl::getFunctionPath(this).getText() and
@@ -1584,11 +1582,6 @@ private module MethodResolution {
15841582
result = this.getArgList().getArg(pos.asPosition() + 1)
15851583
}
15861584

1587-
// needed for `TypeQualifierIsInstantiationOfImplSelfInput`
1588-
Type getTypeAt(TypePath path) {
1589-
result = substituteLookupTraits(getCallExprTypeQualifier(this, path))
1590-
}
1591-
15921585
override predicate supportsAutoDerefAndBorrow() { none() }
15931586
}
15941587

@@ -1699,18 +1692,9 @@ private module MethodResolution {
16991692
)
17001693
}
17011694

1702-
pragma[nomagic]
1703-
private predicate typeQualifierIsInstantiationOf(ImplOrTraitItemNode i) {
1704-
TypeQualifierIsInstantiationOfImplSelf::isInstantiationOf(mc_, i, _)
1705-
}
1706-
17071695
pragma[nomagic]
17081696
private predicate argIsInstantiationOf(ImplOrTraitItemNode i, string name, int arity) {
1709-
(
1710-
ReceiverIsInstantiationOfSelfParam::argIsInstantiationOf(this, i, _)
1711-
or
1712-
this.typeQualifierIsInstantiationOf(i)
1713-
) and
1697+
ReceiverIsInstantiationOfSelfParam::argIsInstantiationOf(this, i, _) and
17141698
mc_.hasNameAndArity(name, arity)
17151699
}
17161700

@@ -1833,6 +1817,7 @@ private module MethodResolution {
18331817
MethodCallCand mcc, ImplItemNode impl, TraitItemNode trait, AssocFunctionType selfType,
18341818
MethodCall mc, Method m, string name, int arity, TypePath strippedTypePath, Type strippedType
18351819
) {
1820+
// mc = Debug::getRelevantLocatable() and
18361821
potentialInstantiationOf0(mcc, impl, selfType, mc, m, name, arity, strippedTypePath,
18371822
strippedType) and
18381823
m = trait.getAnAssocItem()
@@ -1846,6 +1831,7 @@ private module MethodResolution {
18461831
MethodCall mc, Method m, string name, int arity, TypePath strippedTypePath,
18471832
Type strippedType
18481833
|
1834+
// mc = Debug::getRelevantLocatable() and
18491835
potentialInstantiationOf0(mcc, i, selfType, mc, m, name, arity, strippedTypePath,
18501836
strippedType) and
18511837
not (
@@ -1916,45 +1902,6 @@ private module MethodResolution {
19161902
private module ReceiverIsNotInstantiationOfBlanketLikeSelfParam =
19171903
ArgIsInstantiationOf<MethodCallCand, ReceiverIsNotInstantiationOfBlanketLikeSelfParamInput>;
19181904

1919-
/**
1920-
* A configuration for matching the type qualifier of a method call
1921-
* against the type being implemented in an `impl` block. For example,
1922-
* in `Foo::<Bar>::m(x)`, we check that the type `Foo<Bar>` is an
1923-
* instance of the type being implemented.
1924-
*/
1925-
private module TypeQualifierIsInstantiationOfImplSelfInput implements
1926-
IsInstantiationOfInputSig<MethodCallCallExpr, TypeMentionTypeTree>
1927-
{
1928-
pragma[nomagic]
1929-
private predicate potentialInstantiationOf0(
1930-
MethodCallCallExpr ce, ImplItemNode impl, TypeMentionTypeTree constraint
1931-
) {
1932-
ce.hasTypeQualifiedCandidate(impl) and
1933-
constraint = impl.getSelfPath()
1934-
}
1935-
1936-
pragma[nomagic]
1937-
predicate potentialInstantiationOf(
1938-
MethodCallCallExpr ce, TypeAbstraction abs, TypeMentionTypeTree constraint
1939-
) {
1940-
potentialInstantiationOf0(ce, abs, constraint) and
1941-
if abs.(Impl).hasTrait()
1942-
then
1943-
// inherent methods take precedence over trait methods, so only allow
1944-
// trait methods when there are no matching inherent methods
1945-
MkMethodCallCand(ce, _, _).(MethodCallCand).hasNoInherentTarget()
1946-
else any()
1947-
}
1948-
1949-
predicate relevantConstraint(TypeMentionTypeTree constraint) {
1950-
potentialInstantiationOf0(_, _, constraint)
1951-
}
1952-
}
1953-
1954-
private module TypeQualifierIsInstantiationOfImplSelf =
1955-
IsInstantiationOf<MethodCallCallExpr, TypeMentionTypeTree,
1956-
TypeQualifierIsInstantiationOfImplSelfInput>;
1957-
19581905
/**
19591906
* A configuration for matching the types of positional arguments against the
19601907
* types of parameters, when needed to disambiguate the call.
@@ -3494,8 +3441,8 @@ private module Debug {
34943441
Locatable getRelevantLocatable() {
34953442
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
34963443
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
3497-
filepath.matches("%/sqlx.rs") and
3498-
startline = [56 .. 60]
3444+
filepath.matches("%/main.rs") and
3445+
startline = 590
34993446
)
35003447
}
35013448

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class NonAliasPathTypeMention extends PathTypeMention {
164164

165165
/** Gets the type mention in this path for the type parameter `tp`, if any. */
166166
pragma[nomagic]
167-
private TypeMention getTypeMentionForTypeParameter(TypeParameter tp) {
167+
TypeMention getTypeMentionForTypeParameter(TypeParameter tp) {
168168
exists(int i |
169169
result = this.getPositionalTypeArgument(pragma[only_bind_into](i)) and
170170
tp = this.resolveRootType().getPositionalTypeParameter(pragma[only_bind_into](i))
@@ -390,6 +390,34 @@ TypeMention getSelfParamTypeMention(SelfParam self) {
390390
result = self.getTypeRepr()
391391
}
392392

393+
pragma[nomagic]
394+
private predicate pathToSelfParamSelf(TypePath path, TypeMention parent) {
395+
exists(TypeMention self |
396+
self.getParentNode*() instanceof SelfParam and
397+
parent = self and
398+
path.isEmpty()
399+
|
400+
self = any(TraitItemNode trait).getASelfPath()
401+
or
402+
self instanceof ImplSelfMention
403+
)
404+
or
405+
pathToSelfParamSelf(path, parent.(PathTypeRepr).getPath())
406+
or
407+
exists(TypePath suffix, TypeMention tm, TypeParameter tp |
408+
pathToSelfParamSelf(suffix, tm) and
409+
path = TypePath::singleton(tp).append(suffix)
410+
|
411+
tm = parent.(NonAliasPathTypeMention).getTypeMentionForTypeParameter(tp)
412+
or
413+
tm = parent.(RefTypeRepr).getTypeRepr() and
414+
tp = TRefTypeParameter()
415+
)
416+
}
417+
418+
pragma[nomagic]
419+
TypePath getSelfParamSelfPath(SelfParam self) { pathToSelfParamSelf(result, self.getTypeRepr()) }
420+
393421
class DynTraitTypeReprMention extends TypeMention instanceof DynTraitTypeRepr {
394422
private DynTraitType dynType;
395423

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6937,3 +6937,4 @@ inferType
69376937
| pattern_matching.rs:827:5:827:7 | f(...) | | {EXTERNAL LOCATION} | Option |
69386938
| pattern_matching.rs:827:5:827:7 | f(...) | T | file://:0:0:0:0 | () |
69396939
testFailures
6940+
| main.rs:590:21:590:82 | //... | Fixed spurious result: target=MyTrait1::m |

0 commit comments

Comments
 (0)