Skip to content

Commit dc9b92d

Browse files
committed
wip
1 parent e2d2b38 commit dc9b92d

2 files changed

Lines changed: 43 additions & 28 deletions

File tree

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

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ private module Input3 implements InputSig3 {
580580

581581
Operator getTarget() {
582582
exists(ImplOrTraitItemNode i |
583-
result.isAssocFunction(i, this.resolveCallTarget(i, _, _, _), false) // mutual recursion
583+
result.isFunction(this.resolveCallTarget(i, _, _, _), false) // mutual recursion
584584
)
585585
}
586586
}
@@ -1051,8 +1051,10 @@ private class FunctionDeclaration extends Function {
10511051
Type getParameterType(ImplOrTraitItemNodeOption i, int pos, TypePath path) {
10521052
i = parent and
10531053
exists(FunctionPosition fpos | pos = fpos.asPosition() |
1054+
// exists(i.asSome()) and
10541055
result = getAssocFunctionTypeAt(this, i.asSome(), fpos, path)
10551056
or
1057+
// result = fpos.getTypeMention(this).getTypeAt(path)
10561058
i.isNone() and
10571059
result = this.getParam(pos).getTypeRepr().(TypeMention).getTypeAt(path)
10581060
)
@@ -1083,6 +1085,19 @@ private class FunctionDeclaration extends Function {
10831085
else result = this.resolveRetType(i, path)
10841086
}
10851087

1088+
Type getReturnType2(TypePath path) {
1089+
if this.isAsync()
1090+
then
1091+
path.isEmpty() and
1092+
result = getFutureTraitType()
1093+
or
1094+
exists(TypePath suffix |
1095+
result = getReturnTypeMention(this).getTypeAt(suffix) and
1096+
path = TypePath::cons(getDynFutureOutputTypeParameter(), suffix)
1097+
)
1098+
else result = getReturnTypeMention(this).getTypeAt(path)
1099+
}
1100+
10861101
string toStringExt(ImplOrTraitItemNode i) {
10871102
i = parent.asSome() and
10881103
if this = i.getAnAssocItem()
@@ -2830,8 +2845,7 @@ private module AssocFunctionResolution {
28302845
*/
28312846
private module FunctionCallMatchingInput {
28322847
private newtype TDeclaration =
2833-
TFunctionDeclaration(ImplOrTraitItemNodeOption i, FunctionDeclaration f, boolean forIndexExpr) {
2834-
f.isFor(i) and
2848+
TFunctionDeclaration(FunctionDeclaration f, boolean forIndexExpr) {
28352849
(
28362850
forIndexExpr = false
28372851
or
@@ -2848,35 +2862,40 @@ private module FunctionCallMatchingInput {
28482862
}
28492863

28502864
final class Declaration extends TFunctionDeclaration {
2851-
ImplOrTraitItemNodeOption i;
28522865
FunctionDeclaration f;
28532866
boolean forIndexExpr;
28542867

2855-
Declaration() { this = TFunctionDeclaration(i, f, forIndexExpr) }
2868+
Declaration() { this = TFunctionDeclaration(f, forIndexExpr) }
28562869

28572870
FunctionDeclaration getFunction() { result = f }
28582871

2859-
predicate isFunction(ImplOrTraitItemNodeOption i_, Function f_) {
2860-
i_ = i and
2861-
f_ = f
2862-
}
2863-
2864-
predicate isAssocFunction(ImplOrTraitItemNode i_, Function f_, boolean forIndexExpr_) {
2865-
i_ = i.asSome() and
2872+
// predicate isFunction(ImplOrTraitItemNodeOption i_, Function f_) {
2873+
// i_ = i and
2874+
// f_ = f
2875+
// }
2876+
predicate isFunction(Function f_, boolean forIndexExpr_) {
28662877
f_ = f and
28672878
forIndexExpr_ = forIndexExpr
28682879
}
28692880

28702881
TypeParameter getTypeParameter(TypeParameterPosition ppos) {
2871-
result = f.getTypeParameter(i, ppos)
2882+
exists(ImplOrTraitItemNodeOption i | result = f.getTypeParameter(i, ppos) |
2883+
i.isNone()
2884+
or
2885+
i.asSome().getAnAssocItem() = f
2886+
)
28722887
}
28732888

2874-
Type getParameterType(int j, TypePath path) { result = f.getParameterType(i, j, path) }
2889+
Type getParameterType(int j, TypePath path) {
2890+
exists(FunctionPosition fpos | j = fpos.asPosition() |
2891+
result = fpos.getTypeMention(f).getTypeAt(path)
2892+
)
2893+
}
28752894

28762895
Type getReturnType(TypePath path) {
28772896
// index expression `x[i]` desugars to `*x.index(i)`, so we must account for
28782897
// the implicit deref
2879-
exists(TypePath path0 | result = f.getReturnType(i, path0) |
2898+
exists(TypePath path0 | result = f.getReturnType2(path0) |
28802899
forIndexExpr = true and
28812900
path0.isCons(getRefTypeParameter(_), path)
28822901
or
@@ -2885,11 +2904,7 @@ private module FunctionCallMatchingInput {
28852904
)
28862905
}
28872906

2888-
string toString() {
2889-
i.isNone() and result = f.toString()
2890-
or
2891-
result = f.toStringExt(i.asSome())
2892-
}
2907+
string toString() { result = f.toString() }
28932908

28942909
Location getLocation() { result = f.getLocation() }
28952910
}
@@ -3010,7 +3025,7 @@ private module FunctionCallMatchingInput {
30103025

30113026
override Declaration getTarget(string derefChainBorrow) {
30123027
exists(ImplOrTraitItemNode i, boolean forIndexExpr |
3013-
result.isAssocFunction(i, this.getTarget(i, derefChainBorrow), forIndexExpr) and
3028+
result.isFunction(this.getTarget(i, derefChainBorrow), forIndexExpr) and
30143029
if this instanceof IndexExpr then forIndexExpr = true else forIndexExpr = false
30153030
)
30163031
}
@@ -3066,9 +3081,7 @@ private module FunctionCallMatchingInput {
30663081

30673082
pragma[nomagic]
30683083
private Declaration getTarget() {
3069-
result =
3070-
TFunctionDeclaration(ImplOrTraitItemNodeOption::none_(),
3071-
super.resolveCallTargetViaPathResolution(), false)
3084+
result = TFunctionDeclaration(super.resolveCallTargetViaPathResolution(), false)
30723085
}
30733086

30743087
override Declaration getTarget(string derefChainBorrow) {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11722,9 +11722,11 @@ inferType
1172211722
| main.rs:2746:17:2746:18 | &1 | TRef | {EXTERNAL LOCATION} | i32 |
1172311723
| main.rs:2746:18:2746:18 | 1 | | {EXTERNAL LOCATION} | i32 |
1172411724
| main.rs:2747:13:2747:13 | z | | {EXTERNAL LOCATION} | & |
11725+
| main.rs:2747:13:2747:13 | z | TRef | {EXTERNAL LOCATION} | i32 |
1172511726
| main.rs:2747:13:2747:13 | z | TRef | {EXTERNAL LOCATION} | usize |
1172611727
| main.rs:2747:17:2747:17 | x | | {EXTERNAL LOCATION} | usize |
1172711728
| main.rs:2747:17:2747:22 | x.g(...) | | {EXTERNAL LOCATION} | & |
11729+
| main.rs:2747:17:2747:22 | x.g(...) | TRef | {EXTERNAL LOCATION} | i32 |
1172811730
| main.rs:2747:17:2747:22 | x.g(...) | TRef | {EXTERNAL LOCATION} | usize |
1172911731
| main.rs:2747:21:2747:21 | y | | {EXTERNAL LOCATION} | & |
1173011732
| main.rs:2747:21:2747:21 | y | TRef | {EXTERNAL LOCATION} | i32 |
@@ -11733,8 +11735,10 @@ inferType
1173311735
| main.rs:2750:13:2750:13 | y | | {EXTERNAL LOCATION} | usize |
1173411736
| main.rs:2750:24:2750:24 | 1 | | {EXTERNAL LOCATION} | i32 |
1173511737
| main.rs:2751:13:2751:13 | z | | {EXTERNAL LOCATION} | i32 |
11738+
| main.rs:2751:13:2751:13 | z | | {EXTERNAL LOCATION} | usize |
1173611739
| main.rs:2751:17:2751:17 | x | | {EXTERNAL LOCATION} | i32 |
1173711740
| main.rs:2751:17:2751:24 | x.max(...) | | {EXTERNAL LOCATION} | i32 |
11741+
| main.rs:2751:17:2751:24 | x.max(...) | | {EXTERNAL LOCATION} | usize |
1173811742
| main.rs:2751:23:2751:23 | y | | {EXTERNAL LOCATION} | usize |
1173911743
| main.rs:2766:22:2766:26 | SelfParam | | {EXTERNAL LOCATION} | & |
1174011744
| main.rs:2766:22:2766:26 | SelfParam | TRef | main.rs:2765:5:2767:5 | Self [trait Container] |
@@ -11855,8 +11859,6 @@ inferType
1185511859
| overloading.rs:58:13:58:15 | _b3 | | {EXTERNAL LOCATION} | bool |
1185611860
| overloading.rs:58:19:58:64 | ...::method(...) | | {EXTERNAL LOCATION} | bool |
1185711861
| overloading.rs:58:45:58:63 | &... | | {EXTERNAL LOCATION} | & |
11858-
| overloading.rs:58:45:58:63 | &... | TRef | overloading.rs:20:5:21:13 | S |
11859-
| overloading.rs:58:46:58:63 | ...::default(...) | | overloading.rs:20:5:21:13 | S |
1186011862
| overloading.rs:59:13:59:15 | _b4 | | {EXTERNAL LOCATION} | bool |
1186111863
| overloading.rs:59:19:59:48 | ...::method2(...) | | {EXTERNAL LOCATION} | bool |
1186211864
| overloading.rs:59:46:59:47 | &s | | {EXTERNAL LOCATION} | & |
@@ -11880,8 +11882,6 @@ inferType
1188011882
| overloading.rs:64:13:64:15 | _n3 | | {EXTERNAL LOCATION} | i64 |
1188111883
| overloading.rs:64:19:64:65 | ...::method(...) | | {EXTERNAL LOCATION} | i64 |
1188211884
| overloading.rs:64:46:64:64 | &... | | {EXTERNAL LOCATION} | & |
11883-
| overloading.rs:64:46:64:64 | &... | TRef | overloading.rs:20:5:21:13 | S |
11884-
| overloading.rs:64:47:64:64 | ...::default(...) | | overloading.rs:20:5:21:13 | S |
1188511885
| overloading.rs:65:13:65:15 | _n4 | | {EXTERNAL LOCATION} | i64 |
1188611886
| overloading.rs:65:19:65:49 | ...::method2(...) | | {EXTERNAL LOCATION} | i64 |
1188711887
| overloading.rs:65:47:65:48 | &s | | {EXTERNAL LOCATION} | & |
@@ -14624,3 +14624,5 @@ inferType
1462414624
| regressions.rs:179:24:179:27 | S(...) | T | {EXTERNAL LOCATION} | i32 |
1462514625
| regressions.rs:179:26:179:26 | 1 | | {EXTERNAL LOCATION} | i32 |
1462614626
testFailures
14627+
| overloading.rs:58:67:58:125 | //... | Missing result: target=default |
14628+
| overloading.rs:64:68:64:126 | //... | Missing result: target=default |

0 commit comments

Comments
 (0)