Rust: Extend Self resolution logic to all item kinds

This commit is contained in:
Tom Hvitved
2025-12-01 09:58:27 +01:00
parent 250d15aef2
commit 34f3892c35
4 changed files with 9 additions and 17 deletions

View File

@@ -1896,10 +1896,11 @@ private predicate isImplSelfQualifiedPath(
name = path.getText()
}
private TypeAliasItemNode resolveSelfAssocType(PathExt qualifier, PathExt path) {
private ItemNode resolveImplSelfQualified(PathExt qualifier, PathExt path, Namespace ns) {
exists(ImplItemNode impl, string name |
isImplSelfQualifiedPath(impl, qualifier, path, name) and
result = impl.getAssocItem(name)
result = impl.getAssocItem(name) and
ns = result.getNamespace()
)
}
@@ -1909,17 +1910,12 @@ private TypeAliasItemNode resolveSelfAssocType(PathExt qualifier, PathExt path)
*/
pragma[nomagic]
private ItemNode resolvePathCandQualified(PathExt qualifier, ItemNode q, PathExt path, Namespace ns) {
// Special case for `Self::AssocType`; this always refers to the associated
// type in the enclosing `impl` block, if available.
// Special case for `Self::Assoc`; this always refers to the associated
// item in the enclosing `impl` block, if available.
q = resolvePathCandQualifier(qualifier, path, _) and
ns.isType() and
result = resolveSelfAssocType(qualifier, path)
result = resolveImplSelfQualified(qualifier, path, ns)
or
(
not exists(resolveSelfAssocType(qualifier, path))
or
not ns.isType()
) and
not exists(resolveImplSelfQualified(qualifier, path, ns)) and
exists(string name, SuccessorKind kind, UseOption useOpt |
q = resolvePathCandQualifier(qualifier, path, name) and
result = getASuccessor(q, name, ns, kind, useOpt) and

View File

@@ -4,5 +4,3 @@ multipleCallTargets
| main.rs:369:9:371:16 | ...::f(...) |
| main.rs:450:9:454:16 | ...::f(...) |
| main.rs:455:9:459:16 | ...::f(...) |
| main.rs:534:13:534:25 | ...::Assoc(...) |
| main.rs:545:14:545:26 | ...::Assoc(...) |

View File

@@ -531,7 +531,7 @@ mod m16 {
fn Assoc()
-> Self::Assoc { // $ item=S3i32AssocType
Self::Assoc() + 1 // $ item=S3i32AssocFunc $ SPURIOUS: item=S3boolAssocFunc
Self::Assoc() + 1 // $ item=S3i32AssocFunc
} // S3i32AssocFunc
}
@@ -542,7 +542,7 @@ mod m16 {
fn Assoc()
-> Self::Assoc { // $ item=S3boolAssocType
!Self::Assoc() // $ item=S3boolAssocFunc $ SPURIOUS: item=S3i32AssocFunc
!Self::Assoc() // $ item=S3boolAssocFunc
} // S3boolAssocFunc
}
}

View File

@@ -296,7 +296,6 @@ resolvePath
| main.rs:533:16:533:26 | ...::Assoc | main.rs:528:31:530:9 | type Assoc |
| main.rs:534:13:534:16 | Self | main.rs:525:5:525:22 | struct S3 |
| main.rs:534:13:534:23 | ...::Assoc | main.rs:532:9:535:9 | fn Assoc |
| main.rs:534:13:534:23 | ...::Assoc | main.rs:543:9:546:9 | fn Assoc |
| main.rs:539:10:539:15 | Trait5 | main.rs:508:5:512:5 | trait Trait5 |
| main.rs:539:21:539:28 | S3::<...> | main.rs:525:5:525:22 | struct S3 |
| main.rs:539:24:539:27 | bool | {EXTERNAL LOCATION} | struct bool |
@@ -304,7 +303,6 @@ resolvePath
| main.rs:544:16:544:19 | Self | main.rs:525:5:525:22 | struct S3 |
| main.rs:544:16:544:26 | ...::Assoc | main.rs:539:32:541:9 | type Assoc |
| main.rs:545:14:545:17 | Self | main.rs:525:5:525:22 | struct S3 |
| main.rs:545:14:545:24 | ...::Assoc | main.rs:532:9:535:9 | fn Assoc |
| main.rs:545:14:545:24 | ...::Assoc | main.rs:543:9:546:9 | fn Assoc |
| main.rs:562:14:562:16 | Foo | main.rs:552:9:554:9 | trait Foo |
| main.rs:562:22:562:22 | X | main.rs:560:9:560:21 | struct X |