Skip to content

Rust: Disambiguate more method calls based on argument types #19927

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

Merged
merged 2 commits into from
Jul 2, 2025
Merged
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
10 changes: 9 additions & 1 deletion rust/ql/lib/codeql/rust/internal/TypeInference.qll
Original file line number Diff line number Diff line change
Expand Up @@ -1220,9 +1220,17 @@ private Function getTypeParameterMethod(TypeParameter tp, string name) {
result = getMethodSuccessor(tp.(ImplTraitTypeTypeParameter).getImplTraitTypeRepr(), name)
}

pragma[nomagic]
private Type resolveNonTypeParameterTypeAt(TypeMention tm, TypePath path) {
result = tm.resolveTypeAt(path) and
not result instanceof TypeParameter
}

bindingset[t1, t2]
private predicate typeMentionEqual(TypeMention t1, TypeMention t2) {
forex(TypePath path, Type type | t1.resolveTypeAt(path) = type | t2.resolveTypeAt(path) = type)
forex(TypePath path, Type type | resolveNonTypeParameterTypeAt(t1, path) = type |
resolveNonTypeParameterTypeAt(t2, path) = type
)
}

pragma[nomagic]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
multipleCallTargets
| dereference.rs:61:15:61:24 | e1.deref() |
| main.rs:2032:13:2032:31 | ...::from(...) |
| main.rs:2033:13:2033:31 | ...::from(...) |
| main.rs:2034:13:2034:31 | ...::from(...) |
| main.rs:2040:13:2040:31 | ...::from(...) |
| main.rs:2041:13:2041:31 | ...::from(...) |
| main.rs:2042:13:2042:31 | ...::from(...) |
| main.rs:2078:21:2078:43 | ...::from(...) |
| main.rs:2076:13:2076:31 | ...::from(...) |
| main.rs:2077:13:2077:31 | ...::from(...) |
| main.rs:2078:13:2078:31 | ...::from(...) |
| main.rs:2084:13:2084:31 | ...::from(...) |
| main.rs:2085:13:2085:31 | ...::from(...) |
| main.rs:2086:13:2086:31 | ...::from(...) |
| main.rs:2122:21:2122:43 | ...::from(...) |
56 changes: 50 additions & 6 deletions rust/ql/test/library-tests/type-inference/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,6 @@ mod method_supertraits {
}

mod function_trait_bounds_2 {
use std::convert::From;
use std::fmt::Debug;

#[derive(Debug)]
Expand Down Expand Up @@ -1957,36 +1956,81 @@ mod macros {
}

mod method_determined_by_argument_type {
trait MyAdd<T> {
fn my_add(&self, value: T) -> Self;
trait MyAdd<Rhs = Self> {
type Output;

// MyAdd::my_add
fn my_add(self, rhs: Rhs) -> Self::Output;
}

impl MyAdd<i64> for i64 {
type Output = i64;

// MyAdd<i64>::my_add
fn my_add(&self, value: i64) -> Self {
fn my_add(self, value: i64) -> Self {
value
}
}

impl MyAdd<&i64> for i64 {
type Output = i64;

// MyAdd<&i64>::my_add
fn my_add(&self, value: &i64) -> Self {
fn my_add(self, value: &i64) -> Self {
*value // $ method=deref
}
}

impl MyAdd<bool> for i64 {
type Output = i64;

// MyAdd<bool>::my_add
fn my_add(&self, value: bool) -> Self {
fn my_add(self, value: bool) -> Self {
if value { 1 } else { 0 }
}
}

struct S<T>(T);

impl<T: MyAdd> MyAdd for S<T> {
type Output = S<T::Output>;

// S::my_add1
fn my_add(self, other: Self) -> Self::Output {
S((self.0).my_add(other.0)) // $ method=MyAdd::my_add $ fieldof=S
}
}

impl<T: MyAdd> MyAdd<T> for S<T> {
type Output = S<T::Output>;

// S::my_add2
fn my_add(self, other: T) -> Self::Output {
S((self.0).my_add(other)) // $ method=MyAdd::my_add $ fieldof=S
}
}

impl<'a, T> MyAdd<&'a T> for S<T>
where
T: MyAdd<&'a T>,
{
type Output = S<<T as MyAdd<&'a T>>::Output>;

// S::my_add3
fn my_add(self, other: &'a T) -> Self::Output {
S((self.0).my_add(other)) // $ method=MyAdd::my_add $ fieldof=S
}
}

pub fn f() {
let x: i64 = 73;
x.my_add(5i64); // $ method=MyAdd<i64>::my_add
x.my_add(&5i64); // $ method=MyAdd<&i64>::my_add
x.my_add(true); // $ method=MyAdd<bool>::my_add

S(1i64).my_add(S(2i64)); // $ method=S::my_add1
S(1i64).my_add(3i64); // $ MISSING: method=S::my_add2
S(1i64).my_add(&3i64); // $ method=S::my_add3
}
}

Expand Down
Loading