Skip to content

Commit c291ef2

Browse files
committed
wip12
1 parent 362047f commit c291ef2

2 files changed

Lines changed: 43 additions & 33 deletions

File tree

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,9 @@ private module Input3 implements InputSig3 {
450450
}
451451

452452
bindingset[derefChainBorrow]
453-
Type inferCallArgumentType(Call call, string derefChainBorrow, int i, TypePath path) {
454-
exists(FunctionPosition pos |
455-
i = pos.asPosition() and
456-
result = call.(FunctionCallMatchingInput::Access).getInferredType(derefChainBorrow, pos, path)
457-
)
453+
Type inferCallArgumentType(Call call, string derefChainBorrow, int pos, TypePath path) {
454+
result =
455+
call.(FunctionCallMatchingInput::Access).getInferredArgumentType(derefChainBorrow, pos, path)
458456
}
459457

460458
Type inferCallArgumentType(AstNode n, TypePath path) {
@@ -1039,9 +1037,6 @@ private class NonAssocCallExpr extends CallExpr {
10391037

10401038
pragma[nomagic]
10411039
Type getInferredType(FunctionPosition pos, TypePath path) {
1042-
pos.isTypeQualifier() and
1043-
result = getCallExprTypeQualifier(this, path, false)
1044-
or
10451040
result = inferType(this.getNodeAt(pos), path)
10461041
}
10471042
}
@@ -2587,7 +2582,7 @@ private module AssocFunctionResolution {
25872582
* A matching configuration for resolving types of function call expressions
25882583
* like `foo.bar(baz)` and `Foo::bar(baz)`.
25892584
*/
2590-
private module FunctionCallMatchingInput implements MatchingWithEnvironmentInputSig {
2585+
private module FunctionCallMatchingInput {
25912586
import FunctionPositionMatchingInput
25922587

25932588
private newtype TDeclaration =
@@ -2681,9 +2676,7 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
26812676
}
26822677

26832678
bindingset[derefChainBorrow]
2684-
additional predicate decodeDerefChainBorrow(
2685-
string derefChainBorrow, DerefChain derefChain, BorrowKind borrow
2686-
) {
2679+
predicate decodeDerefChainBorrow(string derefChainBorrow, DerefChain derefChain, BorrowKind borrow) {
26872680
exists(int i |
26882681
i = derefChainBorrow.indexOf(";") and
26892682
derefChain = derefChainBorrow.prefix(i) and
@@ -2704,8 +2697,10 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
27042697
abstract private class AccessImpl extends ContextTyping::ContextTypedCallCand {
27052698
abstract AstNode getNodeAt(FunctionPosition pos);
27062699

2700+
// bindingset[derefChainBorrow]
2701+
// abstract Type getInferredType(string derefChainBorrow, FunctionPosition pos, TypePath path);
27072702
bindingset[derefChainBorrow]
2708-
abstract Type getInferredType(string derefChainBorrow, FunctionPosition pos, TypePath path);
2703+
abstract Type getInferredArgumentType(string derefChainBorrow, int pos, TypePath path);
27092704

27102705
abstract Declaration getTarget(string derefChainBorrow);
27112706

@@ -2740,22 +2735,26 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
27402735
}
27412736

27422737
pragma[nomagic]
2743-
private Type getInferredSelfType(FunctionPosition pos, string derefChainBorrow, TypePath path) {
2744-
exists(DerefChain derefChain, BorrowKind borrow |
2745-
result = super.getSelfTypeAt(pos, derefChain, borrow, path) and
2738+
private Type getInferredSelfType(int pos, string derefChainBorrow, TypePath path) {
2739+
exists(FunctionPosition fpos, DerefChain derefChain, BorrowKind borrow |
2740+
result = super.getSelfTypeAt(fpos, derefChain, borrow, path) and
27462741
derefChainBorrow = encodeDerefChainBorrow(derefChain, borrow) and
2747-
super.hasReceiverAtPos(pos)
2742+
super.hasReceiverAtPos(fpos) and
2743+
pos = fpos.asPosition()
27482744
)
27492745
}
27502746

27512747
pragma[nomagic]
2752-
private Type getInferredNonSelfType(FunctionPosition pos, TypePath path) {
2753-
not super.hasReceiverAtPos(pos) and
2754-
result = super.getTypeAt(pos, path)
2748+
private Type getInferredNonSelfType(int pos, TypePath path) {
2749+
exists(FunctionPosition fpos |
2750+
not super.hasReceiverAtPos(fpos) and
2751+
result = super.getTypeAt(fpos, path) and
2752+
pos = fpos.asPosition()
2753+
)
27552754
}
27562755

27572756
bindingset[derefChainBorrow]
2758-
override Type getInferredType(string derefChainBorrow, FunctionPosition pos, TypePath path) {
2757+
override Type getInferredArgumentType(string derefChainBorrow, int pos, TypePath path) {
27592758
result = this.getInferredSelfType(pos, derefChainBorrow, path)
27602759
or
27612760
result = this.getInferredNonSelfType(pos, path)
@@ -2811,14 +2810,17 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
28112810
}
28122811

28132812
pragma[nomagic]
2814-
private Type getInferredType(FunctionPosition pos, TypePath path) {
2815-
result = super.getInferredType(pos, path)
2813+
private Type getInferredArgumentType(int pos, TypePath path) {
2814+
exists(FunctionPosition fpos |
2815+
result = super.getInferredType(fpos, path) and
2816+
pos = fpos.asPosition()
2817+
)
28162818
}
28172819

28182820
bindingset[derefChainBorrow]
2819-
override Type getInferredType(string derefChainBorrow, FunctionPosition pos, TypePath path) {
2821+
override Type getInferredArgumentType(string derefChainBorrow, int pos, TypePath path) {
28202822
exists(derefChainBorrow) and
2821-
result = this.getInferredType(pos, path)
2823+
result = this.getInferredArgumentType(pos, path)
28222824
}
28232825

28242826
pragma[nomagic]

shared/typeinference/codeql/typeinference/internal/TypeInference.qll

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,7 +2219,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
22192219
* Gets the declared type of the `i`th parameter of this callable at `path`.
22202220
*
22212221
* This should also include (possibly implicit) `this`/`self` parameters,
2222-
* using index `-1`.
2222+
* using index `0`.
22232223
*/
22242224
Type getParameterType(int i, TypePath path);
22252225

@@ -2241,7 +2241,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
22412241
/**
22422242
* Gets the AST node corresponding to the `i`th argument of this call.
22432243
*
2244-
* This should include the receiver argument for method calls, using index `-1`.
2244+
* This should include the receiver argument for method calls, using index `0`.
22452245
*/
22462246
AstNode getArgument(int i);
22472247

@@ -2581,13 +2581,16 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
25812581
apos = dpos
25822582
}
25832583

2584+
/** Gets the position used to represent the return type of a callable/type of a call. */
2585+
additional int getReturnPosition() { result = -1 }
2586+
25842587
final private class CallableFinal = Callable;
25852588

25862589
class Declaration extends CallableFinal {
25872590
Type getDeclaredType(DeclarationPosition dpos, TypePath path) {
25882591
result = this.getParameterType(dpos, path)
25892592
or
2590-
dpos = -2 and
2593+
dpos = getReturnPosition() and
25912594
result = this.getReturnType(path)
25922595
}
25932596
}
@@ -2605,14 +2608,18 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
26052608
final private class CallFinal = Call;
26062609

26072610
class Access extends CallFinal {
2611+
pragma[nomagic]
2612+
private Type getInferredResultType(AccessPosition apos, TypePath path) {
2613+
result = inferType(this, path) and
2614+
apos = getReturnPosition()
2615+
}
2616+
26082617
bindingset[e]
26092618
Type getInferredType(AccessEnvironment e, AccessPosition apos, TypePath path) {
26102619
result = inferCallArgumentType(this, e, apos, path)
26112620
or
2612-
result = inferType(this, path) and
2613-
exists(e) and
2614-
apos = -2
2615-
// todo: adjust return type
2621+
result = this.getInferredResultType(apos, path) and
2622+
exists(e)
26162623
}
26172624

26182625
Declaration getTarget(AccessEnvironment e) { result = super.getTarget(e) }
@@ -2622,7 +2629,8 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
26222629
private module CallMatching = MatchingWithEnvironment<CallMatchingInput>;
26232630

26242631
private Type inferCallReturnType(Call call, CallResolutionContext ctx, TypePath path) {
2625-
result = CallMatching::inferAccessType(call, ctx, -2, path)
2632+
result =
2633+
CallMatching::inferAccessType(call, ctx, CallMatchingInput::getReturnPosition(), path)
26262634
}
26272635

26282636
Type inferCallArgumentType(

0 commit comments

Comments
 (0)