Skip to content

Commit 014a611

Browse files
committed
wip2
1 parent dc9b92d commit 014a611

2 files changed

Lines changed: 29 additions & 73 deletions

File tree

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

Lines changed: 22 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -425,13 +425,10 @@ private module Input3 implements InputSig3 {
425425
final class Callable extends FunctionCallMatchingInput::Declaration {
426426
TypeMention getAdditionalTypeParameterConstraint(TypeParameter tp) {
427427
result =
428-
tp.(TypeParamTypeParameter)
429-
.getTypeParam()
430-
.getAdditionalTypeBound(this.getFunction(), _)
431-
.getTypeRepr()
428+
tp.(TypeParamTypeParameter).getTypeParam().getAdditionalTypeBound(this, _).getTypeRepr()
432429
}
433430

434-
AstNode getBody() { result = this.getFunction().getBody() }
431+
AstNode getBody() { result = super.getBody() }
435432
}
436433

437434
class Call extends FunctionCallMatchingInput::Access {
@@ -451,6 +448,15 @@ private module Input3 implements InputSig3 {
451448
call.(FunctionCallMatchingInput::Access).getInferredArgumentType(derefChainBorrow, pos, path)
452449
}
453450

451+
Type inferCallReturnType(Call call, TypePath path) {
452+
exists(TypePath path0 |
453+
result = M3::inferCallReturnTypeDefault(call, _, path0) and
454+
// index expression `x[i]` desugars to `*x.index(i)`, so we must account for
455+
// the implicit deref
456+
if call instanceof IndexExpr then path0.isCons(getRefTypeParameter(_), path) else path = path0
457+
)
458+
}
459+
454460
Type inferCallArgumentTypeContextual(AstNode n, TypePath path) {
455461
exists(FunctionCallMatchingInput::Access call, FunctionPosition pos |
456462
result = inferCallArgumentTypeContextual(call, pos.asPosition(), n, _, _, path) and
@@ -533,9 +539,9 @@ private module Input3 implements InputSig3 {
533539

534540
class Operator extends Callable {
535541
private Method getSelfOrImpl() {
536-
result = f
542+
result = this
537543
or
538-
f.implements(result)
544+
this.implements(result)
539545
}
540546

541547
pragma[nomagic]
@@ -580,7 +586,7 @@ private module Input3 implements InputSig3 {
580586

581587
Operator getTarget() {
582588
exists(ImplOrTraitItemNode i |
583-
result.isFunction(this.resolveCallTarget(i, _, _, _), false) // mutual recursion
589+
result = this.resolveCallTarget(i, _, _, _) // mutual recursion
584590
)
585591
}
586592
}
@@ -2844,78 +2850,28 @@ private module AssocFunctionResolution {
28442850
* like `foo.bar(baz)` and `Foo::bar(baz)`.
28452851
*/
28462852
private module FunctionCallMatchingInput {
2847-
private newtype TDeclaration =
2848-
TFunctionDeclaration(FunctionDeclaration f, boolean forIndexExpr) {
2849-
(
2850-
forIndexExpr = false
2851-
or
2852-
forIndexExpr = true and
2853-
exists(Function traitFunction |
2854-
traitFunction =
2855-
[any(IndexTrait t).getIndexFunction(), any(IndexMutTrait t).getIndexMutFunction()]
2856-
|
2857-
f = traitFunction
2858-
or
2859-
f.implements(traitFunction)
2860-
)
2861-
)
2862-
}
2863-
2864-
final class Declaration extends TFunctionDeclaration {
2865-
FunctionDeclaration f;
2866-
boolean forIndexExpr;
2867-
2868-
Declaration() { this = TFunctionDeclaration(f, forIndexExpr) }
2869-
2870-
FunctionDeclaration getFunction() { result = f }
2871-
2872-
// predicate isFunction(ImplOrTraitItemNodeOption i_, Function f_) {
2873-
// i_ = i and
2874-
// f_ = f
2875-
// }
2876-
predicate isFunction(Function f_, boolean forIndexExpr_) {
2877-
f_ = f and
2878-
forIndexExpr_ = forIndexExpr
2879-
}
2880-
2853+
final class Declaration extends FunctionDeclaration {
28812854
TypeParameter getTypeParameter(TypeParameterPosition ppos) {
2882-
exists(ImplOrTraitItemNodeOption i | result = f.getTypeParameter(i, ppos) |
2855+
exists(ImplOrTraitItemNodeOption i | result = this.getTypeParameter(i, ppos) |
28832856
i.isNone()
28842857
or
2885-
i.asSome().getAnAssocItem() = f
2858+
i.asSome().getAnAssocItem() = this
28862859
)
28872860
}
28882861

28892862
Type getParameterType(int j, TypePath path) {
28902863
exists(FunctionPosition fpos | j = fpos.asPosition() |
2891-
result = fpos.getTypeMention(f).getTypeAt(path)
2864+
result = fpos.getTypeMention(this).getTypeAt(path)
28922865
)
28932866
}
28942867

2895-
Type getReturnType(TypePath path) {
2896-
// index expression `x[i]` desugars to `*x.index(i)`, so we must account for
2897-
// the implicit deref
2898-
exists(TypePath path0 | result = f.getReturnType2(path0) |
2899-
forIndexExpr = true and
2900-
path0.isCons(getRefTypeParameter(_), path)
2901-
or
2902-
forIndexExpr = false and
2903-
path = path0
2904-
)
2905-
}
2906-
2907-
string toString() { result = f.toString() }
2908-
2909-
Location getLocation() { result = f.getLocation() }
2868+
Type getReturnType(TypePath path) { result = this.getReturnType2(path) }
29102869
}
29112870

29122871
pragma[nomagic]
29132872
private TypeMention getAdditionalTypeParameterConstraint(TypeParameter tp, Declaration decl) {
29142873
result =
2915-
tp.(TypeParamTypeParameter)
2916-
.getTypeParam()
2917-
.getAdditionalTypeBound(decl.getFunction(), _)
2918-
.getTypeRepr()
2874+
tp.(TypeParamTypeParameter).getTypeParam().getAdditionalTypeBound(decl, _).getTypeRepr()
29192875
}
29202876

29212877
bindingset[decl]
@@ -3024,10 +2980,7 @@ private module FunctionCallMatchingInput {
30242980
}
30252981

30262982
override Declaration getTarget(string derefChainBorrow) {
3027-
exists(ImplOrTraitItemNode i, boolean forIndexExpr |
3028-
result.isFunction(this.getTarget(i, derefChainBorrow), forIndexExpr) and
3029-
if this instanceof IndexExpr then forIndexExpr = true else forIndexExpr = false
3030-
)
2983+
exists(ImplOrTraitItemNode i | result = this.getTarget(i, derefChainBorrow))
30312984
}
30322985

30332986
pragma[nomagic]
@@ -3080,9 +3033,7 @@ private module FunctionCallMatchingInput {
30803033
}
30813034

30823035
pragma[nomagic]
3083-
private Declaration getTarget() {
3084-
result = TFunctionDeclaration(super.resolveCallTargetViaPathResolution(), false)
3085-
}
3036+
private Declaration getTarget() { result = super.resolveCallTargetViaPathResolution() }
30863037

30873038
override Declaration getTarget(string derefChainBorrow) {
30883039
result = this.getTarget() and

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,11 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
22822282
exists(ctx)
22832283
}
22842284

2285+
/**
2286+
* TODO
2287+
*/
2288+
Type inferCallReturnType(Call call, TypePath path);
2289+
22852290
/**
22862291
* Gets the contextually inferred type of call argument `n` at `path`. The context
22872292
* used is the target of the call, for example in
@@ -2852,7 +2857,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
28522857
or
28532858
result = inferTypeFromStep(n, path)
28542859
or
2855-
result = inferCallReturnType(n, _, path)
2860+
result = inferCallReturnType(n, path)
28562861
or
28572862
result = inferMemberAccessType(n, path)
28582863
or
@@ -2985,7 +2990,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
29852990
private module CallMatching = MatchingWithEnvironment<CallMatchingInput>;
29862991

29872992
pragma[nomagic]
2988-
private Type inferCallReturnType(Call call, CallResolutionContext ctx, TypePath path) {
2993+
Type inferCallReturnTypeDefault(Call call, CallResolutionContext ctx, TypePath path) {
29892994
result =
29902995
CallMatching::inferAccessType(call, ctx, CallMatchingInput::getReturnPosition(), path)
29912996
}

0 commit comments

Comments
 (0)