Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,15 @@ private module AssocFunctionResolution {

predicate hasReceiverAtPos(FunctionPosition pos) { this.hasReceiver() and pos.asPosition() = 0 }

pragma[nomagic]
private predicate hasIncompatibleArgsTarget(
ImplOrTraitItemNode i, FunctionPosition selfPos, DerefChain derefChain, BorrowKind borrow,
AssocFunctionType selfType
) {
SelfArgIsInstantiationOf::argIsInstantiationOf(this, i, selfPos, derefChain, borrow, selfType) and
OverloadedCallArgsAreInstantiationsOf::argsAreNotInstantiationsOf(this, i)
}

/**
* Holds if the function inside `i` with matching name and arity can be ruled
* out as a target of this call, because the candidate receiver type represented
Expand All @@ -1722,16 +1731,11 @@ private module AssocFunctionResolution {
ImplOrTraitItemNode i, FunctionPosition selfPos, DerefChain derefChain, BorrowKind borrow,
Type root
) {
exists(TypePath path |
SelfArgIsInstantiationOf::argIsNotInstantiationOf(this, i, selfPos, derefChain, borrow, path) and
path.isCons(root.getATypeParameter(), _)
)
or
exists(AssocFunctionType selfType |
SelfArgIsInstantiationOf::argIsInstantiationOf(this, i, selfPos, derefChain, borrow,
selfType) and
OverloadedCallArgsAreInstantiationsOf::argsAreNotInstantiationsOf(this, i) and
root = selfType.getTypeAt(TypePath::nil())
exists(AssocFunctionType selfType | root = selfType.getTypeAt(TypePath::nil()) |
this.hasIncompatibleArgsTarget(i, selfPos, derefChain, borrow, selfType)
or
SelfArgIsInstantiationOf::argIsNotInstantiationOf(this, i, selfPos, derefChain, borrow,
selfType)
)
}

Expand Down Expand Up @@ -2608,9 +2612,13 @@ private module AssocFunctionResolution {
pragma[nomagic]
predicate argIsNotInstantiationOf(
AssocFunctionCall afc, ImplOrTraitItemNode i, FunctionPosition selfPos, DerefChain derefChain,
BorrowKind borrow, TypePath path
BorrowKind borrow, AssocFunctionType selfType
) {
argIsNotInstantiationOf(MkAssocFunctionCallCand(afc, selfPos, derefChain, borrow), i, _, path)
exists(TypePath path |
argIsNotInstantiationOf(MkAssocFunctionCallCand(afc, selfPos, derefChain, borrow), i,
selfType, path) and
not path.isEmpty()
)
}

pragma[nomagic]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,15 +566,17 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
)
}

pragma[nomagic]
private predicate typeParametersEqual(
App app, TypeAbstraction abs, Constraint constraint, TypeParameter tp
App app, TypeAbstraction abs, Constraint constraint, int i
) {
satisfiesConcreteTypes(app, abs, constraint) and
tp = getNthTypeParameter(abs, _) and
(
exists(TypeParameter tp |
satisfiesConcreteTypes(app, abs, constraint) and
tp = getNthTypeParameter(abs, i)
|
not exists(getNthTypeParameterPath(constraint, tp, _))
or
exists(int n | n = max(int i | exists(getNthTypeParameterPath(constraint, tp, i))) |
exists(int n | n = max(int j | exists(getNthTypeParameterPath(constraint, tp, j))) |
// If the largest index is 0, then there are no equalities to check as
// the type parameter only occurs once.
if n = 0 then any() else typeParametersEqualToIndex(app, abs, constraint, tp, _, n)
Expand All @@ -585,12 +587,10 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
private predicate typeParametersHaveEqualInstantiationToIndex(
App app, TypeAbstraction abs, Constraint constraint, int i
) {
exists(TypeParameter tp | tp = getNthTypeParameter(abs, i) |
typeParametersEqual(app, abs, constraint, tp) and
if i = 0
then any()
else typeParametersHaveEqualInstantiationToIndex(app, abs, constraint, i - 1)
)
typeParametersEqual(app, abs, constraint, i) and
if i = 0
then any()
else typeParametersHaveEqualInstantiationToIndex(app, abs, constraint, i - 1)
}

/**
Expand Down
Loading