diff --git a/rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll b/rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll index c6a268be126f..08c5966940eb 100644 --- a/rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll +++ b/rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll @@ -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 @@ -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) ) } @@ -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] diff --git a/shared/typeinference/codeql/typeinference/internal/TypeInference.qll b/shared/typeinference/codeql/typeinference/internal/TypeInference.qll index 888ca65fa98a..9f4be49d878a 100644 --- a/shared/typeinference/codeql/typeinference/internal/TypeInference.qll +++ b/shared/typeinference/codeql/typeinference/internal/TypeInference.qll @@ -566,15 +566,17 @@ module Make1 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) @@ -585,12 +587,10 @@ module Make1 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) } /**