Skip to content

Commit 6599a2e

Browse files
committed
Rust: Model impl shadowing
1 parent a0c6df3 commit 6599a2e

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,31 @@ abstract class Type extends TType {
7474
abstract private class StructOrEnumType extends Type {
7575
abstract ItemNode asItemNode();
7676

77-
final override Function getMethod(string name) {
77+
pragma[nomagic]
78+
private Function getMethodCand(ImplOrTraitItemNode impl, string name) {
7879
result = this.asItemNode().getASuccessor(name) and
79-
exists(ImplOrTraitItemNode impl | result = impl.getAnAssocItem() |
80+
result = impl.getAnAssocItem() and
81+
(
8082
impl instanceof Trait
8183
or
8284
impl.(ImplItemNode).isFullyParametric()
8385
)
8486
}
8587

88+
pragma[nomagic]
89+
private Function getImplMethod(ImplOrTraitItemNode impl, string name) {
90+
result = this.getMethodCand(impl, name) and
91+
impl = any(Impl i | not i.hasTrait())
92+
}
93+
94+
final override Function getMethod(string name) {
95+
result = this.getImplMethod(_, name)
96+
or
97+
// methods from `impl` blocks shadow functions from `impl Trait` blocks
98+
result = this.getMethodCand(_, name) and
99+
not exists(this.getImplMethod(_, name))
100+
}
101+
86102
/** Gets all of the fully parametric `impl` blocks that target this type. */
87103
final override ImplMention getABaseTypeMention() {
88104
this.asItemNode() = result.resolveSelfTy() and

rust/ql/test/extractor-tests/canonical_path/CONSISTENCY/PathResolutionConsistency.expected

Lines changed: 0 additions & 3 deletions
This file was deleted.

rust/ql/test/extractor-tests/canonical_path_disabled/CONSISTENCY/PathResolutionConsistency.expected

Lines changed: 0 additions & 3 deletions
This file was deleted.

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

Lines changed: 0 additions & 3 deletions
This file was deleted.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
testFailures
2-
| main.rs:945:26:945:31 | x.m1() | Unexpected result: method=m1 |
32
inferType
43
| loop/main.rs:7:12:7:15 | SelfParam | | loop/main.rs:6:1:8:1 | Self [trait T1] |
54
| loop/main.rs:11:12:11:15 | SelfParam | | loop/main.rs:10:1:14:1 | Self [trait T2] |

0 commit comments

Comments
 (0)