Skip to content

Commit 7fc5fe2

Browse files
committed
wip2
1 parent 05bd335 commit 7fc5fe2

1 file changed

Lines changed: 66 additions & 47 deletions

File tree

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

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ private module AssocFunctionResolution {
13511351
* `i`, and the type at position `posAdj` is `t`.
13521352
*/
13531353
pragma[nomagic]
1354-
private predicate assocFunctionInfo(
1354+
predicate assocFunctionInfo(
13551355
Function f, string name, int arity, ImplOrTraitItemNode i, FunctionPosition posAdj,
13561356
AssocFunctionType t
13571357
) {
@@ -2699,45 +2699,44 @@ private module AssocFunctionResolution {
26992699
}
27002700

27012701
/**
2702-
* A matching configuration for resolving types of method call expressions
2702+
* A matching configuration for resolving types of function call expressions
27032703
* like `foo.bar(baz)`.
27042704
*/
2705-
private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSig {
2705+
private module FunctionCallMatchingInput implements MatchingWithEnvironmentInputSig {
27062706
import FunctionPositionMatchingInput
27072707

2708-
private class MethodDeclaration extends Method, FunctionDeclaration { }
2709-
27102708
private newtype TDeclaration =
2711-
TMethodFunctionDeclaration(ImplOrTraitItemNode i, MethodDeclaration m) { m.isAssoc(i) }
2709+
TFunctionDeclaration(ImplOrTraitItemNodeOption i, FunctionDeclaration f) { f.isFor(i) }
27122710

2713-
final class Declaration extends TMethodFunctionDeclaration {
2714-
ImplOrTraitItemNode parent;
2715-
ImplOrTraitItemNodeOption someParent;
2716-
MethodDeclaration m;
2711+
final class Declaration extends TFunctionDeclaration {
2712+
ImplOrTraitItemNodeOption i;
2713+
FunctionDeclaration f;
27172714

2718-
Declaration() {
2719-
this = TMethodFunctionDeclaration(parent, m) and
2720-
someParent.asSome() = parent
2721-
}
2715+
Declaration() { this = TFunctionDeclaration(i, f) }
27222716

2723-
predicate isMethod(ImplOrTraitItemNode i, Method method) {
2724-
this = TMethodFunctionDeclaration(i, method)
2717+
predicate isAssocFunction(ImplOrTraitItemNode i_, Function f_) {
2718+
i_ = i.asSome() and
2719+
f_ = f
27252720
}
27262721

27272722
TypeParameter getTypeParameter(TypeParameterPosition ppos) {
2728-
result = m.getTypeParameter(someParent, ppos)
2723+
result = f.getTypeParameter(i, ppos)
27292724
}
27302725

27312726
Type getDeclaredType(DeclarationPosition dpos, TypePath path) {
2732-
result = m.getParameterType(someParent, dpos, path)
2727+
result = f.getParameterType(i, dpos, path)
27332728
or
27342729
dpos.isReturn() and
2735-
result = m.getReturnType(someParent, path)
2730+
result = f.getReturnType(i, path)
27362731
}
27372732

2738-
string toString() { result = m.toStringExt(parent) }
2733+
string toString() {
2734+
i.isNone() and result = f.toString()
2735+
or
2736+
result = f.toStringExt(i.asSome())
2737+
}
27392738

2740-
Location getLocation() { result = m.getLocation() }
2739+
Location getLocation() { result = f.getLocation() }
27412740
}
27422741

27432742
class AccessEnvironment = string;
@@ -2758,10 +2757,26 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
27582757
)
27592758
}
27602759

2760+
abstract class Access extends Expr {
2761+
abstract Type getTypeArgument(TypeArgumentPosition apos, TypePath path);
2762+
2763+
abstract AstNode getNodeAt(FunctionPosition posAdj);
2764+
2765+
bindingset[derefChainBorrow]
2766+
abstract Type getInferredType(string derefChainBorrow, FunctionPosition pos, TypePath path);
2767+
2768+
abstract Declaration getTarget(string derefChainBorrow);
2769+
2770+
abstract predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path);
2771+
2772+
abstract predicate hasUnknownTypeAt(FunctionPosition pos, TypePath path);
2773+
}
2774+
27612775
final private class AssocFunctionCallFinal = AssocFunctionResolution::AssocFunctionCall;
27622776

2763-
class Access extends AssocFunctionCallFinal, ContextTyping::ContextTypedCallCand {
2764-
Access() {
2777+
private class AssocFunctionCallAccess extends Access, ContextTyping::ContextTypedCallCand instanceof AssocFunctionResolution::AssocFunctionCall
2778+
{
2779+
AssocFunctionCallAccess() {
27652780
// handled in the `OperationMatchingInput` module
27662781
not this instanceof Operation
27672782
}
@@ -2778,23 +2793,27 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
27782793
result = getCallExprTypeArgument(this, apos, path)
27792794
}
27802795

2796+
override AstNode getNodeAt(FunctionPosition posAdj) {
2797+
result = AssocFunctionCallFinal.super.getNodeAt(posAdj)
2798+
}
2799+
27812800
pragma[nomagic]
27822801
private Type getInferredSelfType(FunctionPosition pos, string derefChainBorrow, TypePath path) {
27832802
exists(DerefChain derefChain, BorrowKind borrow |
2784-
result = this.getSelfTypeAt(pos.getFunctionCallAdjusted(), derefChain, borrow, path) and
2803+
result = super.getSelfTypeAt(pos.getFunctionCallAdjusted(), derefChain, borrow, path) and
27852804
derefChainBorrow = encodeDerefChainBorrow(derefChain, borrow) and
27862805
pos.isSelf()
27872806
)
27882807
}
27892808

27902809
pragma[nomagic]
27912810
private Type getInferredNonSelfType(FunctionPosition pos, TypePath path) {
2792-
result = this.getTypeAt(pos.getFunctionCallAdjusted(), path) and
2811+
result = super.getTypeAt(pos.getFunctionCallAdjusted(), path) and
27932812
not pos.isSelf()
27942813
}
27952814

27962815
bindingset[derefChainBorrow]
2797-
Type getInferredType(string derefChainBorrow, FunctionPosition pos, TypePath path) {
2816+
override Type getInferredType(string derefChainBorrow, FunctionPosition pos, TypePath path) {
27982817
result = this.getInferredSelfType(pos, derefChainBorrow, path)
27992818
or
28002819
result = this.getInferredNonSelfType(pos, path)
@@ -2803,14 +2822,14 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
28032822
Method getTarget(ImplOrTraitItemNode i, string derefChainBorrow) {
28042823
exists(DerefChain derefChain, BorrowKind borrow |
28052824
derefChainBorrow = encodeDerefChainBorrow(derefChain, borrow) and
2806-
result = this.resolveCallTarget(i, derefChain, borrow) // mutual recursion; resolving method calls requires resolving types and vice versa
2825+
result = super.resolveCallTarget(i, derefChain, borrow) // mutual recursion; resolving method calls requires resolving types and vice versa
28072826
)
28082827
}
28092828

2810-
Declaration getTarget(string derefChainBorrow) {
2811-
exists(ImplOrTraitItemNode i, Method m |
2812-
m = this.getTarget(i, derefChainBorrow) and
2813-
result = TMethodFunctionDeclaration(i, m)
2829+
override Declaration getTarget(string derefChainBorrow) {
2830+
exists(ImplOrTraitItemNodeOption i, Function f |
2831+
f = this.getTarget(i.asSome(), derefChainBorrow) and
2832+
result = TFunctionDeclaration(i, f)
28142833
)
28152834
}
28162835

@@ -2819,7 +2838,7 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
28192838
* from the context.
28202839
*/
28212840
pragma[nomagic]
2822-
predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path) {
2841+
override predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path) {
28232842
exists(ImplOrTraitItemNode i |
28242843
this.hasUnknownTypeAt(i, this.getTarget(i, derefChainBorrow), pos, path)
28252844
)
@@ -2830,7 +2849,7 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
28302849
* from the context.
28312850
*/
28322851
pragma[nomagic]
2833-
predicate hasUnknownTypeAt(FunctionPosition pos, TypePath path) {
2852+
override predicate hasUnknownTypeAt(FunctionPosition pos, TypePath path) {
28342853
forex(ImplOrTraitItemNode i, Function f |
28352854
f = CallExprImpl::getResolvedFunction(this) and
28362855
f = i.getAnAssocItem()
@@ -2841,11 +2860,11 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
28412860
}
28422861
}
28432862

2844-
private module MethodCallMatching = MatchingWithEnvironment<MethodCallMatchingInput>;
2863+
private module FunctionCallMatching = MatchingWithEnvironment<FunctionCallMatchingInput>;
28452864

28462865
pragma[nomagic]
2847-
private Type inferMethodCallType0(
2848-
MethodCallMatchingInput::Access a, MethodCallMatchingInput::AccessPosition apos, AstNode n,
2866+
private Type inferFunctionCallType0(
2867+
FunctionCallMatchingInput::Access a, FunctionCallMatchingInput::AccessPosition apos, AstNode n,
28492868
DerefChain derefChain, BorrowKind borrow, TypePath path
28502869
) {
28512870
exists(TypePath path0 |
@@ -2854,9 +2873,9 @@ private Type inferMethodCallType0(
28542873
posAdj = apos.getFunctionCallAdjusted()
28552874
|
28562875
exists(string derefChainBorrow |
2857-
MethodCallMatchingInput::decodeDerefChainBorrow(derefChainBorrow, derefChain, borrow)
2876+
FunctionCallMatchingInput::decodeDerefChainBorrow(derefChainBorrow, derefChain, borrow)
28582877
|
2859-
result = MethodCallMatching::inferAccessType(a, derefChainBorrow, apos, path0)
2878+
result = FunctionCallMatching::inferAccessType(a, derefChainBorrow, apos, path0)
28602879
or
28612880
a.hasUnknownTypeAt(derefChainBorrow, apos, path0) and
28622881
result = TUnknownType()
@@ -2879,8 +2898,8 @@ private Type inferMethodCallType0(
28792898
}
28802899

28812900
pragma[nomagic]
2882-
private Type inferMethodCallTypeNonSelf(AstNode n, FunctionPosition pos, TypePath path) {
2883-
result = inferMethodCallType0(_, pos, n, _, _, path) and
2901+
private Type inferFunctionCallTypeNonSelf(AstNode n, FunctionPosition pos, TypePath path) {
2902+
result = inferFunctionCallType0(_, pos, n, _, _, path) and
28842903
not pos.isSelf()
28852904
}
28862905

@@ -2893,8 +2912,8 @@ private Type inferMethodCallTypeNonSelf(AstNode n, FunctionPosition pos, TypePat
28932912
*/
28942913
pragma[nomagic]
28952914
private Type inferMethodCallTypeSelf(MethodCall mc, AstNode n, DerefChain derefChain, TypePath path) {
2896-
exists(MethodCallMatchingInput::AccessPosition apos, BorrowKind borrow, TypePath path0 |
2897-
result = inferMethodCallType0(mc, apos, n, derefChain, borrow, path0) and
2915+
exists(FunctionCallMatchingInput::AccessPosition apos, BorrowKind borrow, TypePath path0 |
2916+
result = inferFunctionCallType0(mc, apos, n, derefChain, borrow, path0) and
28982917
apos.isSelf()
28992918
|
29002919
borrow.isNoBorrow() and
@@ -2930,7 +2949,7 @@ private Type inferMethodCallTypeSelf(MethodCall mc, AstNode n, DerefChain derefC
29302949
}
29312950

29322951
private Type inferMethodCallTypePreCheck(AstNode n, FunctionPosition pos, TypePath path) {
2933-
result = inferMethodCallTypeNonSelf(n, pos, path)
2952+
result = inferFunctionCallTypeNonSelf(n, pos, path)
29342953
or
29352954
exists(MethodCall mc |
29362955
result = inferMethodCallTypeSelf(mc, n, DerefChain::nil(), path) and
@@ -3173,11 +3192,11 @@ private module OperationMatchingInput implements MatchingInputSig {
31733192
private import codeql.rust.elements.internal.OperationImpl::Impl as OperationImpl
31743193
import FunctionPositionMatchingInput
31753194

3176-
class Declaration extends MethodCallMatchingInput::Declaration {
3195+
class Declaration extends FunctionCallMatchingInput::Declaration {
31773196
private Method getSelfOrImpl() {
3178-
result = m
3197+
result = f
31793198
or
3180-
m.implements(result)
3199+
f.implements(result)
31813200
}
31823201

31833202
pragma[nomagic]
@@ -3220,7 +3239,7 @@ private module OperationMatchingInput implements MatchingInputSig {
32203239

32213240
Declaration getTarget() {
32223241
exists(ImplOrTraitItemNode i |
3223-
result.isMethod(i, this.resolveCallTarget(i, _, _)) // mutual recursion
3242+
result.isAssocFunction(i, this.resolveCallTarget(i, _, _)) // mutual recursion
32243243
)
32253244
}
32263245
}

0 commit comments

Comments
 (0)