Skip to content

Commit 98d8cd1

Browse files
committed
Rust: Make getATypeParameterConstraint return a TypeMention again
1 parent e8e46ac commit 98d8cd1

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,15 @@ private module Input implements InputSig1<Location>, InputSig2<PreTypeMention> {
127127

128128
PreTypeMention getABaseTypeMention(Type t) { none() }
129129

130-
Type getATypeParameterConstraint(TypeParameter tp, TypePath path) {
131-
exists(TypeMention tm | result = tm.getTypeAt(path) |
132-
tm = tp.(TypeParamTypeParameter).getTypeParam().getATypeBound().getTypeRepr() or
133-
tm = tp.(SelfTypeParameter).getTrait() or
134-
tm =
135-
tp.(ImplTraitTypeTypeParameter)
136-
.getImplTraitTypeRepr()
137-
.getTypeBoundList()
138-
.getABound()
139-
.getTypeRepr()
140-
)
130+
PreTypeMention getATypeParameterConstraint(TypeParameter tp) {
131+
result = tp.(TypeParamTypeParameter).getTypeParam().getATypeBound().getTypeRepr() or
132+
result = tp.(SelfTypeParameter).getTrait() or
133+
result =
134+
tp.(ImplTraitTypeTypeParameter)
135+
.getImplTraitTypeRepr()
136+
.getTypeBoundList()
137+
.getABound()
138+
.getTypeRepr()
141139
}
142140

143141
/**
@@ -988,7 +986,7 @@ private module ContextTyping {
988986
or
989987
exists(TypeParameter mid |
990988
assocFunctionMentionsTypeParameterAtNonRetPos(i, f, mid) and
991-
tp = getATypeParameterConstraint(mid, _)
989+
tp = getATypeParameterConstraint(mid).getTypeAt(_)
992990
)
993991
}
994992

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
336336
* ```
337337
* the type parameter `T` has the constraint `IComparable<T>`.
338338
*/
339-
Type getATypeParameterConstraint(TypeParameter tp, TypePath path);
339+
TypeMention getATypeParameterConstraint(TypeParameter tp);
340340

341341
/**
342342
* Holds if
@@ -1308,7 +1308,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
13081308
private module AccessConstraint {
13091309
predicate relevantAccessConstraint(
13101310
Access a, AccessEnvironment e, Declaration target, AccessPosition apos, TypePath path,
1311-
Type constraint
1311+
TypeMention constraint
13121312
) {
13131313
target = a.getTarget(e) and
13141314
typeParameterConstraintHasTypeParameter(target, apos, path, constraint, _, _)
@@ -1336,7 +1336,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
13361336
}
13371337

13381338
/** Gets the constraint that this relevant access should satisfy. */
1339-
Type getConstraint(Declaration target) {
1339+
TypeMention getConstraint(Declaration target) {
13401340
relevantAccessConstraint(a, e, target, apos, path, result)
13411341
}
13421342

@@ -1347,20 +1347,24 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
13471347
Location getLocation() { result = a.getLocation() }
13481348
}
13491349

1350-
private module SatisfiesConstraintInput implements SatisfiesTypeInputSig<RelevantAccess> {
1351-
predicate relevantConstraint(RelevantAccess at, Type constraint) {
1350+
private module SatisfiesTypeParameterConstraintInput implements
1351+
SatisfiesConstraintInputSig<RelevantAccess, TypeMention>
1352+
{
1353+
predicate relevantConstraint(RelevantAccess at, TypeMention constraint) {
13521354
constraint = at.getConstraint(_)
13531355
}
13541356
}
13551357

1358+
private module SatisfiesTypeParameterConstraint =
1359+
SatisfiesConstraint<RelevantAccess, TypeMention, SatisfiesTypeParameterConstraintInput>;
1360+
13561361
predicate satisfiesConstraintType(
13571362
Access a, AccessEnvironment e, Declaration target, AccessPosition apos, TypePath prefix,
1358-
Type constraint, TypePath path, Type t
1363+
TypeMention constraint, TypePath path, Type t
13591364
) {
13601365
exists(RelevantAccess ra |
13611366
ra = MkRelevantAccess(a, apos, e, prefix) and
1362-
SatisfiesType<RelevantAccess, SatisfiesConstraintInput>::satisfiesConstraintType(ra,
1363-
constraint, path, t) and
1367+
SatisfiesTypeParameterConstraint::satisfiesConstraintType(ra, constraint, path, t) and
13641368
constraint = ra.getConstraint(target)
13651369
)
13661370
}
@@ -1469,17 +1473,17 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
14691473
*/
14701474
pragma[nomagic]
14711475
private predicate typeParameterConstraintHasTypeParameter(
1472-
Declaration target, AccessPosition apos, TypePath pathToConstrained, Type constraint,
1476+
Declaration target, AccessPosition apos, TypePath pathToConstrained, TypeMention constraint,
14731477
TypePath pathToTp, TypeParameter tp
14741478
) {
14751479
exists(DeclarationPosition dpos, TypeParameter constrainedTp |
14761480
accessDeclarationPositionMatch(apos, dpos) and
14771481
constrainedTp = target.getTypeParameter(_) and
1482+
constraint = getATypeParameterConstraint(constrainedTp) and
14781483
tp = target.getTypeParameter(_) and
1479-
tp = getATypeParameterConstraint(constrainedTp, pathToTp) and
1484+
tp = constraint.getTypeAt(pathToTp) and
14801485
constrainedTp != tp and
1481-
constrainedTp = target.getDeclaredType(dpos, pathToConstrained) and
1482-
constraint = getATypeParameterConstraint(constrainedTp, TypePath::nil())
1486+
constrainedTp = target.getDeclaredType(dpos, pathToConstrained)
14831487
)
14841488
}
14851489

@@ -1488,7 +1492,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
14881492
Access a, AccessEnvironment e, Declaration target, TypePath path, Type t, TypeParameter tp
14891493
) {
14901494
not exists(getTypeArgument(a, target, tp, _)) and
1491-
exists(Type constraint, AccessPosition apos, TypePath pathToTp, TypePath pathToTp2 |
1495+
exists(TypeMention constraint, AccessPosition apos, TypePath pathToTp, TypePath pathToTp2 |
14921496
typeParameterConstraintHasTypeParameter(target, apos, pathToTp2, constraint, pathToTp, tp) and
14931497
AccessConstraint::satisfiesConstraintType(a, e, target, apos, pathToTp2, constraint,
14941498
pathToTp.appendInverse(path), t)

0 commit comments

Comments
 (0)