Skip to content

Commit c3189e9

Browse files
committed
wip12
1 parent 362047f commit c3189e9

2 files changed

Lines changed: 50 additions & 38 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: 23 additions & 13 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

@@ -2483,7 +2483,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
24832483
private predicate hasUnknownType(AstNode n) { hasUnknownTypeAt(n, _) }
24842484

24852485
pragma[nomagic]
2486-
private Type inferTypeTopDownCand(AstNode n, TypePath path) {
2486+
private Type inferTypeContextualCand(AstNode n, TypePath path) {
24872487
result = inferTypeTopDown(n, path)
24882488
or
24892489
result = inferCallArgumentType(n, path)
@@ -2496,8 +2496,8 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
24962496
}
24972497

24982498
pragma[nomagic]
2499-
private Type inferTypeTopDownCand(AstNode n, TypePath prefix, TypePath path) {
2500-
result = inferTypeTopDownCand(n, path) and
2499+
private Type inferTypeContextualCand(AstNode n, TypePath prefix, TypePath path) {
2500+
result = inferTypeContextualCand(n, path) and
25012501
hasUnknownType(n) and
25022502
prefix = path.getAPrefix()
25032503
}
@@ -2518,9 +2518,10 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
25182518
or
25192519
result = inferCallReturnType(n, _, path)
25202520
or
2521-
// top-down inference
2521+
// contextual typing: only propagate type information from surrounding context
2522+
// into a node which has an explicitly unknown type
25222523
exists(TypePath prefix |
2523-
result = inferTypeTopDownCand(n, prefix, path) and
2524+
result = inferTypeContextualCand(n, prefix, path) and
25242525
hasUnknownTypeAt(n, prefix)
25252526
)
25262527
}
@@ -2581,13 +2582,16 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
25812582
apos = dpos
25822583
}
25832584

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

25862590
class Declaration extends CallableFinal {
25872591
Type getDeclaredType(DeclarationPosition dpos, TypePath path) {
25882592
result = this.getParameterType(dpos, path)
25892593
or
2590-
dpos = -2 and
2594+
dpos = getReturnPosition() and
25912595
result = this.getReturnType(path)
25922596
}
25932597
}
@@ -2605,14 +2609,18 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
26052609
final private class CallFinal = Call;
26062610

26072611
class Access extends CallFinal {
2612+
pragma[nomagic]
2613+
private Type getInferredResultType(AccessPosition apos, TypePath path) {
2614+
result = inferType(this, path) and
2615+
apos = getReturnPosition()
2616+
}
2617+
26082618
bindingset[e]
26092619
Type getInferredType(AccessEnvironment e, AccessPosition apos, TypePath path) {
26102620
result = inferCallArgumentType(this, e, apos, path)
26112621
or
2612-
result = inferType(this, path) and
2613-
exists(e) and
2614-
apos = -2
2615-
// todo: adjust return type
2622+
result = this.getInferredResultType(apos, path) and
2623+
exists(e)
26162624
}
26172625

26182626
Declaration getTarget(AccessEnvironment e) { result = super.getTarget(e) }
@@ -2621,8 +2629,10 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
26212629

26222630
private module CallMatching = MatchingWithEnvironment<CallMatchingInput>;
26232631

2632+
pragma[nomagic]
26242633
private Type inferCallReturnType(Call call, CallResolutionContext ctx, TypePath path) {
2625-
result = CallMatching::inferAccessType(call, ctx, -2, path)
2634+
result =
2635+
CallMatching::inferAccessType(call, ctx, CallMatchingInput::getReturnPosition(), path)
26262636
}
26272637

26282638
Type inferCallArgumentType(

0 commit comments

Comments
 (0)