Skip to content

Commit 92b0a99

Browse files
committed
wip3
1 parent 12186ee commit 92b0a99

2 files changed

Lines changed: 48 additions & 100 deletions

File tree

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

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,6 @@ private module Input1 implements InputSig1<Location> {
3737

3838
class TypeAbstraction = TA::TypeAbstraction;
3939

40-
class TypeArgumentPosition = int;
41-
42-
private newtype TTypeParameterPosition = TTypeParamTypeParameterPosition(TypeParameter tp)
43-
44-
class TypeParameterPosition extends TTypeParameterPosition {
45-
TypeParameter asTypeParameter() { this = TTypeParamTypeParameterPosition(result) }
46-
47-
TypeParam asTypeParam() {
48-
result = this.asTypeParameter().(TypeParamTypeParameter).getTypeParam()
49-
}
50-
51-
string toString() { result = this.asTypeParameter().toString() }
52-
}
53-
54-
bindingset[apos]
55-
bindingset[ppos]
56-
predicate typeArgumentParameterPositionMatch(TypeArgumentPosition apos, TypeParameterPosition ppos) {
57-
apos = ppos.asTypeParam().getPosition()
58-
}
59-
6040
int getTypeParameterId(TypeParameter tp) {
6141
tp =
6242
rank[result](TypeParameter tp0, int kind, int id1, int id2 |
@@ -421,7 +401,7 @@ private module Input3 implements InputSig3 {
421401
final class Parameterizable = ParameterizableImpl;
422402

423403
abstract private class ParameterizableImpl extends AstNode {
424-
abstract TypeParameter getTypeParameter(TypeParameterPosition ppos);
404+
abstract TypeParameter getTypeParameter(int pos);
425405

426406
abstract TypeMention getAdditionalTypeParameterConstraint(TypeParameter tp);
427407

@@ -443,8 +423,8 @@ private module Input3 implements InputSig3 {
443423
)
444424
}
445425

446-
override TypeParameter getTypeParameter(TypeParameterPosition ppos) {
447-
result = this.(FunctionDeclaration).getTypeParameter(ppos)
426+
override TypeParameter getTypeParameter(int pos) {
427+
result = this.(FunctionDeclaration).getTypeParameter(pos)
448428
}
449429

450430
override TypeMention getAdditionalTypeParameterConstraint(TypeParameter tp) {
@@ -475,9 +455,9 @@ private module Input3 implements InputSig3 {
475455

476456
override TypeMention getAdditionalTypeParameterConstraint(TypeParameter tp) { none() }
477457

478-
override TypeParameter getTypeParameter(TypeParameterPosition ppos) {
479-
result = ppos.asTypeParameter() and
480-
ppos.asTypeParam() = this.getTypeItem().getGenericParamList().getATypeParam()
458+
override TypeParameter getTypeParameter(int pos) {
459+
result.(TypeParamTypeParameter).getTypeParam() =
460+
this.getTypeItem().getGenericParamList().getTypeParam(pos)
481461
}
482462

483463
override TypeMention getType() { result = this.getTypeItem() }
@@ -547,7 +527,7 @@ private module Input3 implements InputSig3 {
547527
abstract private class InvocationImpl extends Expr {
548528
abstract TypeMention getTypeQualifier();
549529

550-
abstract Type getTypeArgument(TypeArgumentPosition apos, TypePath path);
530+
abstract Type getTypeArgument(int pos, TypePath path);
551531

552532
abstract AstNode getArgument(int i);
553533

@@ -559,11 +539,11 @@ private module Input3 implements InputSig3 {
559539
override TypeMention getTypeQualifier() { result = getCallExprTypeArgument2(this) }
560540

561541
pragma[nomagic]
562-
override Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
542+
override Type getTypeArgument(int pos, TypePath path) {
563543
result =
564-
this.(MethodCallExpr).getGenericArgList().getTypeArg(apos).(TypeMention).getTypeAt(path)
544+
this.(MethodCallExpr).getGenericArgList().getTypeArg(pos).(TypeMention).getTypeAt(path)
565545
or
566-
result = getCallExprTypeArgument(this, apos, path)
546+
result = getCallExprTypeArgument(this, pos, path)
567547
}
568548

569549
override AstNode getArgument(int i) {
@@ -606,8 +586,8 @@ private module Input3 implements InputSig3 {
606586
override TypeMention getTypeQualifier() { none() }
607587

608588
pragma[nomagic]
609-
override Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
610-
result = NonAssocCallExpr.super.getTypeArgument(apos, path)
589+
override Type getTypeArgument(int pos, TypePath path) {
590+
result = NonAssocCallExpr.super.getTypeArgument(pos, path)
611591
}
612592

613593
override AstNode getArgument(int i) {
@@ -643,8 +623,8 @@ private module Input3 implements InputSig3 {
643623

644624
override TypeMention getTypeQualifier() { result = getCallExprTypeArgument2(this) }
645625

646-
override Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
647-
result = NonAssocCallExpr.super.getTypeArgument(apos, path)
626+
override Type getTypeArgument(int pos, TypePath path) {
627+
result = NonAssocCallExpr.super.getTypeArgument(pos, path)
648628
}
649629

650630
override AstNode getArgument(int i) {
@@ -667,7 +647,7 @@ private module Input3 implements InputSig3 {
667647
}
668648

669649
pragma[nomagic]
670-
override Type getTypeArgument(TypeArgumentPosition apos, TypePath path) { none() }
650+
override Type getTypeArgument(int pos, TypePath path) { none() }
671651
}
672652

673653
private class StructExprConstruction extends StructConstruction, StructExpr {
@@ -1150,21 +1130,27 @@ private class FunctionDeclaration extends Function {
11501130
this = i.asSome().getAnAssocItem()
11511131
}
11521132

1153-
TypeParam getTypeParam(ImplOrTraitItemNodeOption i) {
1133+
private TypeParam getTypeParam(ImplOrTraitItemNodeOption i, int j) {
11541134
i = parent and
1155-
result = [this.getGenericParamList().getATypeParam(), i.asSome().getTypeParam(_)]
1135+
(
1136+
result = this.getGenericParamList().getTypeParam(j)
1137+
or
1138+
result = i.asSome().getTypeParam(_) and
1139+
j = -1
1140+
)
11561141
}
11571142

1158-
private TypeParameter getTypeParameter(ImplOrTraitItemNodeOption i, TypeParameterPosition ppos) {
1159-
result = ppos.asTypeParameter() and
1143+
private TypeParameter getTypeParameter(ImplOrTraitItemNodeOption i, int pos) {
11601144
(
1161-
result = TTypeParamTypeParameter(this.getTypeParam(i))
1145+
result = TTypeParamTypeParameter(this.getTypeParam(i, pos))
11621146
or
11631147
// For every `TypeParam` of this function, any associated types accessed on
11641148
// the type parameter are also type parameters.
1165-
result.(TypeParamAssociatedTypeTypeParameter).getTypeParam() = this.getTypeParam(i)
1149+
result.(TypeParamAssociatedTypeTypeParameter).getTypeParam() = this.getTypeParam(i, _) and
1150+
pos = -1
11661151
or
11671152
i = parent and
1153+
pos = -1 and
11681154
(
11691155
result = TSelfTypeParameter(i.asSome())
11701156
or
@@ -1175,8 +1161,8 @@ private class FunctionDeclaration extends Function {
11751161
)
11761162
}
11771163

1178-
TypeParameter getTypeParameter(TypeParameterPosition ppos) {
1179-
exists(ImplOrTraitItemNodeOption i | result = this.getTypeParameter(i, ppos) |
1164+
TypeParameter getTypeParameter(int pos) {
1165+
exists(ImplOrTraitItemNodeOption i | result = this.getTypeParameter(i, pos) |
11801166
i.isNone()
11811167
or
11821168
i.asSome().getAnAssocItem() = this
@@ -1337,9 +1323,7 @@ private class NonAssocCallExpr extends CallExpr {
13371323
ItemNode resolveCallTargetViaPathResolution() { result = CallExprImpl::getResolvedFunction(this) }
13381324

13391325
pragma[nomagic]
1340-
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
1341-
result = getCallExprTypeArgument(this, apos, path)
1342-
}
1326+
Type getTypeArgument(int pos, TypePath path) { result = getCallExprTypeArgument(this, pos, path) }
13431327

13441328
AstNode getNodeAt(FunctionPosition pos) {
13451329
result = this.getSyntacticArgument(pos.asArgumentPosition())
@@ -3072,7 +3056,7 @@ private module DeconstructionPatMatchingInput implements MatchingInputSig {
30723056
class Access extends Pat instanceof PathAstNode {
30733057
Access() { this instanceof TupleStructPat or this instanceof StructPat }
30743058

3075-
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) { none() }
3059+
Type getTypeArgument(int pos, TypePath path) { none() }
30763060

30773061
AstNode getNodeAt(AccessPosition apos) {
30783062
this =

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

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -141,35 +141,6 @@ signature module InputSig1<LocationSig Location> {
141141
*/
142142
int getTypeParameterId(TypeParameter tp);
143143

144-
/**
145-
* A type argument position, for example an integer.
146-
*
147-
* Type argument positions are used when type arguments are supplied explicitly,
148-
* for example in a method call like `M<int>()` the type argument `int` is at
149-
* position `0`.
150-
*/
151-
bindingset[this]
152-
class TypeArgumentPosition {
153-
/** Gets the textual representation of this position. */
154-
bindingset[this]
155-
string toString();
156-
}
157-
158-
/** A type parameter position, for example an integer. */
159-
bindingset[this]
160-
class TypeParameterPosition {
161-
/** Gets the textual representation of this position. */
162-
bindingset[this]
163-
string toString();
164-
}
165-
166-
/** Holds if `tapos` and `tppos` match. */
167-
bindingset[tapos]
168-
bindingset[tppos]
169-
predicate typeArgumentParameterPositionMatch(
170-
TypeArgumentPosition tapos, TypeParameterPosition tppos
171-
);
172-
173144
/**
174145
* Gets the limit on the length of type paths. Set to `none()` if there should
175146
* be no limit.
@@ -1211,8 +1182,8 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
12111182
/** Gets the location of this declaration. */
12121183
Location getLocation();
12131184

1214-
/** Gets the type parameter at position `tppos` of this declaration, if any. */
1215-
TypeParameter getTypeParameter(TypeParameterPosition tppos);
1185+
/** Gets the type parameter at position `pos` of this declaration, if any. */
1186+
TypeParameter getTypeParameter(int pos);
12161187

12171188
/**
12181189
* Gets the declared type of this declaration at `path` for position `dpos`.
@@ -1262,13 +1233,13 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
12621233
Location getLocation();
12631234

12641235
/**
1265-
* Gets the type at `path` for the type argument at position `tapos` of
1236+
* Gets the type at `path` for the type argument at position `pos` of
12661237
* this access, if any.
12671238
*
12681239
* For example, in a method call like `M<int>()`, `int` is an explicit
12691240
* type argument at position `0`.
12701241
*/
1271-
Type getTypeArgument(TypeArgumentPosition tapos, TypePath path);
1242+
Type getTypeArgument(int pos, TypePath path);
12721243

12731244
/**
12741245
* Gets the inferred type at `path` for the position `apos` and environment `e`
@@ -1299,14 +1270,6 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
12991270
module MatchingWithEnvironment<MatchingWithEnvironmentInputSig Input> {
13001271
private import Input
13011272

1302-
pragma[nomagic]
1303-
private TypeParameter getDeclTypeParameter(Declaration decl, TypeArgumentPosition tapos) {
1304-
exists(TypeParameterPosition tppos |
1305-
result = decl.getTypeParameter(tppos) and
1306-
typeArgumentParameterPositionMatch(tapos, tppos)
1307-
)
1308-
}
1309-
13101273
/**
13111274
* Gets the type of the type argument at `path` in `a` that corresponds to
13121275
* the type parameter `tp` in `target`, if any.
@@ -1318,9 +1281,9 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
13181281
bindingset[a, target]
13191282
pragma[inline_late]
13201283
Type getTypeArgument(Access a, Declaration target, TypeParameter tp, TypePath path) {
1321-
exists(TypeArgumentPosition tapos |
1322-
result = a.getTypeArgument(tapos, path) and
1323-
tp = getDeclTypeParameter(target, tapos) and
1284+
exists(int pos |
1285+
result = a.getTypeArgument(pos, path) and
1286+
tp = target.getTypeParameter(pos) and
13241287
not result instanceof PseudoType
13251288
)
13261289
}
@@ -1819,8 +1782,8 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
18191782
/** Gets the location of this declaration. */
18201783
Location getLocation();
18211784

1822-
/** Gets the type parameter at position `tppos` of this declaration, if any. */
1823-
TypeParameter getTypeParameter(TypeParameterPosition tppos);
1785+
/** Gets the type parameter at position `pos` of this declaration, if any. */
1786+
TypeParameter getTypeParameter(int pos);
18241787

18251788
/**
18261789
* Gets the declared type of this declaration at `path` for position `dpos`.
@@ -1868,7 +1831,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
18681831
* For example, in a method call like `M<int>()`, `int` is an explicit
18691832
* type argument at position `0`.
18701833
*/
1871-
Type getTypeArgument(TypeArgumentPosition tapos, TypePath path);
1834+
Type getTypeArgument(int pos, TypePath path);
18721835

18731836
/**
18741837
* Gets the inferred type at `path` for the position `apos` of this access.
@@ -2120,12 +2083,13 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
21202083
Type getDeclaringType(TypePath path);
21212084

21222085
/**
2123-
* Gets the type parameter at position `ppos` of this element, if any.
2086+
* Gets the type parameter at position `pos` of this element, if any.
21242087
*
2088+
* TODO:
21252089
* This should include type parameters declared on the element itself,
21262090
* as well as type parameters declared on the enclosing declaration(s).
21272091
*/
2128-
TypeParameter getTypeParameter(TypeParameterPosition ppos);
2092+
TypeParameter getTypeParameter(int pos);
21292093

21302094
/**
21312095
* Gets an additional type parameter constraint for the given type parameter,
@@ -2182,8 +2146,8 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
21822146

21832147
/** An invocation expression, for example a call or a variant construction. */
21842148
class Invocation extends Expr {
2185-
/** Gets the explicit type argument at position `apos` and `path` for this invocation, if any. */
2186-
Type getTypeArgument(TypeArgumentPosition apos, TypePath path);
2149+
/** Gets the explicit type argument at position `pos` and `path` for this invocation, if any. */
2150+
Type getTypeArgument(int pos, TypePath path);
21872151

21882152
/**
21892153
* Gets the AST node corresponding to the `i`th argument of this invocation.
@@ -3037,7 +3001,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
30373001
final private class MemberFinal = Member;
30383002

30393003
class Declaration extends MemberFinal {
3040-
TypeParameter getTypeParameter(TypeParameterPosition ppos) { none() }
3004+
TypeParameter getTypeParameter(int pos) { none() }
30413005

30423006
Type getDeclaredType(DeclarationPosition dpos, TypePath path) {
30433007
dpos.isReceiver() and
@@ -3057,7 +3021,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
30573021
final private class MemberAccessFinal = MemberAccess;
30583022

30593023
class Access extends MemberAccessFinal {
3060-
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) { none() }
3024+
Type getTypeArgument(int pos, TypePath path) { none() }
30613025

30623026
Type getInferredType(AccessPosition apos, TypePath path) {
30633027
apos.isReceiver() and

0 commit comments

Comments
 (0)