Skip to content

Commit f0cac32

Browse files
authored
Merge pull request #20236 from hvitved/rust/type-inference-async-dyn-future
Rust: Model `async` return types as `dyn Future`
2 parents 8b32679 + a9b58b8 commit f0cac32

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ private predicate typeEquality(AstNode n1, TypePath prefix1, AstNode n2, TypePat
629629
n2 = be.getStmtList().getTailExpr() and
630630
if be.isAsync()
631631
then
632-
prefix1 = TypePath::singleton(getFutureOutputTypeParameter()) and
632+
prefix1 = TypePath::singleton(getDynFutureOutputTypeParameter()) and
633633
prefix2.isEmpty()
634634
else (
635635
prefix1.isEmpty() and
@@ -2053,7 +2053,7 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
20532053
or
20542054
exists(TypePath suffix |
20552055
result = this.resolveRetType(suffix) and
2056-
path = TypePath::cons(getFutureOutputTypeParameter(), suffix)
2056+
path = TypePath::cons(getDynFutureOutputTypeParameter(), suffix)
20572057
)
20582058
else result = this.resolveRetType(path)
20592059
}
@@ -3024,13 +3024,18 @@ private Type inferLiteralType(LiteralExpr le, TypePath path, boolean certain) {
30243024
}
30253025

30263026
pragma[nomagic]
3027-
private TraitType getFutureTraitType() { result.getTrait() instanceof FutureTrait }
3027+
private DynTraitType getFutureTraitType() { result.getTrait() instanceof FutureTrait }
30283028

30293029
pragma[nomagic]
30303030
private AssociatedTypeTypeParameter getFutureOutputTypeParameter() {
30313031
result.getTypeAlias() = any(FutureTrait ft).getOutputType()
30323032
}
30333033

3034+
pragma[nomagic]
3035+
private DynTraitTypeParameter getDynFutureOutputTypeParameter() {
3036+
result = TDynTraitTypeParameter(any(FutureTrait ft).getOutputType())
3037+
}
3038+
30343039
pragma[nomagic]
30353040
predicate isUnitBlockExpr(BlockExpr be) {
30363041
not be.getStmtList().hasTailExpr() and
@@ -3047,7 +3052,7 @@ private Type inferBlockExprType(BlockExpr be, TypePath path) {
30473052
result = getFutureTraitType()
30483053
or
30493054
isUnitBlockExpr(be) and
3050-
path = TypePath::singleton(getFutureOutputTypeParameter()) and
3055+
path = TypePath::singleton(getDynFutureOutputTypeParameter()) and
30513056
result instanceof UnitType
30523057
) else (
30533058
isUnitBlockExpr(be) and
@@ -3072,6 +3077,7 @@ final private class AwaitTarget extends Expr {
30723077
}
30733078

30743079
private module AwaitSatisfiesConstraintInput implements SatisfiesConstraintInputSig<AwaitTarget> {
3080+
pragma[nomagic]
30753081
predicate relevantConstraint(AwaitTarget term, Type constraint) {
30763082
exists(term) and
30773083
constraint.(TraitType).getTrait() instanceof FutureTrait
@@ -3390,7 +3396,7 @@ private Type inferClosureExprType(AstNode n, TypePath path) {
33903396
exists(ClosureExpr ce |
33913397
n = ce and
33923398
path.isEmpty() and
3393-
result = TDynTraitType(any(FnOnceTrait t))
3399+
result = TDynTraitType(any(FnOnceTrait t)) // always exists because of the mention in `builtins/mentions.rs`
33943400
or
33953401
n = ce and
33963402
path = TypePath::singleton(TDynTraitTypeParameter(any(FnOnceTrait t).getTypeParam())) and

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4908,12 +4908,12 @@ inferType
49084908
| main.rs:2227:25:2229:5 | { ... } | | main.rs:2221:5:2221:14 | S1 |
49094909
| main.rs:2228:9:2228:10 | S1 | | main.rs:2221:5:2221:14 | S1 |
49104910
| main.rs:2231:41:2233:5 | { ... } | | main.rs:2231:16:2231:39 | impl ... |
4911-
| main.rs:2232:9:2232:20 | { ... } | | {EXTERNAL LOCATION} | trait Future |
4912-
| main.rs:2232:9:2232:20 | { ... } | Output | main.rs:2221:5:2221:14 | S1 |
4911+
| main.rs:2232:9:2232:20 | { ... } | | {EXTERNAL LOCATION} | dyn Future |
4912+
| main.rs:2232:9:2232:20 | { ... } | dyn(Output) | main.rs:2221:5:2221:14 | S1 |
49134913
| main.rs:2232:17:2232:18 | S1 | | main.rs:2221:5:2221:14 | S1 |
49144914
| main.rs:2235:41:2237:5 | { ... } | | main.rs:2235:16:2235:39 | impl ... |
4915-
| main.rs:2236:9:2236:16 | { ... } | | {EXTERNAL LOCATION} | trait Future |
4916-
| main.rs:2236:9:2236:16 | { ... } | Output | {EXTERNAL LOCATION} | () |
4915+
| main.rs:2236:9:2236:16 | { ... } | | {EXTERNAL LOCATION} | dyn Future |
4916+
| main.rs:2236:9:2236:16 | { ... } | dyn(Output) | {EXTERNAL LOCATION} | () |
49174917
| main.rs:2245:13:2245:42 | SelfParam | | {EXTERNAL LOCATION} | Pin |
49184918
| main.rs:2245:13:2245:42 | SelfParam | Ptr | {EXTERNAL LOCATION} | & |
49194919
| main.rs:2245:13:2245:42 | SelfParam | Ptr.TRef | main.rs:2239:5:2239:14 | S2 |
@@ -4928,8 +4928,8 @@ inferType
49284928
| main.rs:2253:9:2253:10 | S2 | | main.rs:2239:5:2239:14 | S2 |
49294929
| main.rs:2253:9:2253:10 | S2 | | main.rs:2252:16:2252:39 | impl ... |
49304930
| main.rs:2256:22:2264:5 | { ... } | | {EXTERNAL LOCATION} | () |
4931-
| main.rs:2257:9:2257:12 | f1(...) | | {EXTERNAL LOCATION} | trait Future |
4932-
| main.rs:2257:9:2257:12 | f1(...) | Output | main.rs:2221:5:2221:14 | S1 |
4931+
| main.rs:2257:9:2257:12 | f1(...) | | {EXTERNAL LOCATION} | dyn Future |
4932+
| main.rs:2257:9:2257:12 | f1(...) | dyn(Output) | main.rs:2221:5:2221:14 | S1 |
49334933
| main.rs:2257:9:2257:18 | await ... | | main.rs:2221:5:2221:14 | S1 |
49344934
| main.rs:2257:9:2257:22 | ... .f() | | {EXTERNAL LOCATION} | () |
49354935
| main.rs:2258:9:2258:12 | f2(...) | | main.rs:2231:16:2231:39 | impl ... |
@@ -4943,13 +4943,13 @@ inferType
49434943
| main.rs:2261:9:2261:10 | S2 | | main.rs:2239:5:2239:14 | S2 |
49444944
| main.rs:2261:9:2261:16 | await S2 | | main.rs:2221:5:2221:14 | S1 |
49454945
| main.rs:2261:9:2261:20 | ... .f() | | {EXTERNAL LOCATION} | () |
4946-
| main.rs:2262:13:2262:13 | b | | {EXTERNAL LOCATION} | trait Future |
4947-
| main.rs:2262:13:2262:13 | b | Output | main.rs:2221:5:2221:14 | S1 |
4948-
| main.rs:2262:17:2262:28 | { ... } | | {EXTERNAL LOCATION} | trait Future |
4949-
| main.rs:2262:17:2262:28 | { ... } | Output | main.rs:2221:5:2221:14 | S1 |
4946+
| main.rs:2262:13:2262:13 | b | | {EXTERNAL LOCATION} | dyn Future |
4947+
| main.rs:2262:13:2262:13 | b | dyn(Output) | main.rs:2221:5:2221:14 | S1 |
4948+
| main.rs:2262:17:2262:28 | { ... } | | {EXTERNAL LOCATION} | dyn Future |
4949+
| main.rs:2262:17:2262:28 | { ... } | dyn(Output) | main.rs:2221:5:2221:14 | S1 |
49504950
| main.rs:2262:25:2262:26 | S1 | | main.rs:2221:5:2221:14 | S1 |
4951-
| main.rs:2263:9:2263:9 | b | | {EXTERNAL LOCATION} | trait Future |
4952-
| main.rs:2263:9:2263:9 | b | Output | main.rs:2221:5:2221:14 | S1 |
4951+
| main.rs:2263:9:2263:9 | b | | {EXTERNAL LOCATION} | dyn Future |
4952+
| main.rs:2263:9:2263:9 | b | dyn(Output) | main.rs:2221:5:2221:14 | S1 |
49534953
| main.rs:2263:9:2263:15 | await b | | main.rs:2221:5:2221:14 | S1 |
49544954
| main.rs:2263:9:2263:19 | ... .f() | | {EXTERNAL LOCATION} | () |
49554955
| main.rs:2274:15:2274:19 | SelfParam | | {EXTERNAL LOCATION} | & |
@@ -6550,8 +6550,8 @@ inferType
65506550
| main.rs:3061:5:3061:24 | ...::f(...) | | {EXTERNAL LOCATION} | () |
65516551
| main.rs:3062:5:3062:17 | ...::f(...) | | {EXTERNAL LOCATION} | () |
65526552
| main.rs:3063:5:3063:18 | ...::f(...) | | {EXTERNAL LOCATION} | () |
6553-
| main.rs:3064:5:3064:15 | ...::f(...) | | {EXTERNAL LOCATION} | trait Future |
6554-
| main.rs:3064:5:3064:15 | ...::f(...) | Output | {EXTERNAL LOCATION} | () |
6553+
| main.rs:3064:5:3064:15 | ...::f(...) | | {EXTERNAL LOCATION} | dyn Future |
6554+
| main.rs:3064:5:3064:15 | ...::f(...) | dyn(Output) | {EXTERNAL LOCATION} | () |
65556555
| main.rs:3065:5:3065:19 | ...::f(...) | | {EXTERNAL LOCATION} | () |
65566556
| main.rs:3066:5:3066:17 | ...::f(...) | | {EXTERNAL LOCATION} | () |
65576557
| main.rs:3067:5:3067:14 | ...::f(...) | | {EXTERNAL LOCATION} | () |

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)