Skip to content

Commit 37b1b73

Browse files
committed
Rust: Model async return types as dyn Future
1 parent bb9daa0 commit 37b1b73

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ private predicate typeEquality(AstNode n1, TypePath prefix1, AstNode n2, TypePat
520520
n2 = be.getStmtList().getTailExpr() and
521521
if be.isAsync()
522522
then
523-
prefix1 = TypePath::singleton(getFutureOutputTypeParameter()) and
523+
prefix1 = TypePath::singleton(getDynFutureOutputTypeParameter()) and
524524
prefix2.isEmpty()
525525
else (
526526
prefix1.isEmpty() and
@@ -940,7 +940,7 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
940940
or
941941
exists(TypePath suffix |
942942
result = this.resolveRetType(suffix) and
943-
path = TypePath::cons(getFutureOutputTypeParameter(), suffix)
943+
path = TypePath::cons(getDynFutureOutputTypeParameter(), suffix)
944944
)
945945
else result = this.resolveRetType(path)
946946
}
@@ -1426,16 +1426,22 @@ private Type inferLiteralType(LiteralExpr le, TypePath path, boolean certain) {
14261426
certain = true
14271427
}
14281428

1429+
// always exists because of the mention in `builtins/mentions.rs`
14291430
pragma[nomagic]
1430-
private TraitType getFutureTraitType() { result.getTrait() instanceof FutureTrait }
1431+
private DynTraitType getFutureTraitType() { result.getTrait() instanceof FutureTrait }
14311432

14321433
pragma[nomagic]
14331434
private AssociatedTypeTypeParameter getFutureOutputTypeParameter() {
14341435
result.getTypeAlias() = any(FutureTrait ft).getOutputType()
14351436
}
14361437

14371438
pragma[nomagic]
1438-
private TraitType inferAsyncBlockExprRootType(AsyncBlockExpr abe) {
1439+
private DynTraitTypeParameter getDynFutureOutputTypeParameter() {
1440+
result = TDynTraitTypeParameter(any(FutureTrait ft).getOutputType())
1441+
}
1442+
1443+
pragma[nomagic]
1444+
private DynTraitType inferAsyncBlockExprRootType(AsyncBlockExpr abe) {
14391445
// `typeEquality` handles the non-root case
14401446
exists(abe) and
14411447
result = getFutureTraitType()
@@ -1773,7 +1779,7 @@ private Type inferClosureExprType(AstNode n, TypePath path) {
17731779
exists(ClosureExpr ce |
17741780
n = ce and
17751781
path.isEmpty() and
1776-
result = TDynTraitType(any(FnOnceTrait t))
1782+
result = TDynTraitType(any(FnOnceTrait t)) // always exists because of the mention in `builtins/mentions.rs`
17771783
or
17781784
n = ce and
17791785
path = TypePath::singleton(TDynTraitTypeParameter(any(FnOnceTrait t).getTypeParam())) and

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3728,9 +3728,9 @@ inferType
37283728
| main.rs:1915:25:1917:5 | { ... } | | main.rs:1909:5:1909:14 | S1 |
37293729
| main.rs:1916:9:1916:10 | S1 | | main.rs:1909:5:1909:14 | S1 |
37303730
| main.rs:1919:41:1921:5 | { ... } | | main.rs:1919:16:1919:39 | ImplTraitTypeRepr |
3731-
| main.rs:1920:9:1920:20 | { ... } | | {EXTERNAL LOCATION} | trait Future |
3731+
| main.rs:1920:9:1920:20 | { ... } | | {EXTERNAL LOCATION} | dyn Future |
37323732
| main.rs:1920:9:1920:20 | { ... } | | main.rs:1919:16:1919:39 | ImplTraitTypeRepr |
3733-
| main.rs:1920:9:1920:20 | { ... } | Output | main.rs:1909:5:1909:14 | S1 |
3733+
| main.rs:1920:9:1920:20 | { ... } | dyn(Output) | main.rs:1909:5:1909:14 | S1 |
37343734
| main.rs:1920:17:1920:18 | S1 | | main.rs:1909:5:1909:14 | S1 |
37353735
| main.rs:1929:13:1929:42 | SelfParam | | {EXTERNAL LOCATION} | Pin |
37363736
| main.rs:1929:13:1929:42 | SelfParam | Ptr | file://:0:0:0:0 | & |
@@ -3745,22 +3745,22 @@ inferType
37453745
| main.rs:1936:41:1938:5 | { ... } | | main.rs:1936:16:1936:39 | ImplTraitTypeRepr |
37463746
| main.rs:1937:9:1937:10 | S2 | | main.rs:1923:5:1923:14 | S2 |
37473747
| main.rs:1937:9:1937:10 | S2 | | main.rs:1936:16:1936:39 | ImplTraitTypeRepr |
3748-
| main.rs:1941:9:1941:12 | f1(...) | | {EXTERNAL LOCATION} | trait Future |
3749-
| main.rs:1941:9:1941:12 | f1(...) | Output | main.rs:1909:5:1909:14 | S1 |
3748+
| main.rs:1941:9:1941:12 | f1(...) | | {EXTERNAL LOCATION} | dyn Future |
3749+
| main.rs:1941:9:1941:12 | f1(...) | dyn(Output) | main.rs:1909:5:1909:14 | S1 |
37503750
| main.rs:1941:9:1941:18 | await ... | | main.rs:1909:5:1909:14 | S1 |
37513751
| main.rs:1942:9:1942:12 | f2(...) | | main.rs:1919:16:1919:39 | ImplTraitTypeRepr |
37523752
| main.rs:1942:9:1942:18 | await ... | | main.rs:1909:5:1909:14 | S1 |
37533753
| main.rs:1943:9:1943:12 | f3(...) | | main.rs:1936:16:1936:39 | ImplTraitTypeRepr |
37543754
| main.rs:1943:9:1943:18 | await ... | | main.rs:1909:5:1909:14 | S1 |
37553755
| main.rs:1944:9:1944:10 | S2 | | main.rs:1923:5:1923:14 | S2 |
37563756
| main.rs:1944:9:1944:16 | await S2 | | main.rs:1909:5:1909:14 | S1 |
3757-
| main.rs:1945:13:1945:13 | b | | {EXTERNAL LOCATION} | trait Future |
3758-
| main.rs:1945:13:1945:13 | b | Output | main.rs:1909:5:1909:14 | S1 |
3759-
| main.rs:1945:17:1945:28 | { ... } | | {EXTERNAL LOCATION} | trait Future |
3760-
| main.rs:1945:17:1945:28 | { ... } | Output | main.rs:1909:5:1909:14 | S1 |
3757+
| main.rs:1945:13:1945:13 | b | | {EXTERNAL LOCATION} | dyn Future |
3758+
| main.rs:1945:13:1945:13 | b | dyn(Output) | main.rs:1909:5:1909:14 | S1 |
3759+
| main.rs:1945:17:1945:28 | { ... } | | {EXTERNAL LOCATION} | dyn Future |
3760+
| main.rs:1945:17:1945:28 | { ... } | dyn(Output) | main.rs:1909:5:1909:14 | S1 |
37613761
| main.rs:1945:25:1945:26 | S1 | | main.rs:1909:5:1909:14 | S1 |
3762-
| main.rs:1946:9:1946:9 | b | | {EXTERNAL LOCATION} | trait Future |
3763-
| main.rs:1946:9:1946:9 | b | Output | main.rs:1909:5:1909:14 | S1 |
3762+
| main.rs:1946:9:1946:9 | b | | {EXTERNAL LOCATION} | dyn Future |
3763+
| main.rs:1946:9:1946:9 | b | dyn(Output) | main.rs:1909:5:1909:14 | S1 |
37643764
| main.rs:1946:9:1946:15 | await b | | main.rs:1909:5:1909:14 | S1 |
37653765
| main.rs:1957:15:1957:19 | SelfParam | | file://:0:0:0:0 | & |
37663766
| main.rs:1957:15:1957:19 | SelfParam | &T | main.rs:1956:5:1958:5 | Self [trait Trait1] |
@@ -4954,7 +4954,7 @@ inferType
49544954
| main.rs:2565:5:2565:60 | ...::g(...) | | main.rs:72:5:72:21 | Foo |
49554955
| main.rs:2565:20:2565:38 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
49564956
| main.rs:2565:41:2565:59 | ...::Foo {...} | | main.rs:72:5:72:21 | Foo |
4957-
| main.rs:2581:5:2581:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
4957+
| main.rs:2581:5:2581:15 | ...::f(...) | | {EXTERNAL LOCATION} | dyn Future |
49584958
| pattern_matching.rs:13:26:133:1 | { ... } | | {EXTERNAL LOCATION} | Option |
49594959
| pattern_matching.rs:13:26:133:1 | { ... } | T | file://:0:0:0:0 | () |
49604960
| pattern_matching.rs:14:9:14:13 | value | | {EXTERNAL LOCATION} | Option |

rust/tools/builtins/mentions.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Type mentions required by type inference
2+
3+
use std::future::Future;
4+
fn mention_dyn_future<T>(f: &dyn Future<Output = T>) {}
5+
6+
fn mention_dyn_fn_once<F>(f: &dyn FnOnce() -> F) {}

0 commit comments

Comments
 (0)