Skip to content

Commit 56cba7c

Browse files
committed
wip
1 parent c98bad2 commit 56cba7c

File tree

1 file changed

+25
-63
lines changed

1 file changed

+25
-63
lines changed

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

Lines changed: 25 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,13 +1113,16 @@ private Trait getCallExprTraitQualifier(CallExpr ce) {
11131113
)
11141114
}
11151115

1116+
pragma[nomagic]
1117+
private predicate nonAssocFunction(ItemNode i) { not i instanceof AssocFunction }
1118+
11161119
/**
11171120
* A call expression that can resolve to something that is not an associated
11181121
* function, and hence does not need type inference for resolution.
11191122
*/
11201123
private class NonAssocCallExpr extends CallExpr {
11211124
NonAssocCallExpr() {
1122-
forex(ItemNode i | i = CallExprImpl::getResolvedFunction(this) | not i instanceof AssocFunction)
1125+
forex(ItemNode i | i = CallExprImpl::getResolvedFunction(this) | nonAssocFunction(i))
11231126
}
11241127

11251128
/**
@@ -1477,55 +1480,6 @@ private module AssocFunctionResolution {
14771480
if f instanceof Method then isMethod = true else isMethod = false
14781481
}
14791482

1480-
pragma[nomagic]
1481-
private predicate assocFunctionInfoNonBlanketLike(
1482-
Function f, string name, int arity, FunctionPosition selfPosAdj, ImplOrTraitItemNode i,
1483-
AssocFunctionType selfType, TypePath strippedTypePath, Type strippedType, TypeOption implType,
1484-
TypeOption trait, boolean isMethod
1485-
) {
1486-
assocFunctionInfo(f, name, arity, selfPosAdj, i, selfType, strippedTypePath, strippedType,
1487-
implType, trait, isMethod) and
1488-
not BlanketImplementation::isBlanketLike(i, _, _)
1489-
}
1490-
1491-
pragma[nomagic]
1492-
private predicate assocFunctionInfoNonBlanketLikeTypeParam(
1493-
Function f, string name, int arity, FunctionPosition selfPosAdj, ImplOrTraitItemNode i,
1494-
AssocFunctionType selfType, TypePath strippedTypePath, TypeOption implType, TypeOption trait,
1495-
boolean isMethod
1496-
) {
1497-
assocFunctionInfoNonBlanketLike(f, name, arity, selfPosAdj, i, selfType, strippedTypePath,
1498-
TTypeParamTypeParameter(_), implType, trait, isMethod)
1499-
}
1500-
1501-
/**
1502-
* Holds if associated function `f` with the name `name` and the arity `arity` exists
1503-
* in blanket (like) implementation `impl` of `trait`, and the type at `selfPosAdj` is
1504-
* `selfType`.
1505-
*
1506-
* `blanketPath` points to the type `blanketTypeParam` inside `selfType`, which
1507-
* is the type parameter used in the blanket implementation.
1508-
*
1509-
* `isMethod` indicates whether `f` is a method.
1510-
*/
1511-
pragma[nomagic]
1512-
private predicate assocFunctionSelfInfoBlanketLike(
1513-
Function f, string name, int arity, FunctionPosition selfPosAdj, ImplItemNode impl, Trait trait,
1514-
AssocFunctionType selfType, TypePath blanketPath, TypeParam blanketTypeParam, boolean isMethod
1515-
) {
1516-
assocFunctionInfoBlanketLike(f, name, arity, impl, trait, selfPosAdj, selfType, blanketPath,
1517-
blanketTypeParam) and
1518-
(
1519-
f instanceof Method and
1520-
selfPosAdj.asPosition() = 0
1521-
or
1522-
selfPosAdj.isTypeQualifier()
1523-
or
1524-
traitImplSelfTypeParameterOccurrence(impl, f, selfPosAdj)
1525-
) and
1526-
if f instanceof Method then isMethod = true else isMethod = false
1527-
}
1528-
15291483
pragma[nomagic]
15301484
private predicate assocFunctionTraitInfo(string name, int arity, Trait trait) {
15311485
exists(ImplItemNode i |
@@ -1557,9 +1511,9 @@ private module AssocFunctionResolution {
15571511
AssocFunctionTraitIsVisible::traitIsVisible(afc, impl.resolveTraitTy())
15581512
}
15591513

1560-
bindingset[implType, trait, isMethod]
1514+
pragma[inline]
15611515
private predicate callMatching(
1562-
TypeOption implType, TypeOption trait, boolean isMethod, TypeOption typeQualifier,
1516+
TypeOption implType, TypeOption trait, Boolean isMethod, TypeOption typeQualifier,
15631517
TypeOption traitQualifier, boolean hasReceiver
15641518
) {
15651519
// when present, explicit type qualifier must match the type being implemented
@@ -1577,8 +1531,9 @@ private module AssocFunctionResolution {
15771531
TypeOption typeQualifier, TypeOption traitQualifier, boolean hasReceiver
15781532
) {
15791533
exists(TypeOption implType, TypeOption trait, boolean isMethod |
1580-
assocFunctionInfoNonBlanketLike(f, name, arity, selfPosAdj, i, selfType, strippedTypePath,
1581-
strippedType, implType, trait, isMethod) and
1534+
assocFunctionInfo(f, name, arity, selfPosAdj, i, selfType, strippedTypePath, strippedType,
1535+
implType, trait, isMethod) and
1536+
not BlanketImplementation::isBlanketLike(i, _, _) and
15821537
callMatching(implType, trait, isMethod, typeQualifier, traitQualifier, hasReceiver)
15831538
)
15841539
}
@@ -1590,8 +1545,9 @@ private module AssocFunctionResolution {
15901545
TypeOption traitQualifier, boolean hasReceiver
15911546
) {
15921547
exists(TypeOption implType, TypeOption trait, boolean isMethod |
1593-
assocFunctionInfoNonBlanketLikeTypeParam(f, name, arity, selfPosAdj, i, selfType,
1594-
strippedTypePath, implType, trait, isMethod) and
1548+
assocFunctionInfo(f, name, arity, selfPosAdj, i, selfType, strippedTypePath,
1549+
TTypeParamTypeParameter(_), implType, trait, isMethod) and
1550+
not BlanketImplementation::isBlanketLike(i, _, _) and
15951551
callMatching(implType, trait, isMethod, typeQualifier, traitQualifier, hasReceiver)
15961552
)
15971553
}
@@ -1603,9 +1559,8 @@ private module AssocFunctionResolution {
16031559
* `strippedTypePath` points to the type `strippedType` inside `selfType`,
16041560
* which is the (possibly complex-stripped) root type of `selfType`.
16051561
*
1606-
* This predicate only checks for matching function names and arities, and whether
1607-
* the trait being implemented by `i` (when `i` is not a trait itself) is visible
1608-
* at `afc`.
1562+
* This predicate only performs syntactic checks, such as matching function name
1563+
* and arity.
16091564
*/
16101565
bindingset[afc, strippedTypePath, strippedType]
16111566
pragma[inline_late]
@@ -1637,10 +1592,17 @@ private module AssocFunctionResolution {
16371592
TypeOption traitQualifier, boolean hasReceiver
16381593
) {
16391594
exists(Trait trait, boolean isMethod |
1640-
assocFunctionSelfInfoBlanketLike(f, name, arity, selfPosAdj, impl, trait, selfType,
1641-
blanketPath, blanketTypeParam, isMethod) and
1642-
traitQualifier = [TypeOption::some(TTrait(trait)), any(TypeOption non | non.isNone())] and
1643-
hasReceiver = [isMethod, false]
1595+
assocFunctionInfoBlanketLike(f, name, arity, impl, trait, selfPosAdj, selfType, blanketPath,
1596+
blanketTypeParam) and
1597+
(if f instanceof Method then isMethod = true else isMethod = false) and
1598+
callMatching(_, TypeOption::some(TTrait(trait)), isMethod, _, traitQualifier, hasReceiver)
1599+
|
1600+
isMethod = true and
1601+
selfPosAdj.asPosition() = 0
1602+
or
1603+
selfPosAdj.isTypeQualifier()
1604+
or
1605+
traitImplSelfTypeParameterOccurrence(impl, f, selfPosAdj)
16441606
)
16451607
}
16461608

0 commit comments

Comments
 (0)