Skip to content

Commit 93ab989

Browse files
committed
wip3
1 parent 014a611 commit 93ab989

3 files changed

Lines changed: 65 additions & 151 deletions

File tree

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

Lines changed: 32 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ private class FunctionDeclaration extends Function {
10331033
result = [this.getGenericParamList().getATypeParam(), i.asSome().getTypeParam(_)]
10341034
}
10351035

1036-
TypeParameter getTypeParameter(ImplOrTraitItemNodeOption i, TypeParameterPosition ppos) {
1036+
private TypeParameter getTypeParameter(ImplOrTraitItemNodeOption i, TypeParameterPosition ppos) {
10371037
result = ppos.asTypeParameter() and
10381038
(
10391039
result = TTypeParamTypeParameter(this.getTypeParam(i))
@@ -1053,45 +1053,22 @@ private class FunctionDeclaration extends Function {
10531053
)
10541054
}
10551055

1056-
pragma[nomagic]
1057-
Type getParameterType(ImplOrTraitItemNodeOption i, int pos, TypePath path) {
1058-
i = parent and
1059-
exists(FunctionPosition fpos | pos = fpos.asPosition() |
1060-
// exists(i.asSome()) and
1061-
result = getAssocFunctionTypeAt(this, i.asSome(), fpos, path)
1056+
TypeParameter getTypeParameter(TypeParameterPosition ppos) {
1057+
exists(ImplOrTraitItemNodeOption i | result = this.getTypeParameter(i, ppos) |
1058+
i.isNone()
10621059
or
1063-
// result = fpos.getTypeMention(this).getTypeAt(path)
1064-
i.isNone() and
1065-
result = this.getParam(pos).getTypeRepr().(TypeMention).getTypeAt(path)
1060+
i.asSome().getAnAssocItem() = this
10661061
)
10671062
}
10681063

1069-
private Type resolveRetType(ImplOrTraitItemNodeOption i, TypePath path) {
1070-
i = parent and
1071-
(
1072-
result =
1073-
getAssocFunctionTypeAt(this, i.asSome(), any(FunctionPosition ret | ret.isReturn()), path)
1074-
or
1075-
i.isNone() and
1076-
result = getReturnTypeMention(this).getTypeAt(path)
1064+
pragma[nomagic]
1065+
Type getParameterType(int j, TypePath path) {
1066+
exists(FunctionPosition fpos | j = fpos.asPosition() |
1067+
result = fpos.getTypeMention(this).getTypeAt(path)
10771068
)
10781069
}
10791070

1080-
Type getReturnType(ImplOrTraitItemNodeOption i, TypePath path) {
1081-
if this.isAsync()
1082-
then
1083-
i = parent and
1084-
path.isEmpty() and
1085-
result = getFutureTraitType()
1086-
or
1087-
exists(TypePath suffix |
1088-
result = this.resolveRetType(i, suffix) and
1089-
path = TypePath::cons(getDynFutureOutputTypeParameter(), suffix)
1090-
)
1091-
else result = this.resolveRetType(i, path)
1092-
}
1093-
1094-
Type getReturnType2(TypePath path) {
1071+
Type getReturnType(TypePath path) {
10951072
if this.isAsync()
10961073
then
10971074
path.isEmpty() and
@@ -1103,14 +1080,6 @@ private class FunctionDeclaration extends Function {
11031080
)
11041081
else result = getReturnTypeMention(this).getTypeAt(path)
11051082
}
1106-
1107-
string toStringExt(ImplOrTraitItemNode i) {
1108-
i = parent.asSome() and
1109-
if this = i.getAnAssocItem()
1110-
then result = this.toString()
1111-
else
1112-
result = this + " [" + [i.(Impl).getSelfTy().toString(), i.(Trait).getName().toString()] + "]"
1113-
}
11141083
}
11151084

11161085
private class AssocFunctionDeclaration extends FunctionDeclaration {
@@ -1316,16 +1285,12 @@ private module ContextualTyping {
13161285
*/
13171286
pragma[nomagic]
13181287
private predicate assocFunctionMentionsTypeParameterAtNonRetPos(
1319-
ImplOrTraitItemNode i, Function f, TypeParameter tp
1288+
FunctionDeclaration f, TypeParameter tp
13201289
) {
1321-
exists(FunctionPosition nonRetPos |
1322-
not nonRetPos.isReturn() and
1323-
not nonRetPos.isTypeQualifier() and
1324-
tp = getAssocFunctionTypeAt(f, i, nonRetPos, _)
1325-
)
1290+
tp = f.getParameterType(_, _)
13261291
or
13271292
exists(TypeParameter mid |
1328-
assocFunctionMentionsTypeParameterAtNonRetPos(i, f, mid) and
1293+
assocFunctionMentionsTypeParameterAtNonRetPos(f, mid) and
13291294
tp = getATypeParameterConstraint(mid).getTypeAt(_)
13301295
)
13311296
}
@@ -1342,11 +1307,10 @@ private module ContextualTyping {
13421307
*/
13431308
pragma[nomagic]
13441309
private predicate assocFunctionReturnContextTypedAt(
1345-
ImplOrTraitItemNode i, Function f, FunctionPosition pos, TypePath path, TypeParameter tp
1310+
FunctionDeclaration f, TypePath path, TypeParameter tp
13461311
) {
1347-
pos.isReturn() and
1348-
tp = getAssocFunctionTypeAt(f, i, pos, path) and
1349-
not assocFunctionMentionsTypeParameterAtNonRetPos(i, f, tp)
1312+
tp = f.getReturnType(path) and
1313+
not assocFunctionMentionsTypeParameterAtNonRetPos(f, tp)
13501314
}
13511315

13521316
/**
@@ -1362,16 +1326,14 @@ private module ContextualTyping {
13621326
}
13631327

13641328
/**
1365-
* Holds if this call resolves to `target` inside `i`, and the return type
1366-
* at `pos` and `path` may have to be inferred from the context.
1329+
* Holds if this call resolves to `target`, and the return type at `pos` and
1330+
* `path` may have to be inferred from the context.
13671331
*/
1368-
bindingset[this, i, target]
1369-
predicate hasUnknownTypeAt(
1370-
ImplOrTraitItemNode i, FunctionDeclaration target, FunctionPosition pos, TypePath path
1371-
) {
1332+
bindingset[this, target]
1333+
predicate hasUnknownTypeAt0(FunctionDeclaration target, TypePath path) {
13721334
exists(TypeParameter tp, TypeParameterPosition tppos |
1373-
assocFunctionReturnContextTypedAt(i, target, pos, path, tp) and
1374-
tp = target.getTypeParameter(ImplOrTraitItemNodeOption::some(i), tppos) and
1335+
assocFunctionReturnContextTypedAt(target, path, tp) and
1336+
tp = target.getTypeParameter(tppos) and
13751337
// check that no explicit type arguments have been supplied for `tp`
13761338
not exists(TypeArgumentPosition tapos |
13771339
this.hasTypeArgument(tapos) and
@@ -2850,23 +2812,7 @@ private module AssocFunctionResolution {
28502812
* like `foo.bar(baz)` and `Foo::bar(baz)`.
28512813
*/
28522814
private module FunctionCallMatchingInput {
2853-
final class Declaration extends FunctionDeclaration {
2854-
TypeParameter getTypeParameter(TypeParameterPosition ppos) {
2855-
exists(ImplOrTraitItemNodeOption i | result = this.getTypeParameter(i, ppos) |
2856-
i.isNone()
2857-
or
2858-
i.asSome().getAnAssocItem() = this
2859-
)
2860-
}
2861-
2862-
Type getParameterType(int j, TypePath path) {
2863-
exists(FunctionPosition fpos | j = fpos.asPosition() |
2864-
result = fpos.getTypeMention(this).getTypeAt(path)
2865-
)
2866-
}
2867-
2868-
Type getReturnType(TypePath path) { result = this.getReturnType2(path) }
2869-
}
2815+
class Declaration = FunctionDeclaration;
28702816

28712817
pragma[nomagic]
28722818
private TypeMention getAdditionalTypeParameterConstraint(TypeParameter tp, Declaration decl) {
@@ -2920,7 +2866,7 @@ private module FunctionCallMatchingInput {
29202866
* Holds if the return type of this call at `path` may have to be inferred
29212867
* from the context.
29222868
*/
2923-
abstract predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path);
2869+
abstract predicate hasUnknownTypeAt(string derefChainBorrow, TypePath path);
29242870
}
29252871

29262872
private class AssocFunctionCallAccess extends AccessImpl instanceof AssocFunctionResolution::AssocFunctionCall
@@ -2984,23 +2930,17 @@ private module FunctionCallMatchingInput {
29842930
}
29852931

29862932
pragma[nomagic]
2987-
override predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path) {
2988-
exists(ImplOrTraitItemNode i |
2989-
this.hasUnknownTypeAt(i, this.getTarget(i, derefChainBorrow), pos, path)
2990-
)
2933+
override predicate hasUnknownTypeAt(string derefChainBorrow, TypePath path) {
2934+
this.hasUnknownTypeAt0(this.getTarget(_, derefChainBorrow), path)
29912935
or
29922936
derefChainBorrow = noDerefChainBorrow() and
2993-
forex(ImplOrTraitItemNode i, Function f |
2994-
f = CallExprImpl::getResolvedFunction(this) and
2995-
f = i.getAnAssocItem()
2996-
|
2997-
this.hasUnknownTypeAt(i, f, pos, path)
2937+
forex(Function f | f = CallExprImpl::getResolvedFunction(this) |
2938+
this.hasUnknownTypeAt0(f, path)
29982939
)
29992940
or
30002941
exists(DerefChain derefChain, BorrowKind borrow |
30012942
super.resolutionDependsOnReturnType(derefChain, borrow) and
30022943
derefChainBorrow = encodeDerefChainBorrow(derefChain, borrow) and
3003-
pos.isReturn() and
30042944
path.isEmpty()
30052945
)
30062946
}
@@ -3041,13 +2981,12 @@ private module FunctionCallMatchingInput {
30412981
}
30422982

30432983
pragma[nomagic]
3044-
override predicate hasUnknownTypeAt(string derefChainBorrow, FunctionPosition pos, TypePath path) {
2984+
override predicate hasUnknownTypeAt(string derefChainBorrow, TypePath path) {
30452985
derefChainBorrow = noDerefChainBorrow() and
30462986
exists(FunctionDeclaration f, TypeParameter tp |
30472987
f = super.resolveCallTargetViaPathResolution() and
3048-
pos.isReturn() and
3049-
tp = f.getReturnType(_, path) and
3050-
not tp = f.getParameterType(_, _, _) and
2988+
tp = f.getReturnType(path) and
2989+
not tp = f.getParameterType(_, _) and
30512990
// check that no explicit type arguments have been supplied for `tp`
30522991
not exists(TypeArgumentPosition tapos |
30532992
this.hasTypeArgument(tapos) and
@@ -3120,10 +3059,7 @@ pragma[nomagic]
31203059
private Type inferUnknownType(AstNode n, TypePath path) {
31213060
result = TUnknownType() and
31223061
(
3123-
exists(FunctionCallMatchingInput::Access call, FunctionPosition pos |
3124-
n = call.getNodeAt(pos) and
3125-
call.hasUnknownTypeAt(_, pos, path)
3126-
)
3062+
n.(FunctionCallMatchingInput::Access).hasUnknownTypeAt(_, path)
31273063
or
31283064
n.(Input3::Construction).hasUnknownReturnTypeAt(path)
31293065
or

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7787,7 +7787,6 @@ inferType
77877787
| main.rs:764:17:764:26 | ...::m2(...) | | {EXTERNAL LOCATION} | i32 |
77887788
| main.rs:764:24:764:25 | S1 | | main.rs:606:5:607:14 | S1 |
77897789
| main.rs:765:13:765:13 | y | | {EXTERNAL LOCATION} | i32 |
7790-
| main.rs:765:22:765:31 | ...::m2(...) | | {EXTERNAL LOCATION} | i32 |
77917790
| main.rs:765:29:765:30 | S2 | | main.rs:608:5:609:14 | S2 |
77927791
| main.rs:782:15:782:18 | SelfParam | | main.rs:770:5:774:5 | MyEnum |
77937792
| main.rs:782:15:782:18 | SelfParam | A | main.rs:781:10:781:10 | T |

0 commit comments

Comments
 (0)