Skip to content

Commit 407449f

Browse files
committed
Rust: Add metric and debug predicates for type that reach the length limit
1 parent 3bc1d47 commit 407449f

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ private module Input1 implements InputSig1<Location> {
127127
tp0 order by kind, id1, id2
128128
)
129129
}
130+
131+
int getTypePathLimit() { result = 10 }
130132
}
131133

132134
private import Input1
@@ -135,6 +137,8 @@ private module M1 = Make1<Location, Input1>;
135137

136138
private import M1
137139

140+
predicate getTypePathLimit = Input1::getTypePathLimit/0;
141+
138142
class TypePath = M1::TypePath;
139143

140144
module TypePath = M1::TypePath;
@@ -2217,8 +2221,9 @@ private module Debug {
22172221
private Locatable getRelevantLocatable() {
22182222
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
22192223
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
2220-
filepath.matches("%/sqlx.rs") and
2221-
startline = [56 .. 60]
2224+
// startline = [319 .. 326] and
2225+
startline = 325 and
2226+
filepath.matches("%/register_allocator.rs")
22222227
)
22232228
}
22242229

@@ -2227,6 +2232,11 @@ private module Debug {
22272232
result = inferType(n, path)
22282233
}
22292234

2235+
Type debugInferTypeForNodeAtLimit(AstNode n, TypePath path) {
2236+
result = inferType(n, path) and
2237+
exists(TypePath path0 | exists(inferType(n, path0)) and path0.length() = getTypePathLimit())
2238+
}
2239+
22302240
Function debugResolveCallTarget(Call c) {
22312241
c = getRelevantLocatable() and
22322242
result = resolveCallTarget(c)
@@ -2265,6 +2275,10 @@ private module Debug {
22652275
result = strictcount(Type t0 | t0 = inferType(n, path))
22662276
}
22672277

2278+
predicate countTypesForNodeAtLimit(AstNode n, int c) {
2279+
c = strictcount(Type t, TypePath path | t = debugInferTypeForNodeAtLimit(n, path))
2280+
}
2281+
22682282
predicate maxTypes(AstNode n, TypePath path, Type t, int c) {
22692283
c = countTypesAtPath(n, path, t) and
22702284
c = max(countTypesAtPath(_, _, _))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @name Nodes With Type At Length Limit
3+
* @description Counts the number of AST nodes with a type at the type path length limit.
4+
* @kind metric
5+
* @id rust/summary/nodes-at-type-path-length-limit
6+
* @tags summary
7+
*/
8+
9+
import rust
10+
import codeql.rust.internal.TypeInference
11+
12+
from int atLimit
13+
where
14+
atLimit =
15+
count(AstNode n, TypePath path |
16+
exists(inferType(n, path)) and path.length() = getTypePathLimit()
17+
|
18+
n
19+
)
20+
select atLimit

0 commit comments

Comments
 (0)