Skip to content

Commit e0f16cf

Browse files
committed
dbg2
1 parent 0b1b88c commit e0f16cf

File tree

2 files changed

+111
-9
lines changed

2 files changed

+111
-9
lines changed

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

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ private TypeMention getTypeAnnotation(AstNode n) {
210210
)
211211
}
212212

213+
private predicate hasKnownType(AstNode n) {
214+
none()
215+
// exists(TypeMention tm |
216+
// tm = getTypeAnnotation(n) and
217+
// not tm = any(InferTypeRepr itr).getParentNode*()
218+
// )
219+
// or
220+
// n instanceof SelfParam
221+
// or
222+
// n.getLocation().getStartLine() in [1495 .. 1499] and
223+
// n.getFile().getBaseName() = "ordered_float.rs"
224+
}
225+
213226
/** Gets the type of `n`, which has an explicit type annotation. */
214227
pragma[nomagic]
215228
private Type inferAnnotatedType(AstNode n, TypePath path) {
@@ -302,7 +315,8 @@ pragma[nomagic]
302315
private Type inferTypeEquality(AstNode n, TypePath path) {
303316
exists(TypePath prefix1, AstNode n2, TypePath prefix2, TypePath suffix |
304317
result = inferType(n2, prefix2.appendInverse(suffix)) and
305-
path = prefix1.append(suffix)
318+
path = prefix1.append(suffix) and
319+
not hasKnownType(n)
306320
|
307321
typeEquality(n, prefix1, n2, prefix2)
308322
or
@@ -482,7 +496,8 @@ pragma[nomagic]
482496
private Type inferStructExprType(AstNode n, TypePath path) {
483497
exists(StructExprMatchingInput::Access a, StructExprMatchingInput::AccessPosition apos |
484498
n = a.getNodeAt(apos) and
485-
result = StructExprMatching::inferAccessType(a, apos, path)
499+
result = StructExprMatching::inferAccessType(a, apos, path) and
500+
not hasKnownType(n)
486501
)
487502
}
488503

@@ -526,12 +541,25 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
526541
}
527542
}
528543

544+
private Locatable getRelevantLocatable() {
545+
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
546+
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
547+
filepath.matches("%/common/hashtable/src/hashtable.rs") and
548+
startline = [128, 156]
549+
)
550+
}
551+
529552
abstract class Declaration extends AstNode {
530553
abstract TypeParameter getTypeParameter(TypeParameterPosition ppos);
531554

532555
pragma[nomagic]
533556
abstract Type getParameterType(DeclarationPosition dpos, TypePath path);
534557

558+
private Type dbggetDeclaredType(DeclarationPosition dpos, TypePath path) {
559+
this = getRelevantLocatable() and
560+
result = this.getDeclaredType(dpos, path)
561+
}
562+
535563
abstract Type getReturnType(TypePath path);
536564

537565
Type getDeclaredType(DeclarationPosition dpos, TypePath path) {
@@ -749,6 +777,13 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
749777
or
750778
result = CallExprImpl::getResolvedFunction(this)
751779
}
780+
781+
private Declaration dbggetTarget() {
782+
this = getRelevantLocatable() and
783+
result = resolveMethodCallTarget(this) // mutual recursion; resolving method calls requires resolving types and vice versa
784+
// or
785+
// result = CallExprImpl::getResolvedFunction(this)
786+
}
752787
}
753788

