Skip to content

Commit 793d2c7

Browse files
authored
Merge pull request #21215 from paldepind/shared/type-mention-tree
Shared, Rust: Use `HasTypeTreeSig` for `TypeMention`
2 parents f76211c + 452d307 commit 793d2c7

File tree

9 files changed

+139
-164
lines changed

9 files changed

+139
-164
lines changed

rust/ql/lib/codeql/rust/dataflow/internal/ModelsAsData.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private module Debug {
224224
sc.propagatesFlow(input, _, _, _) and
225225
input.head() = SummaryComponent::argument(pos) and
226226
p = pos.getParameterIn(sc.getParamList()) and
227-
tm.resolveType() instanceof RefType and
227+
tm.getType() instanceof RefType and
228228
not input.tail().head() = SummaryComponent::content(TSingletonContentSet(TReferenceContent()))
229229
|
230230
tm = p.getTypeRepr()
@@ -239,7 +239,7 @@ private module Debug {
239239
exists(TypeMention tm |
240240
relevantManualModel(sc, can) and
241241
sc.propagatesFlow(_, output, _, _) and
242-
tm.resolveType() instanceof RefType and
242+
tm.getType() instanceof RefType and
243243
output.head() = SummaryComponent::return(_) and
244244
not output.tail().head() =
245245
SummaryComponent::content(TSingletonContentSet(TReferenceContent())) and

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ predicate isBlanketLike(ImplItemNode i, TypePath blanketSelfPath, TypeParam blan
4747
exists(TypeMention tm, Type root, TypeParameter tp |
4848
tm = i.(Impl).getSelfTy() and
4949
complexSelfRoot(root, tp) and
50-
tm.resolveType() = root and
51-
tm.resolveTypeAt(blanketSelfPath) = TTypeParamTypeParameter(blanketTypeParam) and
50+
tm.getType() = root and
51+
tm.getTypeAt(blanketSelfPath) = TTypeParamTypeParameter(blanketTypeParam) and
5252
blanketSelfPath = TypePath::singleton(tp) and
5353
hasFirstNonTrivialTraitBound(blanketTypeParam, _)
5454
)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class DerefImplItemNode extends ImplItemNode {
2525
*/
2626
pragma[nomagic]
2727
predicate targetHasTypeParameterAt(TypePath path) {
28-
this.getAssocItem("Target").(TypeAlias).getTypeRepr().(TypeMention).resolveTypeAt(path)
29-
instanceof TypeParameter
28+
this.getAssocItem("Target").(TypeAlias).getTypeRepr().(TypeMention).getTypeAt(path) instanceof
29+
TypeParameter
3030
}
3131

3232
/** Gets the first type parameter of the type being implemented, if any. */

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private import FunctionType
1515

1616
pragma[nomagic]
1717
private Type resolveNonTypeParameterTypeAt(TypeMention tm, TypePath path) {
18-
result = tm.resolveTypeAt(path) and
18+
result = tm.getTypeAt(path) and
1919
not result instanceof TypeParameter
2020
}
2121

@@ -32,7 +32,7 @@ private predicate implSiblingCandidate(
3232
) {
3333
trait = impl.(ImplItemNode).resolveTraitTy() and
3434
selfTy = impl.getSelfTy() and
35-
rootType = selfTy.resolveType()
35+
rootType = selfTy.getType()
3636
}
3737

3838
pragma[nomagic]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ Type getAssocFunctionTypeAt(Function f, ImplOrTraitItemNode i, FunctionPosition
103103
// No specialization needed when the function is directly in the trait or
104104
// impl block or the declared type is not a type parameter
105105
(parent = i or not result instanceof TypeParameter) and
106-
result = pos.getTypeMention(f).resolveTypeAt(path)
106+
result = pos.getTypeMention(f).getTypeAt(path)
107107
or
108108
exists(TypePath prefix, TypePath suffix, TypeParameter tp, TypeMention constraint |
109109
BaseTypes::rootTypesSatisfaction(_, TTrait(parent), i, _, constraint) and
110110
path = prefix.append(suffix) and
111-
tp = pos.getTypeMention(f).resolveTypeAt(prefix) and
111+
tp = pos.getTypeMention(f).getTypeAt(prefix) and
112112
if tp = TSelfTypeParameter(_)
113113
then result = resolveImplOrTraitType(i, suffix)
114114
else result = getTraitConstraintTypeAt(i, constraint, tp, suffix)

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

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,11 @@ class TypePath = M1::TypePath;
134134

135135
module TypePath = M1::TypePath;
136136

137-
private module Input2 implements InputSig2 {
138-
private import TypeMention as TM
139-
140-
class TypeMention = TM::TypeMention;
141-
137+
private module Input2 implements InputSig2<TypeMention> {
142138
TypeMention getABaseTypeMention(Type t) { none() }
143139

144140
Type getATypeParameterConstraint(TypeParameter tp, TypePath path) {
145-
exists(TypeMention tm | result = tm.resolveTypeAt(path) |
141+
exists(TypeMention tm | result = tm.getTypeAt(path) |
146142
tm = tp.(TypeParamTypeParameter).getTypeParam().getATypeBound().getTypeRepr() or
147143
tm = tp.(SelfTypeParameter).getTrait() or
148144
tm =
@@ -212,7 +208,7 @@ private module Input2 implements InputSig2 {
212208
}
213209
}
214210

215-
private module M2 = Make2<Input2>;
211+
private module M2 = Make2<TypeMention, Input2>;
216212

217213
import M2
218214

@@ -231,7 +227,7 @@ module Consistency {
231227
// mention for the self type has multiple types for a path.
232228
not exists(ImplItemNode impl, TypePath selfTypePath |
233229
n = impl.getAnAssocItem().(Function).getSelfParam() and
234-
strictcount(impl.(Impl).getSelfTy().(TypeMention).resolveTypeAt(selfTypePath)) > 1
230+
strictcount(impl.(Impl).getSelfTy().(TypeMention).getTypeAt(selfTypePath)) > 1
235231
)
236232
}
237233
}
@@ -297,7 +293,7 @@ private class FunctionDeclaration extends Function {
297293
result = getAssocFunctionTypeAt(this, i.asSome(), pos, path)
298294
or
299295
i.isNone() and
300-
result = this.getParam(pos.asPosition()).getTypeRepr().(TypeMention).resolveTypeAt(path)
296+
result = this.getParam(pos.asPosition()).getTypeRepr().(TypeMention).getTypeAt(path)
301297
)
302298
}
303299

@@ -308,7 +304,7 @@ private class FunctionDeclaration extends Function {
308304
getAssocFunctionTypeAt(this, i.asSome(), any(FunctionPosition pos | pos.isReturn()), path)
309305
or
310306
i.isNone() and
311-
result = getReturnTypeMention(this).resolveTypeAt(path)
307+
result = getReturnTypeMention(this).getTypeAt(path)
312308
)
313309
}
314310

@@ -352,12 +348,12 @@ private TypeMention getCallExprTypeMentionArgument(CallExpr ce, TypeArgumentPosi
352348

353349
pragma[nomagic]
354350
private Type getCallExprTypeArgument(CallExpr ce, TypeArgumentPosition apos, TypePath path) {
355-
result = getCallExprTypeMentionArgument(ce, apos).resolveTypeAt(path)
351+
result = getCallExprTypeMentionArgument(ce, apos).getTypeAt(path)
356352
or
357353
// Handle constructions that use `Self(...)` syntax
358354
exists(Path p, TypePath path0 |
359355
p = CallExprImpl::getFunctionPath(ce) and
360-
result = p.(TypeMention).resolveTypeAt(path0) and
356+
result = p.(TypeMention).getTypeAt(path0) and
361357
path0.isCons(TTypeParamTypeParameter(apos.asTypeParam()), path)
362358
)
363359
}
@@ -380,16 +376,16 @@ private TypeMention getTypeAnnotation(AstNode n) {
380376
/** Gets the type of `n`, which has an explicit type annotation. */
381377
pragma[nomagic]
382378
private Type inferAnnotatedType(AstNode n, TypePath path) {
383-
result = getTypeAnnotation(n).resolveTypeAt(path)
379+
result = getTypeAnnotation(n).getTypeAt(path)
384380
or
385-
result = n.(ShorthandSelfParameterMention).resolveTypeAt(path)
381+
result = n.(ShorthandSelfParameterMention).getTypeAt(path)
386382
}
387383

388384
pragma[nomagic]
389385
private Type inferFunctionBodyType(AstNode n, TypePath path) {
390386
exists(Function f |
391387
n = f.getFunctionBody() and
392-
result = getReturnTypeMention(f).resolveTypeAt(path) and
388+
result = getReturnTypeMention(f).getTypeAt(path) and
393389
not exists(ImplTraitReturnType i | i.getFunction() = f |
394390
result = i or result = i.getATypeParameter()
395391
)
@@ -433,7 +429,7 @@ module CertainTypeInference {
433429
private TypePath getPathToImplSelfTypeParam(TypeParam tp) {
434430
exists(ImplItemNode impl |
435431
tp = impl.getTypeParam(_) and
436-
TTypeParamTypeParameter(tp) = impl.(Impl).getSelfTy().(TypeMention).resolveTypeAt(result)
432+
TTypeParamTypeParameter(tp) = impl.(Impl).getSelfTy().(TypeMention).getTypeAt(result)
437433
)
438434
}
439435

@@ -449,7 +445,7 @@ module CertainTypeInference {
449445
// and the path `Foo<i64>::bar` we must resolve `A` to `i64`.
450446
exists(TypePath pathToTp |
451447
pathToTp = getPathToImplSelfTypeParam(tp) and
452-
result = p.getQualifier().(TypeMention).resolveTypeAt(pathToTp.appendInverse(suffix))
448+
result = p.getQualifier().(TypeMention).getTypeAt(pathToTp.appendInverse(suffix))
453449
)
454450
or
455451
// For type parameters of the function we must resolve their
@@ -465,11 +461,11 @@ module CertainTypeInference {
465461
}
466462

467463
private Type inferCertainStructExprType(StructExpr se, TypePath path) {
468-
result = se.getPath().(TypeMention).resolveTypeAt(path)
464+
result = se.getPath().(TypeMention).getTypeAt(path)
469465
}
470466

471467
private Type inferCertainStructPatType(StructPat sp, TypePath path) {
472-
result = sp.getPath().(TypeMention).resolveTypeAt(path)
468+
result = sp.getPath().(TypeMention).getTypeAt(path)
473469
}
474470

475471
predicate certainTypeEquality(AstNode n1, TypePath prefix1, AstNode n2, TypePath prefix2) {
@@ -905,7 +901,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
905901
// type of a field
906902
exists(TypeMention tp |
907903
tp = this.getField(dpos.asFieldPos()).getTypeRepr() and
908-
result = tp.resolveTypeAt(path)
904+
result = tp.getTypeAt(path)
909905
)
910906
or
911907
// type parameter of the struct itself
@@ -958,7 +954,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
958954
// Handle constructions that use `Self {...}` syntax
959955
exists(TypeMention tm, TypePath path0 |
960956
tm = this.getStructPath() and
961-
result = tm.resolveTypeAt(path0) and
957+
result = tm.getTypeAt(path0) and
962958
path0.isCons(TTypeParamTypeParameter(apos.asTypeParam()), path)
963959
)
964960
}
@@ -1068,7 +1064,7 @@ pragma[nomagic]
10681064
private Type getCallExprTypeQualifier(CallExpr ce, TypePath path) {
10691065
exists(TypeMention tm |
10701066
tm = getCallExprPathQualifier(ce) and
1071-
result = tm.resolveTypeAt(path) and
1067+
result = tm.getTypeAt(path) and
10721068
not resolvePath(tm) instanceof Trait
10731069
)
10741070
}
@@ -2330,19 +2326,19 @@ private module MethodResolution {
23302326
* instance of the type being implemented.
23312327
*/
23322328
private module TypeQualifierIsInstantiationOfImplSelfInput implements
2333-
IsInstantiationOfInputSig<MethodCallCallExpr, TypeMentionTypeTree>
2329+
IsInstantiationOfInputSig<MethodCallCallExpr, TypeMention>
23342330
{
23352331
pragma[nomagic]
23362332
private predicate potentialInstantiationOf0(
2337-
MethodCallCallExpr ce, ImplItemNode impl, TypeMentionTypeTree constraint
2333+
MethodCallCallExpr ce, ImplItemNode impl, TypeMention constraint
23382334
) {
23392335
ce.hasTypeQualifiedCandidate(impl) and
23402336
constraint = impl.getSelfPath()
23412337
}
23422338

23432339
pragma[nomagic]
23442340
predicate potentialInstantiationOf(
2345-
MethodCallCallExpr ce, TypeAbstraction abs, TypeMentionTypeTree constraint
2341+
MethodCallCallExpr ce, TypeAbstraction abs, TypeMention constraint
23462342
) {
23472343
potentialInstantiationOf0(ce, abs, constraint) and
23482344
if abs.(Impl).hasTrait()
@@ -2353,14 +2349,13 @@ private module MethodResolution {
23532349
else any()
23542350
}
23552351

2356-
predicate relevantConstraint(TypeMentionTypeTree constraint) {
2352+
predicate relevantConstraint(TypeMention constraint) {
23572353
potentialInstantiationOf0(_, _, constraint)
23582354
}
23592355
}
23602356

23612357
private module TypeQualifierIsInstantiationOfImplSelf =
2362-
IsInstantiationOf<MethodCallCallExpr, TypeMentionTypeTree,
2363-
TypeQualifierIsInstantiationOfImplSelfInput>;
2358+
IsInstantiationOf<MethodCallCallExpr, TypeMention, TypeQualifierIsInstantiationOfImplSelfInput>;
23642359

23652360
/**
23662361
* A configuration for anti-matching the type of a receiver against the type of
@@ -2481,7 +2476,7 @@ private module MethodCallMatchingInput implements MatchingWithEnvironmentInputSi
24812476
.getGenericArgList()
24822477
.getTypeArg(apos.asMethodTypeArgumentPosition())
24832478
.(TypeMention)
2484-
.resolveTypeAt(path)
2479+
.getTypeAt(path)
24852480
or
24862481
result = getCallExprTypeArgument(this, apos, path)
24872482
}
@@ -3012,7 +3007,7 @@ abstract private class TupleLikeConstructor extends Addressable {
30123007
}
30133008

30143009
Type getParameterType(FunctionPosition pos, TypePath path) {
3015-
result = this.getTupleField(pos.asPosition()).getTypeRepr().(TypeMention).resolveTypeAt(path)
3010+
result = this.getTupleField(pos.asPosition()).getTypeRepr().(TypeMention).getTypeAt(path)
30163011
}
30173012
}
30183013

@@ -3367,7 +3362,7 @@ private module FieldExprMatchingInput implements MatchingInputSig {
33673362
)
33683363
or
33693364
dpos.isField() and
3370-
result = this.getTypeRepr().(TypeMention).resolveTypeAt(path)
3365+
result = this.getTypeRepr().(TypeMention).getTypeAt(path)
33713366
}
33723367

33733368
override string toString() { result = this.getAstNode().toString() }
@@ -3719,7 +3714,7 @@ private module StructPatMatchingInput implements MatchingInputSig {
37193714
// The struct/enum type is supplied explicitly as a type qualifier, e.g.
37203715
// `let Foo<Bar>::Variant { ... } = ...`.
37213716
apos.isStructPos() and
3722-
result = this.getPath().(TypeMention).resolveTypeAt(path)
3717+
result = this.getPath().(TypeMention).getTypeAt(path)
37233718
}
37243719

37253720
Declaration getTarget() { result = resolvePath(this.getPath()) }
@@ -3769,7 +3764,7 @@ private module TupleStructPatMatchingInput implements MatchingInputSig {
37693764
// The struct/enum type is supplied explicitly as a type qualifier, e.g.
37703765
// `let Option::<Foo>::Some(x) = ...`.
37713766
apos.isSelf() and
3772-
result = this.getPath().(TypeMention).resolveTypeAt(path)
3767+
result = this.getPath().(TypeMention).getTypeAt(path)
37733768
}
37743769

37753770
Declaration getTarget() { result = resolvePath(this.getPath()) }
@@ -3958,13 +3953,13 @@ private Type inferClosureExprType(AstNode n, TypePath path) {
39583953
or
39593954
// Propagate return type annotation to body
39603955
n = ce.getClosureBody() and
3961-
result = ce.getRetType().getTypeRepr().(TypeMention).resolveTypeAt(path)
3956+
result = ce.getRetType().getTypeRepr().(TypeMention).getTypeAt(path)
39623957
)
39633958
}
39643959

39653960
pragma[nomagic]
39663961
private Type inferCastExprType(CastExpr ce, TypePath path) {
3967-
result = ce.getTypeRepr().(TypeMention).resolveTypeAt(path)
3962+
result = ce.getTypeRepr().(TypeMention).getTypeAt(path)
39683963
}
39693964

39703965
cached
@@ -4162,7 +4157,7 @@ private module Debug {
41624157

41634158
predicate debugInferShorthandSelfType(ShorthandSelfParameterMention self, TypePath path, Type t) {
41644159
self = getRelevantLocatable() and
4165-
t = self.resolveTypeAt(path)
4160+
t = self.getTypeAt(path)
41664161
}
41674162

41684163
predicate debugInferMethodCallType(AstNode n, TypePath path, Type t) {
@@ -4177,7 +4172,7 @@ private module Debug {
41774172

41784173
predicate debugTypeMention(TypeMention tm, TypePath path, Type type) {
41794174
tm = getRelevantLocatable() and
4180-
tm.resolveTypeAt(path) = type
4175+
tm.getTypeAt(path) = type
41814176
}
41824177

41834178
Type debugInferAnnotatedType(AstNode n, TypePath path) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ query predicate illFormedTypeMention(TypeMention tm) {
1717
not tm =
1818
any(PathTypeMention ptm |
1919
exists(ptm.resolvePathTypeAt(TypePath::nil())) and
20-
not exists(ptm.resolveType())
20+
not exists(ptm.getType())
2121
or
2222
ptm.(NonAliasPathTypeMention).getResolved() instanceof TypeAlias
2323
) and

0 commit comments

Comments
 (0)