Skip to content

Commit 7907d14

Browse files
committed
wip
1 parent c8f25db commit 7907d14

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

rust/ql/lib/codeql/rust/elements/internal/ImplImpl.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,11 @@ module Impl {
3333
result = "impl " + trait + this.getSelfTy().toAbbreviatedString() + " { ... }"
3434
)
3535
}
36+
37+
/**
38+
* Holds if this is an inherent `impl` block, that is, one that does not implement a trait.
39+
*/
40+
pragma[nomagic]
41+
predicate isInherent() { not this.hasTrait() }
3642
}
3743
}

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,23 +1521,14 @@ private module AssocFunctionResolution {
15211521

15221522
private module AssocFunctionTraitIsVisible = TraitIsVisible<assocFunctionCallTraitCandidate/2>;
15231523

1524-
pragma[nomagic]
1525-
predicate callVisibleTraitCandidate(AssocFunctionCall afc, Trait trait) {
1526-
AssocFunctionTraitIsVisible::traitIsVisible(afc, trait)
1527-
or
1528-
// if the call has a trait qualifier then that trait must be visible
1529-
trait = afc.getTrait()
1530-
}
1524+
predicate callVisibleTraitCandidate = AssocFunctionTraitIsVisible::traitIsVisible/2;
15311525

15321526
bindingset[afc, impl]
15331527
pragma[inline_late]
15341528
private predicate callVisibleImplTraitCandidate(AssocFunctionCall afc, ImplItemNode impl) {
15351529
callVisibleTraitCandidate(afc, impl.resolveTraitTy())
15361530
}
15371531

1538-
pragma[nomagic]
1539-
private predicate implHasNoTrait(Impl i) { not i.hasTrait() }
1540-
15411532
/**
15421533
* Holds if call `afc` may target function `f` in `i` with type `selfType` at
15431534
* `selfPos`.
@@ -1560,11 +1551,18 @@ private module AssocFunctionResolution {
15601551
assocFunctionInfoNonBlanketLike(f, name, arity, selfPos, selfPosAdj, i, selfType,
15611552
strippedTypePath, strippedType, implType, isMethod)
15621553
|
1563-
implHasNoTrait(i)
1554+
i.(Impl).isInherent() and
1555+
not afc.hasTrait()
15641556
or
15651557
callVisibleImplTraitCandidate(afc, i)
15661558
or
15671559
callVisibleTraitCandidate(afc, i)
1560+
or
1561+
exists(Trait trait | trait = afc.getTrait() |
1562+
i.(ImplItemNode).resolveTraitTy() = trait
1563+
or
1564+
i = trait
1565+
)
15681566
)
15691567
}
15701568

@@ -1588,8 +1586,11 @@ private module AssocFunctionResolution {
15881586
exists(string name, int arity, boolean isMethod |
15891587
afc.hasNameAndArity(name, arity, _, isMethod) and
15901588
assocFunctionSelfInfoBlanketLike(f, name, arity, selfPos, selfPosAdj, impl, _, self,
1591-
blanketPath, blanketTypeParam, isMethod) and
1589+
blanketPath, blanketTypeParam, isMethod)
1590+
|
15921591
callVisibleImplTraitCandidate(afc, impl)
1592+
or
1593+
impl.resolveTraitTy() = afc.getTrait()
15931594
)
15941595
}
15951596

@@ -1642,23 +1643,26 @@ private module AssocFunctionResolution {
16421643
result = inferType(this.getNodeAt(pos), path)
16431644
}
16441645

1646+
pragma[nomagic]
1647+
private predicate isRelevantSelfPosRec(FunctionPosition selfPos) {
1648+
not this.hasReceiver() and
1649+
exists(TypePath strippedTypePath, Type strippedType |
1650+
strippedType = substituteLookupTraits(this.getTypeAt(selfPos, strippedTypePath)) and
1651+
nonBlanketLikeCandidate(this, _, _, selfPos, _, _, strippedTypePath, strippedType)
1652+
)
1653+
}
1654+
16451655
/**
16461656
* Holds if `selfPos` is a potentially relevant position for resolving this call.
16471657
*/
1648-
pragma[nomagic]
1658+
pragma[inline]
16491659
private predicate isRelevantSelfPos(FunctionPosition selfPos) {
1650-
exists(TypePath strippedTypePath, Type strippedType |
1651-
strippedType = substituteLookupTraits(this.getTypeAt(selfPos, strippedTypePath))
1652-
|
1653-
selfPos.isSelfOrTypeQualifier()
1654-
or
1655-
not this.hasReceiver() and
1656-
(
1657-
blanketLikeCandidate(this, _, _, selfPos, _, _, _, _)
1658-
or
1659-
nonBlanketLikeCandidate(this, _, _, selfPos, _, _, strippedTypePath, strippedType)
1660-
)
1661-
)
1660+
selfPos.isSelfOrTypeQualifier()
1661+
or
1662+
not this.hasReceiver() and
1663+
blanketLikeCandidate(this, _, _, selfPos, _, _, _, _)
1664+
or
1665+
this.isRelevantSelfPosRec(selfPos)
16621666
}
16631667

16641668
/**
@@ -2314,7 +2318,7 @@ private module AssocFunctionResolution {
23142318
forall(Impl i |
23152319
assocFunctionInfoNonBlanketLike(_, name, arity, _, selfPosAdj, i, _, strippedTypePath,
23162320
strippedType, _, _) and
2317-
not i.hasTrait()
2321+
i.isInherent()
23182322
|
23192323
this.hasIncompatibleInherentTarget(i)
23202324
)
@@ -2548,7 +2552,7 @@ private module AssocFunctionResolution {
25482552
AssocFunctionCallCand afcc, TypeAbstraction abs, AssocFunctionType constraint
25492553
) {
25502554
ArgIsInstantiationOfSelfParamInput::potentialInstantiationOf0(afcc, abs, constraint) and
2551-
abs = any(Impl i | not i.hasTrait()) and
2555+
abs.(Impl).isInherent() and
25522556
// todo: comment
25532557
exists(FunctionPosition selfPosAdj | afcc = MkAssocFunctionCallCand(_, _, selfPosAdj, _, _) |
25542558
selfPosAdj.isTypeQualifier() or selfPosAdj.asPosition() = 0

0 commit comments

Comments
 (0)