Skip to content

Commit 57bace8

Browse files
authored
Merge pull request #22972 from hvitved/type-inference/type-param-escape-consistency-check
Type inference: Consistency check for escaping type parameters
2 parents 1e066e5 + 2035f20 commit 57bace8

13 files changed

Lines changed: 157 additions & 10 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: TypeInferenceConsistency.ql
2+
postprocess: utils/test/ExternalLocationPostProcessing.ql

‎rust/ql/lib/codeql/rust/internal/typeinference/Type.qll‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ abstract class TypeParameter extends Type {
401401
override TypeParameter getPositionalTypeParameter(int i) { none() }
402402

403403
abstract ItemNode getDeclaringItem();
404+
405+
AstNode getScope() { result = this.getDeclaringItem() }
404406
}
405407

406408
private class RawTypeParameter = @type_param or @trait or @type_alias or @impl_trait_type_repr;
@@ -583,6 +585,8 @@ class ImplTraitTypeParameter extends TypeParameter, TImplTraitTypeParameter {
583585

584586
override ItemNode getDeclaringItem() { none() }
585587

588+
override AstNode getScope() { result = implTrait.getFunction() }
589+
586590
override string toString() { result = "impl(" + typeParam.toString() + ")" }
587591

588592
override Location getLocation() { result = typeParam.getLocation() }
@@ -633,6 +637,8 @@ class ImplTraitTypeTypeParameter extends ImplTraitType, TypeParameter {
633637

634638
override ItemNode getDeclaringItem() { none() }
635639

640+
override AstNode getScope() { result = function }
641+
636642
override Function getFunction() { result = function }
637643

638644
override TypeParameter getPositionalTypeParameter(int i) { none() }

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,7 +2272,11 @@ private module Input3 implements InputSig3 {
22722272
BoolType() { this.getTypeItem() instanceof Builtins::Bool }
22732273
}
22742274

2275-
class AstNode = Rust::AstNode;
2275+
class AstNode extends Rust::AstNode {
2276+
AstNode getParent() { result = this.getParentNode() }
2277+
}
2278+
2279+
AstNode getTypeParameterScope(TypeParameter tp) { result = tp.getScope() }
22762280

22772281
final class Expr = ExprImpl;
22782282

@@ -2286,13 +2290,13 @@ private module Input3 implements InputSig3 {
22862290
TypeMention getType() { result = this.getTypeRepr() }
22872291
}
22882292

2289-
class Switch extends Rust::MatchExpr {
2293+
class Switch extends Expr, Rust::MatchExpr {
22902294
Expr getExpr() { result = this.getScrutinee() }
22912295

22922296
Case getCase(int index) { result = this.getArm(index) }
22932297
}
22942298

2295-
class Case extends Rust::MatchArm {
2299+
class Case extends AstNode, Rust::MatchArm {
22962300
AstNode getAPattern() { result = this.getPat() }
22972301

22982302
AstNode getBody() { result = this.getExpr() }
@@ -2449,8 +2453,8 @@ private module Input3 implements InputSig3 {
24492453
// mutual recursion; resolving fields requires resolving types and vice versa
24502454
result =
24512455
[
2452-
resolveStructFieldExpr(this, _).(AstNode),
2453-
resolveTupleFieldExpr(this, _)
2456+
resolveStructFieldExpr(this, _).(Field),
2457+
resolveTupleFieldExpr(this, _).(Field)
24542458
]
24552459
}
24562460
}
@@ -2480,7 +2484,7 @@ private module Input3 implements InputSig3 {
24802484
)
24812485
}
24822486

2483-
class Return extends ReturnExpr {
2487+
class Return extends Expr, ReturnExpr {
24842488
Expr getExpr() { result = super.getExpr() }
24852489
}
24862490

@@ -2583,15 +2587,15 @@ private module Input3 implements InputSig3 {
25832587
override TypeItem getTypeItem() { result = this }
25842588

25852589
override Parameter getParameter(int i) {
2586-
result = [super.getTupleField(i).(AstNode), super.getNthStructField(i)]
2590+
result = [super.getTupleField(i).(Parameter), super.getNthStructField(i).(Parameter)]
25872591
}
25882592
}
25892593

25902594
private class VariantConstructor extends ConstructorImpl instanceof Variant {
25912595
override TypeItem getTypeItem() { result = super.getEnum() }
25922596

25932597
override Parameter getParameter(int i) {
2594-
result = [super.getTupleField(i).(AstNode), super.getNthStructField(i)]
2598+
result = [super.getTupleField(i).(Parameter), super.getNthStructField(i).(Parameter)]
25952599
}
25962600
}
25972601

@@ -3155,7 +3159,7 @@ module Consistency = M3::Consistency;
31553159

31563160
module CachedStage = M3::CachedStage;
31573161

3158-
private predicate typeTestAstNodeRepr(AstNode n, string repr) {
3162+
private predicate typeTestAstNodeRepr(Input3::AstNode n, string repr) {
31593163
repr = [n.toString(), n.(IdentPat).getName().getText()]
31603164
}
31613165

‎rust/ql/lib/codeql/rust/internal/typeinference/TypeInferenceConsistency.qll‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ int getTypeInferenceInconsistencyCounts(string type) {
4747
or
4848
type = "Non-unique certain type information" and
4949
result = count(AstNode n, TypePath path | nonUniqueCertainType(n, path) | n)
50+
or
51+
type = "Escaping type parameter" and
52+
result = count(AstNode n, TypePath path | typeParameterEscapesScope(n, path, _))
5053
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
typeParameterEscapesScope
2+
| test.rs:83:17:83:20 | data | B | {EXTERNAL LOCATION} | T::Data[Body] |
3+
| test.rs:83:17:83:20 | data | B | {EXTERNAL LOCATION} | T::Data[Body] |
4+
| test.rs:83:24:83:43 | await ... | E | {EXTERNAL LOCATION} | T::Error[Body] |
5+
| test.rs:83:24:83:43 | await ... | T.B | {EXTERNAL LOCATION} | T::Data[Body] |
6+
| test.rs:83:24:83:44 | TryExpr | B | {EXTERNAL LOCATION} | T::Data[Body] |
7+
| test.rs:84:18:84:22 | &data | TRef.B | {EXTERNAL LOCATION} | T::Data[Body] |
8+
| test.rs:84:19:84:22 | data | B | {EXTERNAL LOCATION} | T::Data[Body] |
9+
| test.rs:86:25:86:28 | data | B | {EXTERNAL LOCATION} | T::Data[Body] |
10+
| test.rs:92:23:92:33 | Some(...) | T.E | {EXTERNAL LOCATION} | T::Error[Body] |
11+
| test.rs:92:23:92:33 | Some(...) | T.T.T | {EXTERNAL LOCATION} | T::Data[Body] |
12+
| test.rs:92:28:92:32 | frame | E | {EXTERNAL LOCATION} | T::Error[Body] |
13+
| test.rs:92:28:92:32 | frame | E | {EXTERNAL LOCATION} | T::Error[Body] |
14+
| test.rs:92:28:92:32 | frame | T.T | {EXTERNAL LOCATION} | T::Data[Body] |
15+
| test.rs:92:28:92:32 | frame | T.T | {EXTERNAL LOCATION} | T::Data[Body] |
16+
| test.rs:92:37:92:58 | await ... | T.E | {EXTERNAL LOCATION} | T::Error[Body] |
17+
| test.rs:92:37:92:58 | await ... | T.T.T | {EXTERNAL LOCATION} | T::Data[Body] |
18+
| test.rs:93:24:93:33 | Some(...) | T.TRef | {EXTERNAL LOCATION} | T::Data[Body] |
19+
| test.rs:93:29:93:32 | data | TRef | {EXTERNAL LOCATION} | T::Data[Body] |
20+
| test.rs:93:29:93:32 | data | TRef | {EXTERNAL LOCATION} | T::Data[Body] |
21+
| test.rs:93:37:93:41 | frame | E | {EXTERNAL LOCATION} | T::Error[Body] |
22+
| test.rs:93:37:93:41 | frame | T.T | {EXTERNAL LOCATION} | T::Data[Body] |
23+
| test.rs:93:37:93:42 | TryExpr | T | {EXTERNAL LOCATION} | T::Data[Body] |
24+
| test.rs:93:37:93:53 | ... .data_ref() | T.TRef | {EXTERNAL LOCATION} | T::Data[Body] |
25+
| test.rs:94:49:94:52 | data | TRef | {EXTERNAL LOCATION} | T::Data[Body] |
26+
| test.rs:95:26:95:29 | data | TRef | {EXTERNAL LOCATION} | T::Data[Body] |
27+
| test.rs:96:26:96:29 | data | TRef | {EXTERNAL LOCATION} | T::Data[Body] |
28+
| test.rs:97:33:97:36 | data | TRef | {EXTERNAL LOCATION} | T::Data[Body] |
29+
| test.rs:557:18:557:42 | await ... | T | {EXTERNAL LOCATION} | St::Item[Stream] |
30+
| test.rs:557:18:557:51 | ... .unwrap() | | {EXTERNAL LOCATION} | St::Item[Stream] |
31+
| test.rs:558:23:558:32 | Some(...) | T | {EXTERNAL LOCATION} | St::Item[Stream] |
32+
| test.rs:558:28:558:31 | line | | {EXTERNAL LOCATION} | St::Item[Stream] |
33+
| test.rs:558:28:558:31 | line | | {EXTERNAL LOCATION} | St::Item[Stream] |
34+
| test.rs:558:36:558:60 | await ... | T | {EXTERNAL LOCATION} | St::Item[Stream] |
35+
| test.rs:559:22:559:25 | line | | {EXTERNAL LOCATION} | St::Item[Stream] |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
typeParameterEscapesScope
2+
| main.rs:405:26:410:9 | { ... } | | main.rs:380:7:380:7 | T |
3+
| main.rs:409:13:409:19 | ...::c | | main.rs:380:7:380:7 | T |
4+
| main.rs:466:9:470:12 | ...::c | | main.rs:380:7:380:7 | T |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
typeParameterEscapesScope
2+
| main.rs:2240:9:2240:31 | ... .my_add(...) | T.T | main.rs:2126:10:2126:17 | T::Output[MyAdd] |
3+
| main.rs:2240:9:2240:31 | ... .my_add(...) | T.T | main.rs:2135:10:2135:17 | T::Output[MyAdd] |
4+
| main.rs:2240:9:2240:31 | ... .my_add(...) | T.T | main.rs:2144:14:2144:14 | T::Output[MyAdd] |
5+
| main.rs:2296:13:2296:13 | s | | {EXTERNAL LOCATION} | I::Item[Iterator] |
6+
| main.rs:2296:13:2296:13 | s | | {EXTERNAL LOCATION} | I::Item[Iterator] |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
typeParameterEscapesScope
2+
| sqlx.rs:43:61:43:80 | await ... | T.DB | {EXTERNAL LOCATION} | DB |
3+
| sqlx.rs:43:61:43:81 | TryExpr | DB | {EXTERNAL LOCATION} | DB |
4+
| sqlx.rs:169:9:169:16 | mut conn | DB | {EXTERNAL LOCATION} | DB |
5+
| sqlx.rs:169:13:169:16 | conn | DB | {EXTERNAL LOCATION} | DB |
6+
| sqlx.rs:169:20:169:39 | await ... | T.DB | {EXTERNAL LOCATION} | DB |
7+
| sqlx.rs:169:20:169:40 | TryExpr | DB | {EXTERNAL LOCATION} | DB |
8+
| sqlx.rs:179:13:179:16 | conn | DB | {EXTERNAL LOCATION} | DB |
9+
| sqlx.rs:181:17:181:20 | conn | DB | {EXTERNAL LOCATION} | DB |
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
typeParameterEscapesScope
2+
| test_storage.rs:64:9:64:17 | mut conn1 | DB | {EXTERNAL LOCATION} | DB |
3+
| test_storage.rs:64:13:64:17 | conn1 | DB | {EXTERNAL LOCATION} | DB |
4+
| test_storage.rs:64:21:64:41 | await ... | T.DB | {EXTERNAL LOCATION} | DB |
5+
| test_storage.rs:64:21:64:42 | TryExpr | DB | {EXTERNAL LOCATION} | DB |
6+
| test_storage.rs:85:13:85:17 | conn1 | DB | {EXTERNAL LOCATION} | DB |
7+
| test_storage.rs:86:13:86:17 | conn1 | DB | {EXTERNAL LOCATION} | DB |
8+
| test_storage.rs:87:13:87:17 | conn1 | DB | {EXTERNAL LOCATION} | DB |
9+
| test_storage.rs:88:13:88:17 | conn1 | DB | {EXTERNAL LOCATION} | DB |
10+
| test_storage.rs:89:13:89:17 | conn1 | DB | {EXTERNAL LOCATION} | DB |
11+
| test_storage.rs:90:13:90:17 | conn1 | DB | {EXTERNAL LOCATION} | DB |
12+
| test_storage.rs:91:13:91:17 | conn1 | DB | {EXTERNAL LOCATION} | DB |
13+
| test_storage.rs:92:13:92:17 | conn1 | DB | {EXTERNAL LOCATION} | DB |
14+
| test_storage.rs:93:13:93:17 | conn1 | DB | {EXTERNAL LOCATION} | DB |
15+
| test_storage.rs:94:13:94:17 | conn1 | DB | {EXTERNAL LOCATION} | DB |
16+
| test_storage.rs:95:13:95:17 | conn1 | DB | {EXTERNAL LOCATION} | DB |
17+
| test_storage.rs:96:13:96:17 | conn1 | DB | {EXTERNAL LOCATION} | DB |
18+
| test_storage.rs:131:9:131:17 | mut conn3 | DB | {EXTERNAL LOCATION} | DB |
19+
| test_storage.rs:131:13:131:17 | conn3 | DB | {EXTERNAL LOCATION} | DB |
20+
| test_storage.rs:131:21:131:41 | await ... | T.DB | {EXTERNAL LOCATION} | DB |
21+
| test_storage.rs:131:21:131:42 | TryExpr | DB | {EXTERNAL LOCATION} | DB |
22+
| test_storage.rs:134:13:134:17 | conn3 | DB | {EXTERNAL LOCATION} | DB |
23+
| test_storage.rs:135:13:135:17 | conn3 | DB | {EXTERNAL LOCATION} | DB |

‎shared/typeinference/codeql/typeinference/internal/TypeInference.qll‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,13 +2091,24 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
20912091

20922092
/** An AST node. */
20932093
class AstNode {
2094+
/** Gets the parent of this AST node, if any. */
2095+
AstNode getParent();
2096+
20942097
/** Gets a textual representation of this AST node. */
20952098
string toString();
20962099

20972100
/** Gets the location of this AST node. */
20982101
Location getLocation();
20992102
}
21002103

2104+
/**
2105+
* Gets the scope of type parameter `tp`.
2106+
*
2107+
* Only used in a consistency check to ensure that type parameters do not
2108+
* escape their scope.
2109+
*/
2110+
AstNode getTypeParameterScope(TypeParameter tp);
2111+
21012112
/** An expression. */
21022113
class Expr extends AstNode;
21032114

@@ -3698,6 +3709,11 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
36983709
Input3::inferTypeForDefaults(n, path) = t and
36993710
not inferType(n, path) = t
37003711
}
3712+
3713+
query predicate typeParameterEscapesScope(AstNode n, TypePath path, TypeParameter tp) {
3714+
tp = inferType(n, path) and
3715+
not n.getParent*() = getTypeParameterScope(tp)
3716+
}
37013717
}
37023718

37033719
/**

0 commit comments

Comments
 (0)