754789
predicate accessDeclarationPositionMatch(AccessPosition apos, DeclarationPosition dpos) {
@@ -826,7 +861,8 @@ private Type inferCallExprBaseType(AstNode n, TypePath path) {
826861
TypePath path0
827862
|
828863
n = a.getNodeAt(apos) and
829-
result = CallExprBaseMatching::inferAccessType(a, apos, path0)
864+
result = CallExprBaseMatching::inferAccessType(a, apos, path0) and
865+
not hasKnownType(n)
830866
|
831867
if
832868
apos.isBorrowed(true)
@@ -987,7 +1023,8 @@ private Type inferFieldExprType(AstNode n, TypePath path) {
9871023
FieldExprMatchingInput::Access a, FieldExprMatchingInput::AccessPosition apos, TypePath path0
9881024
|
9891025
n = a.getNodeAt(apos) and
990-
result = FieldExprMatching::inferAccessType(a, apos, path0)
1026+
result = FieldExprMatching::inferAccessType(a, apos, path0) and
1027+
not hasKnownType(n)
9911028
|
9921029
if apos.isSelf()
9931030
then
@@ -1592,8 +1629,8 @@ private module Debug {
15921629
private Locatable getRelevantLocatable() {
15931630
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
15941631
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
1595-
filepath.matches("%/sqlx.rs") and
1596-
startline = [56 .. 60]
1632+
filepath.matches("%/expression/src/utils/arithmetics_type.rs") and
1633+
startline = 1049
15971634
)
15981635
}
15991636

@@ -1607,19 +1644,52 @@ private module Debug {
16071644
result = resolveMethodCallTarget(mce)
16081645
}
16091646

1647+
predicate debugInferImplicitSelfType(SelfParam self, TypePath path, Type t) {
1648+
self = getRelevantLocatable() and
1649+
t = inferImplicitSelfType(self, path)
1650+
}
1651+
1652+
predicate debugInferCallExprBaseType(AstNode n, TypePath path, Type t) {
1653+
n = getRelevantLocatable() and
1654+
t = inferCallExprBaseType(n, path)
1655+
}
1656+
16101657
predicate debugTypeMention(TypeMention tm, TypePath path, Type type) {
16111658
tm = getRelevantLocatable() and
16121659
tm.resolveTypeAt(path) = type
16131660
}
16141661

16151662
pragma[nomagic]
1616-
private int countTypes(AstNode n, TypePath path, Type t) {
1663+
private int countTypesAtPath(AstNode n, TypePath path, Type t) {
16171664
t = inferType(n, path) and
16181665
result = strictcount(Type t0 | t0 = inferType(n, path))
16191666
}
16201667

16211668
predicate maxTypes(AstNode n, TypePath path, Type t, int c) {
1622-
c = countTypes(n, path, t) and
1623-
c = max(countTypes(_, _, _))
1669+
c = countTypesAtPath(n, path, t) and
1670+
c = max(countTypesAtPath(_, _, _))
1671+
}
1672+
1673+
pragma[nomagic]
1674+
private predicate typePathLength(AstNode n, TypePath path, Type t, int len) {
1675+
t = inferType(n, path) and
1676+
len = path.length() and
1677+
not n.getFile().getBaseName() = "ordered_float.rs"
1678+
}
1679+
1680+
predicate maxTypePath(AstNode n, TypePath path, Type t, int len) {
1681+
typePathLength(n, path, t, len) and
1682+
len = max(int i | typePathLength(_, _, _, i))
1683+
}
1684+
1685+
pragma[nomagic]
1686+
private int countTypePaths(AstNode n, TypePath path, Type t) {
1687+
t = inferType(n, path) and
1688+
result = strictcount(TypePath path0, Type t0 | t0 = inferType(n, path0))
1689+
}
1690+
1691+
predicate maxTypePaths(AstNode n, TypePath path, Type t, int c) {
1692+
c = countTypePaths(n, path, t) and
1693+
c = max(countTypePaths(_, _, _))
16241694
}
16251695
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,16 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
14401440
typeConstraintBaseTypeMatch(a, target, path, t, tp)
14411441
}
14421442

1443+
private predicate debugtypeMatch(
1444+
Access a, Declaration target, TypePath path, Type t, TypeParameter tp
1445+
) {
1446+
typeMatch(a, target, path, t, tp) and
1447+
a.getLocation()
1448+
.hasLocationInfo(any(string filePath |
1449+
filePath.matches("%/common/hashtable/src/hashtable.rs")
1450+
), 156, _, _, _)
1451+
}
1452+
14431453
/**
14441454
* Gets the inferred type of `a` at `path` for position `apos`.
14451455
*
@@ -1496,6 +1506,28 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
14961506
)
14971507
)
14981508
}
1509+
1510+
private Type debuginferAccessType(
1511+
Access a, DeclarationPosition dpos, Declaration target, AccessPosition apos, TypePath path
1512+
) {
1513+
// result = inferAccessType(a, apos, path) and
1514+
accessDeclarationPositionMatch(apos, dpos) and
1515+
// A suffix of `path` leads to a type parameter in the target
1516+
// exists(Declaration target, TypePath prefix, TypeParameter tp, TypePath suffix |
1517+
// tp = target.getDeclaredType(dpos, prefix) and
1518+
// path = prefix.append(suffix) and
1519+
// typeMatch(a, target, suffix, result, tp)
1520+
// )
1521+
// or
1522+
// `path` corresponds directly to a concrete type in the declaration
1523+
result = target.getDeclaredType(dpos, path) and
1524+
target = a.getTarget() and
1525+
not result instanceof TypeParameter and
1526+
a.getLocation()
1527+
.hasLocationInfo(any(string filePath |
1528+
filePath.matches("%/common/hashtable/src/hashtable.rs")
1529+
), 156, _, _, _)
1530+
}
14991531
}
15001532

15011533
/** Provides consistency checks. */

0 commit comments

Comments
 (0)