Skip to content

Commit 4909d80

Browse files
committed
Shared: Generalize typeConstraintBaseTypeMatch
1 parent 3324d07 commit 4909d80

1 file changed

Lines changed: 44 additions & 59 deletions

File tree

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

Lines changed: 44 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,14 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
13461346
module MatchingWithEnvironment<MatchingWithEnvironmentInputSig Input> {
13471347
private import Input
13481348

1349+
pragma[nomagic]
1350+
private TypeParameter getDeclTypeParameter(Declaration decl, TypeArgumentPosition tapos) {
1351+
exists(TypeParameterPosition tppos |
1352+
result = decl.getTypeParameter(tppos) and
1353+
typeArgumentParameterPositionMatch(tapos, tppos)
1354+
)
1355+
}
1356+
13491357
/**
13501358
* Gets the type of the type argument at `path` in `a` that corresponds to
13511359
* the type parameter `tp` in `target`, if any.
@@ -1356,11 +1364,11 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
13561364
*/
13571365
bindingset[a, target]
13581366
pragma[inline_late]
1359-
private Type getTypeArgument(Access a, Declaration target, TypeParameter tp, TypePath path) {
1360-
exists(TypeArgumentPosition tapos, TypeParameterPosition tppos |
1367+
Type getTypeArgument(Access a, Declaration target, TypeParameter tp, TypePath path) {
1368+
exists(TypeArgumentPosition tapos |
13611369
result = a.getTypeArgument(tapos, path) and
1362-
tp = target.getTypeParameter(tppos) and
1363-
typeArgumentParameterPositionMatch(tapos, tppos)
1370+
tp = getDeclTypeParameter(target, tapos) and
1371+
not isPseudoType(result)
13641372
)
13651373
}
13661374

@@ -1526,42 +1534,35 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
15261534

15271535
private module AccessConstraint {
15281536
private predicate relevantAccessConstraint(
1529-
Access a, AccessEnvironment e, Declaration target, AccessPosition apos, TypePath path,
1530-
TypeMention constraint
1537+
Access a, AccessEnvironment e, Declaration target, TypeParameter constrainedTp,
1538+
AccessPosition apos, TypePath path, TypeMention constraint
15311539
) {
15321540
target = a.getTarget(e) and
1533-
typeParameterHasConstraint(target, apos, _, path, constraint)
1541+
typeParameterHasConstraint(target, apos, constrainedTp, path, constraint)
15341542
}
15351543

15361544
private newtype TRelevantAccess =
1537-
MkRelevantAccess(Access a, AccessPosition apos, AccessEnvironment e, TypePath path) {
1538-
relevantAccessConstraint(a, e, _, apos, path, _)
1545+
MkRelevantAccess(Access a, AccessEnvironment e, TypeParameter constrainedTp) {
1546+
relevantAccessConstraint(a, e, _, constrainedTp, _, _, _)
15391547
}
15401548

1541-
/**
1542-
* If the access `a` for `apos`, environment `e`, and `path` has an inferred type
1543-
* which type inference requires to satisfy some constraint.
1544-
*/
15451549
private class RelevantAccess extends MkRelevantAccess {
15461550
Access a;
1547-
AccessPosition apos;
15481551
AccessEnvironment e;
1549-
TypePath path;
1552+
TypeParameter constrainedTp;
15501553

1551-
RelevantAccess() { this = MkRelevantAccess(a, apos, e, path) }
1554+
RelevantAccess() { this = MkRelevantAccess(a, e, constrainedTp) }
15521555

15531556
pragma[nomagic]
1554-
Type getTypeAt(TypePath suffix) {
1555-
result = a.getInferredType(e, apos, path.appendInverse(suffix))
1556-
}
1557+
Type getTypeAt(TypePath path) { typeMatch(a, e, _, path, result, constrainedTp) }
15571558

15581559
/** Gets the constraint that this relevant access should satisfy. */
15591560
TypeMention getConstraint(Declaration target) {
1560-
relevantAccessConstraint(a, e, target, apos, path, result)
1561+
relevantAccessConstraint(a, e, target, constrainedTp, _, _, result)
15611562
}
15621563

15631564
string toString() {
1564-
result = a.toString() + ", " + apos.toString() + ", " + path.toString()
1565+
result = a.toString() + ", " + e.toString() + ", " + constrainedTp.toString()
15651566
}
15661567

15671568
Location getLocation() { result = a.getLocation() }
@@ -1577,7 +1578,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
15771578
class TypeMatchingContext = Access;
15781579

15791580
TypeMatchingContext getTypeMatchingContext(RelevantAccess at) {
1580-
at = MkRelevantAccess(result, _, _, _)
1581+
at = MkRelevantAccess(result, _, _)
15811582
}
15821583

15831584
pragma[nomagic]
@@ -1592,40 +1593,27 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
15921593

15931594
pragma[nomagic]
15941595
predicate satisfiesConstraintAtTypeParameter(
1595-
Access a, AccessEnvironment e, Declaration target, AccessPosition apos, TypePath prefix,
1596+
Access a, AccessEnvironment e, Declaration target, AccessPosition apos, TypePath path,
15961597
TypeMention constraint, TypePath pathToTypeParamInConstraint,
15971598
TypePath pathToTypeParamInSub
15981599
) {
1599-
exists(RelevantAccess ra |
1600-
ra = MkRelevantAccess(a, apos, e, prefix) and
1600+
exists(RelevantAccess ra, TypeParameter constrainedTp |
1601+
ra = MkRelevantAccess(a, e, constrainedTp) and
1602+
relevantAccessConstraint(a, e, target, constrainedTp, apos, path, constraint) and
16011603
SatisfiesTypeParameterConstraint::satisfiesConstraintAtTypeParameter(ra, constraint,
1602-
pathToTypeParamInConstraint, pathToTypeParamInSub) and
1603-
constraint = ra.getConstraint(target)
1604+
pathToTypeParamInConstraint, pathToTypeParamInSub)
16041605
)
16051606
}
16061607

16071608
pragma[nomagic]
16081609
predicate satisfiesConstraint(
1609-
Access a, AccessEnvironment e, Declaration target, AccessPosition apos, TypePath prefix,
1610+
Access a, AccessEnvironment e, Declaration target, TypeParameter constrainedTp,
16101611
TypeMention constraint, TypePath path, Type t
16111612
) {
16121613
exists(RelevantAccess ra |
1613-
ra = MkRelevantAccess(a, apos, e, prefix) and
1614-
SatisfiesTypeParameterConstraint::satisfiesConstraint(ra, constraint, path, t) and
1615-
constraint = ra.getConstraint(target)
1616-
)
1617-
}
1618-
1619-
pragma[nomagic]
1620-
predicate satisfiesConstraintThrough(
1621-
Access a, AccessEnvironment e, Declaration target, AccessPosition apos, TypePath prefix,
1622-
TypeAbstraction abs, TypeMention constraint, TypePath path, Type t
1623-
) {
1624-
exists(RelevantAccess ra |
1625-
ra = MkRelevantAccess(a, apos, e, prefix) and
1626-
SatisfiesTypeParameterConstraint::satisfiesConstraintThrough(ra, abs, constraint, path,
1627-
t) and
1628-
constraint = ra.getConstraint(target)
1614+
ra = MkRelevantAccess(a, e, constrainedTp) and
1615+
relevantAccessConstraint(a, e, target, constrainedTp, _, _, constraint) and
1616+
SatisfiesTypeParameterConstraint::satisfiesConstraint(ra, constraint, path, t)
16291617
)
16301618
}
16311619
}
@@ -1662,9 +1650,9 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
16621650
}
16631651

16641652
/**
1665-
* Holds if the declared type of `target` contains a type parameter at
1666-
* `apos` and `pathToConstrained` that must satisfy `constraint` and `tp`
1667-
* occurs at `pathToTp` in `constraint`.
1653+
* Holds if the declared type of `target` contains type parameter `constrainedTp`
1654+
* at `apos` that must satisfy `constraint` and `tp` occurs at `pathToTp` in
1655+
* `constraint`.
16681656
*
16691657
* For example, in
16701658
* ```csharp
@@ -1673,32 +1661,29 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
16731661
* ```
16741662
* with the method declaration being the target and with `apos`
16751663
* corresponding to `item`, we have the following
1676-
* - `pathToConstrained = ""`,
16771664
* - `tp = T1`,
16781665
* - `constraint = IFoo`,
16791666
* - `pathToTp = "A"`.
16801667
*/
16811668
pragma[nomagic]
16821669
private predicate typeParameterConstraintHasTypeParameter(
1683-
Declaration target, AccessPosition apos, TypePath pathToConstrained, TypeMention constraint,
1684-
TypePath pathToTp, TypeParameter tp
1670+
Declaration target, AccessPosition apos, TypeParameter constrainedTp,
1671+
TypeMention constraint, TypePath pathToTp, TypeParameter tp
16851672
) {
1686-
exists(TypeParameter constrainedTp |
1687-
typeParameterHasConstraint(target, apos, constrainedTp, pathToConstrained, constraint) and
1688-
tp = target.getTypeParameter(_) and
1689-
tp = constraint.getTypeAt(pathToTp) and
1690-
constrainedTp != tp
1691-
)
1673+
typeParameterHasConstraint(target, apos, constrainedTp, _, constraint) and
1674+
tp = target.getTypeParameter(_) and
1675+
tp = constraint.getTypeAt(pathToTp) and
1676+
constrainedTp != tp
16921677
}
16931678

16941679
pragma[nomagic]
16951680
private predicate typeConstraintBaseTypeMatch(
16961681
Access a, AccessEnvironment e, Declaration target, TypePath path, Type t, TypeParameter tp
16971682
) {
16981683
not exists(getTypeArgument(a, target, tp, _)) and
1699-
exists(TypeMention constraint, AccessPosition apos, TypePath pathToTp, TypePath pathToTp2 |
1700-
typeParameterConstraintHasTypeParameter(target, apos, pathToTp2, constraint, pathToTp, tp) and
1701-
AccessConstraint::satisfiesConstraint(a, e, target, apos, pathToTp2, constraint,
1684+
exists(TypeMention constraint, TypeParameter constrainedTp, TypePath pathToTp |
1685+
typeParameterConstraintHasTypeParameter(target, _, constrainedTp, constraint, pathToTp, tp) and
1686+
AccessConstraint::satisfiesConstraint(a, e, target, constrainedTp, constraint,
17021687
pathToTp.appendInverse(path), t)
17031688
)
17041689
}

0 commit comments

Comments
 (0)