Rust: Path resolution for bounded type parameters

This commit is contained in:
Tom Hvitved
2025-02-20 12:57:03 +01:00
parent 8f0aced868
commit 496f485375
3 changed files with 16 additions and 1 deletions

View File

@@ -154,6 +154,13 @@ abstract class ItemNode extends AstNode {
result.(AssocItemNode).hasImplementation() and
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. */
@@ -375,6 +382,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() }

View File

@@ -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

View File

@@ -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 |