Skip to content

Rust: Model impl shadowing #19392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions rust/ql/lib/codeql/rust/internal/Type.qll
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,31 @@ abstract class Type extends TType {
abstract private class StructOrEnumType extends Type {
abstract ItemNode asItemNode();

final override Function getMethod(string name) {
pragma[nomagic]
private Function getMethodCand(ImplOrTraitItemNode impl, string name) {
result = this.asItemNode().getASuccessor(name) and
exists(ImplOrTraitItemNode impl | result = impl.getAnAssocItem() |
result = impl.getAnAssocItem() and
(
impl instanceof Trait
or
impl.(ImplItemNode).isFullyParametric()
)
}

pragma[nomagic]
private Function getImplMethod(ImplOrTraitItemNode impl, string name) {
result = this.getMethodCand(impl, name) and
impl = any(Impl i | not i.hasTrait())
}

final override Function getMethod(string name) {
result = this.getImplMethod(_, name)
or
// methods from `impl` blocks shadow functions from `impl Trait` blocks
result = this.getMethodCand(_, name) and
not exists(this.getImplMethod(_, name))
}

/** Gets all of the fully parametric `impl` blocks that target this type. */
final override ImplMention getABaseTypeMention() {
this.asItemNode() = result.resolveSelfTy() and
Expand Down

This file was deleted.

This file was deleted.

28 changes: 28 additions & 0 deletions rust/ql/test/library-tests/type-inference/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,33 @@ mod borrowed_typed {
}
}

mod impl_overlap {
#[derive(Debug)]
struct S1;

trait MyTrait {
fn m1(self) -> S1;
}

impl MyTrait for S1 {
fn m1(self) -> S1 {
panic!("not called");
}
}

#[rustfmt::skip]
impl S1 {
fn m1(self) -> S1 { // target
self
}
}

pub fn f() {
let x = S1;
println!("{:?}", x.m1()); // $ method=target
}
}

fn main() {
field_access::f();
method_impl::f();
Expand All @@ -935,4 +962,5 @@ fn main() {
trait_implicit_self_borrow::f();
implicit_self_borrow::f();
borrowed_typed::f();
impl_overlap::f();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,17 @@ inferType
| main.rs:918:15:918:16 | &x | | file://:0:0:0:0 | & |
| main.rs:918:15:918:16 | &x | &T | main.rs:894:5:894:13 | S |
| main.rs:918:16:918:16 | x | | main.rs:894:5:894:13 | S |
| main.rs:924:5:924:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo |
| main.rs:925:5:925:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo |
| main.rs:925:20:925:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
| main.rs:925:41:925:59 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
| main.rs:927:15:927:18 | SelfParam | | main.rs:926:5:928:5 | Self [trait MyTrait] |
| main.rs:931:15:931:18 | SelfParam | | main.rs:923:5:924:14 | S1 |
| main.rs:931:27:933:9 | { ... } | | main.rs:923:5:924:14 | S1 |
| main.rs:938:15:938:18 | SelfParam | | main.rs:923:5:924:14 | S1 |
| main.rs:938:27:940:9 | { ... } | | main.rs:923:5:924:14 | S1 |
| main.rs:939:13:939:16 | self | | main.rs:923:5:924:14 | S1 |
| main.rs:944:13:944:13 | x | | main.rs:923:5:924:14 | S1 |
| main.rs:944:17:944:18 | S1 | | main.rs:923:5:924:14 | S1 |
| main.rs:945:26:945:26 | x | | main.rs:923:5:924:14 | S1 |
| main.rs:945:26:945:31 | x.m1() | | main.rs:923:5:924:14 | S1 |
| main.rs:951:5:951:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo |
| main.rs:952:5:952:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo |
| main.rs:952:20:952:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
| main.rs:952:41:952:59 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |