Skip to content

Commit 13861b8

Browse files
committed
Address review comments
1 parent 3fa4ea4 commit 13861b8

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ private predicate typeEqualityLeft(AstNode n1, TypePath path1, AstNode n2, TypeP
240240
any(PrefixExpr pe |
241241
pe.getOperatorName() = "*" and
242242
pe.getExpr() = n1 and
243-
path1 = TypePath::consInverse(TRefTypeParameter(), path2)
243+
path1.isCons(TRefTypeParameter(), path2)
244244
)
245245
}
246246

@@ -926,7 +926,7 @@ private Type inferRefExprType(Expr e, TypePath path) {
926926
e = re.getExpr() and
927927
exists(TypePath exprPath, TypePath refPath, Type exprType |
928928
result = inferType(re, exprPath) and
929-
exprPath = TypePath::consInverse(TRefTypeParameter(), refPath) and
929+
exprPath.isCons(TRefTypeParameter(), refPath) and
930930
exprType = inferType(e)
931931
|
932932
if exprType = TRefType()
@@ -940,8 +940,9 @@ private Type inferRefExprType(Expr e, TypePath path) {
940940

941941
pragma[nomagic]
942942
private Type inferTryExprType(TryExpr te, TypePath path) {
943-
exists(TypeParam tp |
944-
result = inferType(te.getExpr(), TypePath::consInverse(TTypeParamTypeParameter(tp), path))
943+
exists(TypeParam tp, TypePath path0 |
944+
result = inferType(te.getExpr(), path0) and
945+
path0.isCons(TTypeParamTypeParameter(tp), path)
945946
|
946947
tp = any(ResultEnum r).getGenericParamList().getGenericParam(0)
947948
or
@@ -1017,7 +1018,7 @@ private module Cached {
10171018
pragma[nomagic]
10181019
Type getTypeAt(TypePath path) {
10191020
exists(TypePath path0 | result = inferType(this, path0) |
1020-
path0 = TypePath::consInverse(TRefTypeParameter(), path)
1021+
path0.isCons(TRefTypeParameter(), path)
10211022
or
10221023
not path0.isCons(TRefTypeParameter(), _) and
10231024
not (path0.isEmpty() and result = TRefType()) and

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

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
184184
/** Gets the length of this path, assuming the length is at least 2. */
185185
bindingset[this]
186186
pragma[inline_late]
187-
private int length2() {
187+
private int lengthAtLeast2() {
188188
// Same as
189189
// `result = strictcount(this.indexOf(".")) + 1`
190190
// but performs better because it doesn't use an aggregate
@@ -200,7 +200,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
200200
else
201201
if exists(TypeParameter::decode(this))
202202
then result = 1
203-
else result = this.length2()
203+
else result = this.lengthAtLeast2()
204204
}
205205

206206
/** Gets the path obtained by appending `suffix` onto this path. */
@@ -216,7 +216,7 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
216216
(
217217
not exists(getTypePathLimit())
218218
or
219-
result.length2() <= getTypePathLimit()
219+
result.lengthAtLeast2() <= getTypePathLimit()
220220
)
221221
)
222222
}
@@ -228,22 +228,26 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
228228
* so there is no need to check the length of `result`.
229229
*/
230230
bindingset[this, result]
231-
TypePath appendInverse(TypePath suffix) {
232-
if result.isEmpty()
233-
then this.isEmpty() and suffix.isEmpty()
234-
else
235-
if this.isEmpty()
236-
then suffix = result
237-
else (
238-
result = this and suffix.isEmpty()
239-
or
240-
result = this + "." + suffix
241-
)
231+
TypePath appendInverse(TypePath suffix) { suffix = result.stripPrefix(this) }
232+
233+
/** Gets the path obtained by removing `prefix` from this path. */
234+
bindingset[this, prefix]
235+
TypePath stripPrefix(TypePath prefix) {
236+
if prefix.isEmpty()
237+
then result = this
238+
else (
239+
this = prefix and
240+
result.isEmpty()
241+
or
242+
this = prefix + "." + result
243+
)
242244
}
243245

244246
/** Holds if this path starts with `tp`, followed by `suffix`. */
245247
bindingset[this]
246-
predicate isCons(TypeParameter tp, TypePath suffix) { this = TypePath::consInverse(tp, suffix) }
248+
predicate isCons(TypeParameter tp, TypePath suffix) {
249+
suffix = this.stripPrefix(TypePath::singleton(tp))
250+
}
247251
}
248252

249253
/** Provides predicates for constructing `TypePath`s. */
@@ -260,15 +264,6 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
260264
*/
261265
bindingset[suffix]
262266
TypePath cons(TypeParameter tp, TypePath suffix) { result = singleton(tp).append(suffix) }
263-
264-
/**
265-
* Gets the type path obtained by appending the singleton type path `tp`
266-
* onto `suffix`.
267-
*/
268-
bindingset[result]
269-
TypePath consInverse(TypeParameter tp, TypePath suffix) {
270-
result = singleton(tp).appendInverse(suffix)
271-
}
272267
}
273268

274269
/**

0 commit comments

Comments
 (0)