Skip to content

Commit 95b0c79

Browse files
committed
Rust: Context typing for constructors
1 parent 0670ec7 commit 95b0c79

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

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

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ private module ContextTyping {
955955
*/
956956
bindingset[this, i, target]
957957
predicate isContextTypedAt(
958-
ImplOrTraitItemNode i, Function target, TypePath path, FunctionPosition pos
958+
ImplOrTraitItemNode i, Function target, FunctionPosition pos, TypePath path
959959
) {
960960
exists(TypeParameter tp |
961961
assocFunctionReturnContextTypedAt(i, target, pos, path, tp) and
@@ -2090,6 +2090,17 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
20902090
}
20912091

20922092
Declaration getTarget(string derefChainBorrow) { result = this.getTarget(_, derefChainBorrow) }
2093+
2094+
/**
2095+
* Holds if the return type of this call at `path` may have to be inferred
2096+
* from the context.
2097+
*/
2098+
pragma[nomagic]
2099+
predicate isContextTypedAt(string derefChainBorrow, FunctionPosition pos, TypePath path) {
2100+
exists(ImplOrTraitItemNode i |
2101+
this.isContextTypedAt(i, this.getTarget(i, derefChainBorrow), pos, path)
2102+
)
2103+
}
20932104
}
20942105
}
20952106

@@ -2105,10 +2116,8 @@ private Type inferMethodCallType0(
21052116
(
21062117
result = MethodCallMatching::inferAccessType(a, derefChainBorrow, apos, path0)
21072118
or
2108-
exists(ImplOrTraitItemNode i |
2109-
a.isContextTypedAt(i, a.getTarget(i, derefChainBorrow), path0, apos) and
2110-
result = TContextType()
2111-
)
2119+
a.isContextTypedAt(derefChainBorrow, apos, path0) and
2120+
result = TContextType()
21122121
)
21132122
|
21142123
if
@@ -2585,6 +2594,35 @@ private module NonMethodCallMatchingInput implements MatchingInputSig {
25852594
Declaration getTarget() {
25862595
result = this.resolveCallTarget() // potential mutual recursion; resolving some associated function calls requires resolving types
25872596
}
2597+
2598+
/**
2599+
* Holds if the return type of this call at `path` may have to be inferred
2600+
* from the context.
2601+
*/
2602+
pragma[nomagic]
2603+
predicate isContextTypedAt(FunctionPosition pos, TypePath path) {
2604+
exists(ImplOrTraitItemNode i |
2605+
this.isContextTypedAt(i,
2606+
[
2607+
this.resolveCallTargetViaPathResolution().(NonMethodFunction),
2608+
this.resolveCallTargetViaTypeInference(i),
2609+
this.resolveTraitFunctionViaPathResolution(i)
2610+
], pos, path)
2611+
)
2612+
or
2613+
// Tuple declarations, such as `None`, may also be context typed
2614+
exists(TupleDeclaration td, TypeParameter tp |
2615+
td = this.resolveCallTargetViaPathResolution() and
2616+
pos.isReturn() and
2617+
tp = td.getReturnType(path) and
2618+
not tp = td.getParameterType(_, _) and
2619+
// check that no explicit type arguments have been supplied for `tp`
2620+
not exists(TypeArgumentPosition tapos |
2621+
exists(this.getTypeArgument(tapos, _)) and
2622+
TTypeParamTypeParameter(tapos.asTypeParam()) = tp
2623+
)
2624+
)
2625+
}
25882626
}
25892627
}
25902628

