Skip to content

Commit 34f3892

Browse files
committed
Rust: Extend Self resolution logic to all item kinds
1 parent 250d15a commit 34f3892

File tree

4 files changed

+9
-17
lines changed

4 files changed

+9
-17
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,10 +1896,11 @@ private predicate isImplSelfQualifiedPath(
18961896
name = path.getText()
18971897
}
18981898

1899-
private TypeAliasItemNode resolveSelfAssocType(PathExt qualifier, PathExt path) {
1899+
private ItemNode resolveImplSelfQualified(PathExt qualifier, PathExt path, Namespace ns) {
19001900
exists(ImplItemNode impl, string name |
19011901
isImplSelfQualifiedPath(impl, qualifier, path, name) and
1902-
result = impl.getAssocItem(name)
1902+
result = impl.getAssocItem(name) and
1903+
ns = result.getNamespace()
19031904
)
19041905
}
19051906

@@ -1909,17 +1910,12 @@ private TypeAliasItemNode resolveSelfAssocType(PathExt qualifier, PathExt path)
19091910
*/
19101911
pragma[nomagic]
19111912
private ItemNode resolvePathCandQualified(PathExt qualifier, ItemNode q, PathExt path, Namespace ns) {
1912-
// Special case for `Self::AssocType`; this always refers to the associated
1913-
// type in the enclosing `impl` block, if available.
1913+
// Special case for `Self::Assoc`; this always refers to the associated
1914+
// item in the enclosing `impl` block, if available.
19141915
q = resolvePathCandQualifier(qualifier, path, _) and
1915-
ns.isType() and
1916-
result = resolveSelfAssocType(qualifier, path)
1916+
result = resolveImplSelfQualified(qualifier, path, ns)
19171917
or
1918-
(
1919-
not exists(resolveSelfAssocType(qualifier, path))
1920-
or
1921-
not ns.isType()
1922-
) and
1918+
not exists(resolveImplSelfQualified(qualifier, path, ns)) and
19231919
exists(string name, SuccessorKind kind, UseOption useOpt |
19241920
q = resolvePathCandQualifier(qualifier, path, name) and
19251921
result = getASuccessor(q, name, ns, kind, useOpt) and

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@ multipleCallTargets
44
| main.rs:369:9:371:16 | ...::f(...) |
55
| main.rs:450:9:454:16 | ...::f(...) |
66
| main.rs:455:9:459:16 | ...::f(...) |
7-
| main.rs:534:13:534:25 | ...::Assoc(...) |
8-
| main.rs:545:14:545:26 | ...::Assoc(...) |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ mod m16 {
531531

532532
fn Assoc()
533533
-> Self::Assoc { // $ item=S3i32AssocType
534-
Self::Assoc() + 1 // $ item=S3i32AssocFunc $ SPURIOUS: item=S3boolAssocFunc
534+
Self::Assoc() + 1 // $ item=S3i32AssocFunc
535535
} // S3i32AssocFunc
536536
}
537537

@@ -542,7 +542,7 @@ mod m16 {
542542

543543
fn Assoc()
544544
-> Self::Assoc { // $ item=S3boolAssocType
545-
!Self::Assoc() // $ item=S3boolAssocFunc $ SPURIOUS: item=S3i32AssocFunc
545+
!Self::Assoc() // $ item=S3boolAssocFunc
546546
} // S3boolAssocFunc
547547
}
548548
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,13 @@ resolvePath
296296
| main.rs:533:16:533:26 | ...::Assoc | main.rs:528:31:530:9 | type Assoc |
297297
| main.rs:534:13:534:16 | Self | main.rs:525:5:525:22 | struct S3 |
298298
| main.rs:534:13:534:23 | ...::Assoc | main.rs:532:9:535:9 | fn Assoc |
299-
| main.rs:534:13:534:23 | ...::Assoc | main.rs:543:9:546:9 | fn Assoc |
300299
| main.rs:539:10:539:15 | Trait5 | main.rs:508:5:512:5 | trait Trait5 |
301300
| main.rs:539:21:539:28 | S3::<...> | main.rs:525:5:525:22 | struct S3 |
302301
| main.rs:539:24:539:27 | bool | {EXTERNAL LOCATION} | struct bool |
303302
| main.rs:540:22:540:25 | bool | {EXTERNAL LOCATION} | struct bool |
304303
| main.rs:544:16:544:19 | Self | main.rs:525:5:525:22 | struct S3 |
305304
| main.rs:544:16:544:26 | ...::Assoc | main.rs:539:32:541:9 | type Assoc |
306305
| main.rs:545:14:545:17 | Self | main.rs:525:5:525:22 | struct S3 |
307-
| main.rs:545:14:545:24 | ...::Assoc | main.rs:532:9:535:9 | fn Assoc |
308306
| main.rs:545:14:545:24 | ...::Assoc | main.rs:543:9:546:9 | fn Assoc |
309307
| main.rs:562:14:562:16 | Foo | main.rs:552:9:554:9 | trait Foo |
310308
| main.rs:562:22:562:22 | X | main.rs:560:9:560:21 | struct X |

0 commit comments

Comments
 (0)