Skip to content

Commit ca2838b

Browse files
committed
Address review comments
1 parent 7a6ab70 commit ca2838b

File tree

4 files changed

+44
-27
lines changed

4 files changed

+44
-27
lines changed

rust/ql/lib/codeql/rust/elements/internal/InvocationExprImpl.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ module Impl {
66

77
private newtype TArgumentPosition =
88
TPositionalArgumentPosition(int i) {
9+
// For the type `FunctionPosition` used by type inference, we work with function-call syntax
10+
// adjusted positions, so a call like `x.m(a, b, c)` needs positions `0` through `3`; for this
11+
// reason, there is no `- 1` after `max(...)` below.
912
i in [0 .. max([any(ParamList l).getNumberOfParams(), any(ArgList l).getNumberOfArgs()])]
1013
} or
1114
TSelfArgumentPosition() or

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

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ private class FunctionDeclaration extends Function {
343343
}
344344
}
345345

346-
private class AssocFunction extends FunctionDeclaration {
347-
AssocFunction() { this.isAssoc(_) }
346+
private class AssocFunctionDeclaration extends FunctionDeclaration {
347+
AssocFunctionDeclaration() { this.isAssoc(_) }
348348
}
349349

350350
pragma[nomagic]
@@ -1114,10 +1114,10 @@ private Trait getCallExprTraitQualifier(CallExpr ce) {
11141114
}
11151115

11161116
pragma[nomagic]
1117-
private predicate nonAssocFunction(ItemNode i) { not i instanceof AssocFunction }
1117+
private predicate nonAssocFunction(ItemNode i) { not i instanceof AssocFunctionDeclaration }
11181118

11191119
/**
1120-
* A call expression that can resolve to something that is not an associated
1120+
* A call expression that can only resolve to something that is not an associated
11211121
* function, and hence does not need type inference for resolution.
11221122
*/
11231123
private class NonAssocCallExpr extends CallExpr {
@@ -1201,9 +1201,7 @@ private module ContextTyping {
12011201
abstract class ContextTypedCallCand extends AstNode {
12021202
abstract Type getTypeArgument(TypeArgumentPosition apos, TypePath path);
12031203

1204-
private predicate hasTypeArgument(TypeArgumentPosition apos) {
1205-
exists(this.getTypeArgument(apos, _))
1206-
}
1204+
predicate hasTypeArgument(TypeArgumentPosition apos) { exists(this.getTypeArgument(apos, _)) }
12071205

12081206
/**
12091207
* Holds if this call resolves to `target` inside `i`, and the return type
@@ -2209,7 +2207,7 @@ private module AssocFunctionResolution {
22092207
* in `derefChain` and `borrow`.
22102208
*/
22112209
pragma[nomagic]
2212-
AssocFunction resolveCallTarget(
2210+
AssocFunctionDeclaration resolveCallTarget(
22132211
ImplOrTraitItemNode i, FunctionPosition selfPos, DerefChain derefChain, BorrowKind borrow
22142212
) {
22152213
exists(AssocFunctionCallCand afcc |
@@ -2288,7 +2286,9 @@ private module AssocFunctionResolution {
22882286
exists(getCallExprPathQualifier(this)) and
22892287
// even if a target cannot be resolved by path resolution, it may still
22902288
// be possible to resolve a blanket implementation (so not `forex`)
2291-
forall(ItemNode i | i = CallExprImpl::getResolvedFunction(this) | i instanceof AssocFunction)
2289+
forall(ItemNode i | i = CallExprImpl::getResolvedFunction(this) |
2290+
i instanceof AssocFunctionDeclaration
2291+
)
22922292
}
22932293

22942294
override predicate hasNameAndArity(string name, int arity) {
@@ -2373,7 +2373,9 @@ private module AssocFunctionResolution {
23732373
}
23742374

23752375
pragma[nomagic]
2376-
private AssocFunction getAssocFunctionSuccessor(ImplOrTraitItemNode i, string name, int arity) {
2376+
private AssocFunctionDeclaration getAssocFunctionSuccessor(
2377+
ImplOrTraitItemNode i, string name, int arity
2378+
) {
23772379
result = i.getASuccessor(name) and
23782380
arity = result.getNumberOfParamsInclSelf()
23792381
}
@@ -2488,7 +2490,7 @@ private module AssocFunctionResolution {
24882490
}
24892491

24902492
pragma[nomagic]
2491-
AssocFunction resolveCallTargetCand(ImplOrTraitItemNode i) {
2493+
AssocFunctionDeclaration resolveCallTargetCand(ImplOrTraitItemNode i) {
24922494
exists(string name, int arity |
24932495
this.selfArgIsInstantiationOf(i, name, arity) and
24942496
result = getAssocFunctionSuccessor(i, name, arity)
@@ -2497,7 +2499,7 @@ private module AssocFunctionResolution {
24972499

24982500
/** Gets the associated function targeted by this call, if any. */
24992501
pragma[nomagic]
2500-
AssocFunction resolveCallTarget(ImplOrTraitItemNode i) {
2502+
AssocFunctionDeclaration resolveCallTarget(ImplOrTraitItemNode i) {
25012503
result = this.resolveCallTargetCand(i) and
25022504
not FunctionOverloading::functionResolutionDependsOnArgument(i, result, _, _)
25032505
or
@@ -2896,18 +2898,15 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
28962898
result = this.getInferredNonSelfType(pos, path)
28972899
}
28982900

2899-
private AssocFunction getTarget(ImplOrTraitItemNode i, string derefChainBorrow) {
2901+
private AssocFunctionDeclaration getTarget(ImplOrTraitItemNode i, string derefChainBorrow) {
29002902
exists(DerefChain derefChain, BorrowKind borrow |
29012903
derefChainBorrow = encodeDerefChainBorrow(derefChain, borrow) and
29022904
result = super.resolveCallTarget(i, _, derefChain, borrow) // mutual recursion; resolving method calls requires resolving types and vice versa
29032905
)
29042906
}
29052907

29062908
override Declaration getTarget(string derefChainBorrow) {
2907-
exists(ImplOrTraitItemNodeOption i, AssocFunction f |
2908-
f = this.getTarget(i.asSome(), derefChainBorrow) and
2909-
result = TFunctionDeclaration(i, f)
2910-
)
2909+
exists(ImplOrTraitItemNode i | result.isAssocFunction(i, this.getTarget(i, derefChainBorrow)))
29112910
}
29122911

29132912
pragma[nomagic]
@@ -2926,7 +2925,9 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
29262925
}
29272926
}
29282927

2929-
private class NonAssocFunctionCallAccess extends Access instanceof NonAssocCallExpr {
2928+
private class NonAssocFunctionCallAccess extends Access instanceof NonAssocCallExpr,
2929+
CallExprImpl::CallExprCall
2930+
{
29302931
pragma[nomagic]
29312932
override Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
29322933
result = NonAssocCallExpr.super.getTypeArgument(apos, path)
@@ -2949,11 +2950,9 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
29492950

29502951
pragma[nomagic]
29512952
private Declaration getTarget() {
2952-
exists(ImplOrTraitItemNodeOption i, FunctionDeclaration f |
2953-
f = super.resolveCallTargetViaPathResolution() and
2954-
f.isDirectlyFor(i) and
2955-
result = TFunctionDeclaration(i, f)
2956-
)
2953+
result =
2954+
TFunctionDeclaration(ImplOrTraitItemNodeOption::none_(),
2955+
super.resolveCallTargetViaPathResolution())
29572956
}
29582957

29592958
override Declaration getTarget(string derefChainBorrow) {
@@ -2964,9 +2963,16 @@ private module FunctionCallMatchingInput implements MatchingWithEnvironmentInput
29642963
pragma[nomagic]
29652964
override predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path) {
29662965
derefChainBorrow = noDerefChainBorrow() and
2967-
exists(ImplOrTraitItemNodeOption i, FunctionDeclaration f |
2968-
TFunctionDeclaration(i, f) = this.getTarget() and
2969-
this.hasUnknownTypeAt(i.asSome(), f, pos, path)
2966+
exists(FunctionDeclaration f, TypeParameter tp |
2967+
f = super.resolveCallTargetViaPathResolution() and
2968+
pos.isReturn() and
2969+
tp = f.getReturnType(_, path) and
2970+
not tp = f.getParameterType(_, _, _) and
2971+
// check that no explicit type arguments have been supplied for `tp`
2972+
not exists(TypeArgumentPosition tapos |
2973+
this.hasTypeArgument(tapos) and
2974+
TTypeParamTypeParameter(tapos.asTypeParam()) = tp
2975+
)
29702976
)
29712977
}
29722978
}
@@ -3135,6 +3141,11 @@ private module TupleLikeConstructionMatchingInput implements MatchingInputSig {
31353141
class Declaration = TupleLikeConstructor;
31363142

31373143
class Access extends NonAssocCallExpr, ContextTyping::ContextTypedCallCand {
3144+
Access() {
3145+
this instanceof CallExprImpl::TupleStructExpr or
3146+
this instanceof CallExprImpl::TupleVariantExpr
3147+
}
3148+
31383149
override Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
31393150
result = NonAssocCallExpr.super.getTypeArgument(apos, path)
31403151
}

rust/ql/test/library-tests/type-inference/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2698,7 +2698,7 @@ mod context_typed {
26982698
let s = Default::default(); // $ target=default type=s:S
26992699
S::f(s); // $ target=f
27002700

2701-
let z = free_function(); // $ target=free_function MISSING: type=z:i32
2701+
let z = free_function(); // $ target=free_function type=z:i32
27022702
x.push(z); // $ target=push
27032703
}
27042704
}

rust/ql/test/library-tests/type-inference/type-inference.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12195,10 +12195,13 @@ inferType
1219512195
| main.rs:2698:17:2698:34 | ...::default(...) | | main.rs:2639:5:2640:13 | S |
1219612196
| main.rs:2699:9:2699:15 | ...::f(...) | | {EXTERNAL LOCATION} | () |
1219712197
| main.rs:2699:14:2699:14 | s | | main.rs:2639:5:2640:13 | S |
12198+
| main.rs:2701:13:2701:13 | z | | {EXTERNAL LOCATION} | i32 |
12199+
| main.rs:2701:17:2701:31 | free_function(...) | | {EXTERNAL LOCATION} | i32 |
1219812200
| main.rs:2702:9:2702:9 | x | | {EXTERNAL LOCATION} | Vec |
1219912201
| main.rs:2702:9:2702:9 | x | A | {EXTERNAL LOCATION} | Global |
1220012202
| main.rs:2702:9:2702:9 | x | T | {EXTERNAL LOCATION} | i32 |
1220112203
| main.rs:2702:9:2702:17 | x.push(...) | | {EXTERNAL LOCATION} | () |
12204+
| main.rs:2702:16:2702:16 | z | | {EXTERNAL LOCATION} | i32 |
1220212205
| main.rs:2709:14:2709:17 | SelfParam | | main.rs:2707:5:2715:5 | Self [trait MyTrait] |
1220312206
| main.rs:2712:14:2712:18 | SelfParam | | {EXTERNAL LOCATION} | & |
1220412207
| main.rs:2712:14:2712:18 | SelfParam | TRef | main.rs:2707:5:2715:5 | Self [trait MyTrait] |

0 commit comments

Comments
 (0)