Skip to content

Commit 47aa7ba

Browse files
committed
Shared, Rust: Adjust type inference predicates to better match use sites
1 parent 53e1939 commit 47aa7ba

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

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

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,11 +1229,8 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
12291229
predicate relevantAccessConstraint(
12301230
Access a, Declaration target, AccessPosition apos, TypePath path, Type constraint
12311231
) {
1232-
exists(DeclarationPosition dpos |
1233-
accessDeclarationPositionMatch(apos, dpos) and
1234-
target = a.getTarget() and
1235-
typeParameterConstraintHasTypeParameter(target, dpos, path, _, constraint, _, _)
1236-
)
1232+
target = a.getTarget() and
1233+
typeParameterConstraintHasTypeParameter(target, apos, path, constraint, _, _)
12371234
}
12381235

12391236
private newtype TRelevantAccess =
@@ -1276,12 +1273,11 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
12761273
}
12771274

12781275
predicate satisfiesConstraintType(
1279-
Access a, AccessPosition apos, TypePath prefix, Type constraint, TypePath path, Type t
1276+
Access a, Declaration target, AccessPosition apos, TypePath prefix, Type constraint,
1277+
TypePath path, Type t
12801278
) {
1281-
exists(RelevantAccess at | at = MkRelevantAccess(a, _, apos, prefix) |
1282-
SatisfiesConstraint<RelevantAccess, SatisfiesConstraintInput>::satisfiesConstraintType(at,
1283-
constraint, path, t)
1284-
)
1279+
SatisfiesConstraint<RelevantAccess, SatisfiesConstraintInput>::satisfiesConstraintType(MkRelevantAccess(a,
1280+
target, apos, prefix), constraint, path, t)
12851281
}
12861282
}
12871283

@@ -1370,37 +1366,38 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
13701366
}
13711367

13721368
/**
1373-
* Holds if `tp1` and `tp2` are distinct type parameters of `target`, the
1374-
* declared type at `dpos` mentions `tp1` at `path1`, `tp1` has a base
1375-
* type mention of type `constraint` that mentions `tp2` at the path
1376-
* `path2`.
1369+
* Holds if the declared type of `target` contains a type parameter at
1370+
* `apos` and `pathToConstrained` that must satisfy `constraint` and `tp`
1371+
* occurs at `pathToTp` in `constraint`.
13771372
*
1378-
* For this example
1373+
* For example, in
13791374
* ```csharp
13801375
* interface IFoo<A> { }
13811376
* T1 M<T1, T2>(T2 item) where T2 : IFoo<T1> { }
13821377
* ```
1383-
* with the method declaration being the target and the for the first
1384-
* parameter position, we have the following
1385-
* - `path1 = ""`,
1386-
* - `tp1 = T2`,
1378+
* with the method declaration being the target and with `apos`
1379+
* corresponding to `item`, we have the following
1380+
* - `pathToConstrained = ""`,
1381+
* - `tp = T1`,
13871382
* - `constraint = IFoo`,
1388-
* - `path2 = "A"`, and
1389-
* - `tp2 = T1`.
1383+
* - `pathToTp = "A"`.
13901384
*/
13911385
pragma[nomagic]
13921386
private predicate typeParameterConstraintHasTypeParameter(
1393-
Declaration target, DeclarationPosition dpos, TypePath path1, TypeParameter tp1,
1394-
Type constraint, TypePath path2, TypeParameter tp2
1387+
Declaration target, AccessPosition apos, TypePath pathToConstrained, Type constraint,
1388+
TypePath pathToTp, TypeParameter tp
13951389
) {
1396-
tp1 = target.getTypeParameter(_) and
1397-
tp2 = target.getTypeParameter(_) and
1398-
tp1 != tp2 and
1399-
tp1 = target.getDeclaredType(dpos, path1) and
1400-
exists(TypeMention tm |
1401-
tm = getATypeParameterConstraint(tp1) and
1402-
tm.resolveTypeAt(path2) = tp2 and
1403-
constraint = resolveTypeMentionRoot(tm)
1390+
exists(DeclarationPosition dpos, TypeParameter constrainedTp |
1391+
accessDeclarationPositionMatch(apos, dpos) and
1392+
constrainedTp = target.getTypeParameter(_) and
1393+
tp = target.getTypeParameter(_) and
1394+
constrainedTp != tp and
1395+
constrainedTp = target.getDeclaredType(dpos, pathToConstrained) and
1396+
exists(TypeMention tm |
1397+
tm = getATypeParameterConstraint(constrainedTp) and
1398+
tm.resolveTypeAt(pathToTp) = tp and
1399+
constraint = resolveTypeMentionRoot(tm)
1400+
)
14041401
)
14051402
}
14061403

@@ -1409,15 +1406,9 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
14091406
Access a, Declaration target, TypePath path, Type t, TypeParameter tp
14101407
) {
14111408
not exists(getTypeArgument(a, target, tp, _)) and
1412-
target = a.getTarget() and
1413-
exists(
1414-
Type constraint, AccessPosition apos, DeclarationPosition dpos, TypePath pathToTp,
1415-
TypePath pathToTp2
1416-
|
1417-
accessDeclarationPositionMatch(apos, dpos) and
1418-
typeParameterConstraintHasTypeParameter(target, dpos, pathToTp2, _, constraint, pathToTp,
1419-
tp) and
1420-
AccessConstraint::satisfiesConstraintType(a, apos, pathToTp2, constraint,
1409+
exists(Type constraint, AccessPosition apos, TypePath pathToTp, TypePath pathToTp2 |
1410+
typeParameterConstraintHasTypeParameter(target, apos, pathToTp2, constraint, pathToTp, tp) and
1411+
AccessConstraint::satisfiesConstraintType(a, target, apos, pathToTp2, constraint,
14211412
pathToTp.appendInverse(path), t)
14221413
)
14231414
}

0 commit comments

Comments
 (0)