Skip to content

Commit 496f485

Browse files
committed
Rust: Path resolution for bounded type parameters
1 parent 8f0aced commit 496f485

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ abstract class ItemNode extends AstNode {
154154
result.(AssocItemNode).hasImplementation() and
155155
not impl.declares(name)
156156
)
157+
or
158+
// type parameters have access to the associated items of its bounds
159+
this =
160+
any(TypeParamItemNode param |
161+
result = param.resolveABound().getASuccessorRec(name) and
162+
result instanceof AssocItemNode
163+
)
157164
}
158165

159166
/** Gets a successor named `name` of this item, if any. */
@@ -375,6 +382,13 @@ private class BlockExprItemNode extends ItemNode instanceof BlockExpr {
375382
}
376383

377384
private class TypeParamItemNode extends ItemNode instanceof TypeParam {
385+
pragma[nomagic]
386+
Path getABoundPath() {
387+
result = super.getTypeBoundList().getABound().getTypeRepr().(PathTypeRepr).getPath()
388+
}
389+
390+
ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }
391+
378392
override string getName() { result = TypeParam.super.getName().getText() }
379393

380394
override Namespace getNamespace() { result.isType() }

rust/ql/test/library-tests/path-resolution/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ mod m17 {
461461
MyTrait // $ item=I2
462462
>(x: T) { // $ item=I5
463463
x.f(); // $ MISSING: item=I1
464-
T::f(&x); // $ MISSING: item=I1
464+
T::f(&x); // $ item=I1
465465
MyTrait::f(&x); // $ item=I1
466466
} // I6
467467

rust/ql/test/library-tests/path-resolution/path-resolution.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ resolvePath
197197
| main.rs:461:7:461:13 | MyTrait | main.rs:445:5:447:5 | trait MyTrait |
198198
| main.rs:462:10:462:10 | T | main.rs:460:10:460:10 | T |
199199
| main.rs:464:9:464:9 | T | main.rs:460:10:460:10 | T |
200+
| main.rs:464:9:464:12 | ...::f | main.rs:446:9:446:20 | fn f |
200201
| main.rs:465:9:465:15 | MyTrait | main.rs:445:5:447:5 | trait MyTrait |
201202
| main.rs:465:9:465:18 | ...::f | main.rs:446:9:446:20 | fn f |
202203
| main.rs:470:9:470:9 | g | main.rs:459:5:466:5 | fn g |

0 commit comments

Comments
 (0)