Skip to content

Commit 6e34415

Browse files
committed
Rust: Handle multiple type bounds for the same type parameter in getTypeBound
1 parent 94a6fd5 commit 6e34415

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

rust/ql/lib/codeql/rust/elements/internal/TypeParamImpl.qll

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ module Impl {
2828
/** Gets the position of this type parameter. */
2929
int getPosition() { this = any(GenericParamList l).getTypeParam(result) }
3030

31-
private int nrOfDirectTypeBounds() {
32-
result = this.getTypeBoundList().getNumberOfBounds()
33-
or
34-
not this.hasTypeBoundList() and
35-
result = 0
31+
private TypeBound getTypeBoundAt(int i, int j) {
32+
exists(TypeBoundList tbl | result = tbl.getBound(j) |
33+
tbl = this.getTypeBoundList() and i = 0
34+
or
35+
exists(WherePred wp |
36+
wp = this.(TypeParamItemNode).getAWherePred() and
37+
tbl = wp.getTypeBoundList() and
38+
wp = any(WhereClause wc).getPredicate(i)
39+
)
40+
)
3641
}
3742

3843
/**
@@ -42,12 +47,7 @@ module Impl {
4247
* any `where` clauses for this type parameter.
4348
*/
4449
TypeBound getTypeBound(int index) {
45-
exists(TypeBoundList tbl, int offset | result = tbl.getBound(index - offset) |
46-
tbl = this.getTypeBoundList() and offset = 0
47-
or
48-
tbl = this.(TypeParamItemNode).getAWherePred().getTypeBoundList() and
49-
offset = this.nrOfDirectTypeBounds()
50-
)
50+
result = rank[index + 1](int i, int j | | this.getTypeBoundAt(i, j) order by i, j)
5151
}
5252

5353
/**
@@ -56,7 +56,12 @@ module Impl {
5656
* This includes type bounds directly on this type parameter and bounds from
5757
* any `where` clauses for this type parameter.
5858
*/
59-
TypeBound getATypeBound() { result = this.getTypeBound(_) }
59+
TypeBound getATypeBound() {
60+
// NOTE: This predicate is used in path resolution, so it can not be
61+
// defined using `getTypeBound` as that would cause non-monotonic
62+
// recursion due to the `rank`.
63+
result = this.getTypeBoundAt(_, _)
64+
}
6065

6166
override string toAbbreviatedString() { result = this.getName().getText() }
6267

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -932,11 +932,7 @@ class TypeParamItemNode extends TypeItemNode instanceof TypeParam {
932932
}
933933

934934
pragma[nomagic]
935-
Path getTypeBoundPath(int index) {
936-
result = super.getTypeBound(index).getTypeRepr().(PathTypeRepr).getPath()
937-
}
938-
939-
Path getABoundPath() { result = this.getTypeBoundPath(_) }
935+
Path getABoundPath() { result = super.getATypeBound().getTypeRepr().(PathTypeRepr).getPath() }
940936

941937
pragma[nomagic]
942938
ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }

0 commit comments

Comments
 (0)