Skip to content

Commit

Permalink
Rust: Path resolution for bounded type parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
hvitved committed Feb 20, 2025
1 parent 8a518bf commit b6c3a1b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions rust/ql/lib/codeql/rust/elements/internal/PathResolution.qll
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ abstract class ItemNode extends AstNode {
not impl.declares(name)
)
)
or
// type parameters have access to the associated items of its bounds
this =
any(TypeParamItemNode param |
result = param.resolveABound().getASuccessorRec(name) and
result instanceof AssocItemNode
)
}

/** Gets a successor named `name` of this item, if any. */
Expand Down Expand Up @@ -377,6 +384,13 @@ private class BlockExprItemNode extends ItemNode instanceof BlockExpr {
}

private class TypeParamItemNode extends ItemNode instanceof TypeParam {
pragma[nomagic]
Path getABoundPath() {
result = super.getTypeBoundList().getABound().getTypeRepr().(PathTypeRepr).getPath()
}

ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }

override string getName() { result = TypeParam.super.getName().getText() }

override Namespace getNamespace() { result.isType() }
Expand Down
2 changes: 1 addition & 1 deletion rust/ql/test/library-tests/path-resolution/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ mod m17 {
MyTrait // $ item=I2
>(x: T) { // $ item=I5
x.f(); // $ MISSING: item=I1
T::f(&x); // $ MISSING: item=I1
T::f(&x); // $ item=I1
MyTrait::f(&x); // $ item=I1
} // I6

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ resolvePath
| main.rs:461:7:461:13 | MyTrait | main.rs:445:5:447:5 | trait MyTrait |
| main.rs:462:10:462:10 | T | main.rs:460:10:460:10 | T |
| main.rs:464:9:464:9 | T | main.rs:460:10:460:10 | T |
| main.rs:464:9:464:12 | ...::f | main.rs:446:9:446:20 | fn f |
| main.rs:465:9:465:15 | MyTrait | main.rs:445:5:447:5 | trait MyTrait |
| main.rs:465:9:465:18 | ...::f | main.rs:446:9:446:20 | fn f |
| main.rs:470:9:470:9 | g | main.rs:459:5:466:5 | fn g |
Expand Down

0 comments on commit b6c3a1b

Please sign in to comment.