Skip to content

Commit 7e51318

Browse files
committed
Rust: Special-case Self::AssocType in path resolution
1 parent 0d46dc0 commit 7e51318

File tree

5 files changed

+12
-11
lines changed

5 files changed

+12
-11
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,16 @@ private ItemNode resolvePathCandQualified(PathExt qualifier, ItemNode q, PathExt
18971897
q = resolvePathCandQualifier(qualifier, path, name) and
18981898
result = getASuccessor(q, name, ns, kind, useOpt) and
18991899
checkQualifiedVisibility(path, result, kind, useOpt)
1900+
|
1901+
// Special case for `Self::AssocType`; this always refers to the associated
1902+
// type in the enclosing `impl` block, if available.
1903+
forall(ImplItemNode impl, TypeAliasItemNode alias |
1904+
qualifier = impl.getASelfPath() and alias = result
1905+
|
1906+
alias = impl.getAnAssocItem()
1907+
or
1908+
not exists(impl.getAssocItem(name).(TypeAliasItemNode))
1909+
)
19001910
)
19011911
}
19021912

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +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-
multiplePathResolutions
8-
| main.rs:492:20:492:34 | ...::AssocType |
9-
| main.rs:504:20:504:34 | ...::AssocType |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ mod m16 {
489489
; // S2Trait3AssocType
490490

491491
fn f(&self) {
492-
let x: Self::AssocType = 42; // $ item=S2Trait3AssocType $ SPURIOUS: item=S2Trait4AssocType
492+
let x: Self::AssocType = 42; // $ item=S2Trait3AssocType
493493
} // S2asTrait3::f
494494
}
495495

@@ -501,7 +501,7 @@ mod m16 {
501501
fn g(&self) {
502502
Self::f(&self); // $ item=S2asTrait3::f
503503
S2::f(&self); // $ item=S2asTrait3::f
504-
let x: Self::AssocType = true; // $ item=S2Trait4AssocType $ SPURIOUS: item=S2Trait3AssocType
504+
let x: Self::AssocType = true; // $ item=S2Trait4AssocType
505505
}
506506
}
507507
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ resolvePath
269269
| main.rs:488:26:488:28 | i32 | {EXTERNAL LOCATION} | struct i32 |
270270
| main.rs:492:20:492:23 | Self | main.rs:484:5:484:14 | struct S2 |
271271
| main.rs:492:20:492:34 | ...::AssocType | main.rs:487:26:489:9 | type AssocType |
272-
| main.rs:492:20:492:34 | ...::AssocType | main.rs:497:26:499:9 | type AssocType |
273272
| main.rs:497:10:497:15 | Trait4 | main.rs:478:5:482:5 | trait Trait4 |
274273
| main.rs:497:21:497:22 | S2 | main.rs:484:5:484:14 | struct S2 |
275274
| main.rs:498:26:498:29 | bool | {EXTERNAL LOCATION} | struct bool |
@@ -278,7 +277,6 @@ resolvePath
278277
| main.rs:503:13:503:14 | S2 | main.rs:484:5:484:14 | struct S2 |
279278
| main.rs:503:13:503:17 | ...::f | main.rs:489:11:493:9 | fn f |
280279
| main.rs:504:20:504:23 | Self | main.rs:484:5:484:14 | struct S2 |
281-
| main.rs:504:20:504:34 | ...::AssocType | main.rs:487:26:489:9 | type AssocType |
282280
| main.rs:504:20:504:34 | ...::AssocType | main.rs:497:26:499:9 | type AssocType |
283281
| main.rs:521:14:521:16 | Foo | main.rs:511:9:513:9 | trait Foo |
284282
| main.rs:521:22:521:22 | X | main.rs:519:9:519:21 | struct X |

rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,3 @@ multipleCallTargets
1111
| main.rs:2642:13:2642:31 | ...::from(...) |
1212
| main.rs:2643:13:2643:31 | ...::from(...) |
1313
| main.rs:2644:13:2644:31 | ...::from(...) |
14-
multiplePathResolutions
15-
| main.rs:2463:41:2463:52 | ...::Output |
16-
| main.rs:2472:38:2472:49 | ...::Output |
17-
| main.rs:2484:42:2484:53 | ...::Output |

0 commit comments

Comments
 (0)