@@ -2597,15 +2635,8 @@ private Type inferNonMethodCallType0(
25972635
exists(NonMethodCallMatchingInput::Access a | n = a.getNodeAt(apos) |
25982636
result = NonMethodCallMatching::inferAccessType(a, apos, path)
25992637
or
2600-
exists(ImplOrTraitItemNode i |
2601-
a.isContextTypedAt(i,
2602-
[
2603-
a.resolveCallTargetViaPathResolution().(NonMethodFunction),
2604-
a.resolveCallTargetViaTypeInference(i),
2605-
a.resolveTraitFunctionViaPathResolution(i)
2606-
], path, apos) and
2607-
result = TContextType()
2608-
)
2638+
a.isContextTypedAt(apos, path) and
2639+
result = TContextType()
26092640
)
26102641
}
26112642

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3115,9 +3115,12 @@ inferType
31153115
| main.rs:1370:26:1370:27 | p3 | Snd | main.rs:1310:5:1311:14 | S3 |
31163116
| main.rs:1372:9:1372:55 | g(...) | | file://:0:0:0:0 | () |
31173117
| main.rs:1372:11:1372:54 | ...::PairSnd(...) | | main.rs:1285:5:1291:5 | PairOption |
3118+
| main.rs:1372:11:1372:54 | ...::PairSnd(...) | Fst | main.rs:1307:5:1308:14 | S2 |
31183119
| main.rs:1372:11:1372:54 | ...::PairSnd(...) | Snd | main.rs:1285:5:1291:5 | PairOption |
3120+
| main.rs:1372:11:1372:54 | ...::PairSnd(...) | Snd.Fst | main.rs:1307:5:1308:14 | S2 |
31193121
| main.rs:1372:11:1372:54 | ...::PairSnd(...) | Snd.Snd | main.rs:1310:5:1311:14 | S3 |
31203122
| main.rs:1372:31:1372:53 | ...::PairSnd(...) | | main.rs:1285:5:1291:5 | PairOption |
3123+
| main.rs:1372:31:1372:53 | ...::PairSnd(...) | Fst | main.rs:1307:5:1308:14 | S2 |
31213124
| main.rs:1372:31:1372:53 | ...::PairSnd(...) | Snd | main.rs:1310:5:1311:14 | S3 |
31223125
| main.rs:1372:51:1372:52 | S3 | | main.rs:1310:5:1311:14 | S3 |
31233126
| main.rs:1374:13:1374:13 | x | | {EXTERNAL LOCATION} | Result |
@@ -3823,6 +3826,7 @@ inferType
38233826
| main.rs:1700:21:1700:25 | input | T | main.rs:1699:20:1699:27 | T |
38243827
| main.rs:1700:21:1700:26 | TryExpr | | main.rs:1699:20:1699:27 | T |
38253828
| main.rs:1701:22:1701:38 | ...::Ok(...) | | {EXTERNAL LOCATION} | Result |
3829+
| main.rs:1701:22:1701:38 | ...::Ok(...) | E | main.rs:1667:5:1668:14 | S1 |
38263830
| main.rs:1701:22:1701:38 | ...::Ok(...) | T | main.rs:1699:20:1699:27 | T |
38273831
| main.rs:1701:22:1704:10 | ... .and_then(...) | | {EXTERNAL LOCATION} | Result |
38283832
| main.rs:1701:22:1704:10 | ... .and_then(...) | E | main.rs:1667:5:1668:14 | S1 |
@@ -3895,6 +3899,7 @@ inferType
38953899
| main.rs:1722:37:1722:63 | try_complex(...) | E | main.rs:1667:5:1668:14 | S1 |
38963900
| main.rs:1722:37:1722:63 | try_complex(...) | T | main.rs:1667:5:1668:14 | S1 |
38973901
| main.rs:1722:49:1722:62 | ...::Ok(...) | | {EXTERNAL LOCATION} | Result |
3902+
| main.rs:1722:49:1722:62 | ...::Ok(...) | E | main.rs:1667:5:1668:14 | S1 |
38983903
| main.rs:1722:49:1722:62 | ...::Ok(...) | T | main.rs:1667:5:1668:14 | S1 |
38993904
| main.rs:1722:60:1722:61 | S1 | | main.rs:1667:5:1668:14 | S1 |
39003905
| main.rs:1722:65:1724:9 | { ... } | | file://:0:0:0:0 | () |

0 commit comments

Comments
 (0)