Skip to content

Rust: Disambiguate associated function calls #19995

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 4 commits into from
Jul 10, 2025

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Jul 7, 2025

This PR implements type-based disambiguation of calls to associated functions.

Before this PR, whenever we saw a call to an associated function like

Foo::bar(baz)

we would resolve bar to whatever our path resolution logic did. However, this could result in two kinds of ambiguities that this PR resolves.

Overlapping trait implementations

trait MyFrom<T> {
    fn my_from(value: T) -> Self;
}

impl MyFrom<i64> for i64 {
    fn my_from(value: i64) -> Self {
        value
    }
}

impl MyFrom<bool> for i64 {
    fn my_from(value: bool) -> Self {
        if value { 1 } else { 0 }
    }
}

In a call like i64::my_from(73i64), path resolution resolves my_from to both implementations, and type information about the argument 73i64 is required to disambiguate. This is an extension of #19749.

Calls to trait functions

Using the example above, in a call like MyFrom::my_from(73i64), path resolution resolves my_from to the trait function. We then need consider all possible implementations of my_from, and pick the one(s) where the argument types match the parameter types.

Another typical instance of this is calls to Default::default(), where we have no arguments to determine the relevant call target. Instead, we need to use type information about the context in which the call occurs, e.g. in let x : i64 = Default::default() we know that the return type should be i64.

DCA looks fine, though the percentage of resolvable calls drops from 59.6 % to 56.5 %, which is more than I had anticipated. Given that this PR unblocks other work, we can investigate the missing call targets follow-up.

@github-actions github-actions bot added Ruby Rust Pull requests that update Rust code labels Jul 7, 2025
@hvitved hvitved force-pushed the rust/disambiguate-assoc-function-calls branch from fc8ff54 to 0aaedf5 Compare July 8, 2025 09:06
@github-actions github-actions bot removed the Ruby label Jul 8, 2025
@hvitved hvitved force-pushed the rust/disambiguate-assoc-function-calls branch 5 times, most recently from c2ad6f9 to 0ef372f Compare July 9, 2025 09:32
@hvitved hvitved force-pushed the rust/disambiguate-assoc-function-calls branch 2 times, most recently from a36613d to 67ecb66 Compare July 9, 2025 17:45
@hvitved hvitved force-pushed the rust/disambiguate-assoc-function-calls branch 3 times, most recently from 7fa76b7 to 604fda8 Compare July 10, 2025 10:48
@hvitved hvitved force-pushed the rust/disambiguate-assoc-function-calls branch from 604fda8 to c7d20eb Compare July 10, 2025 11:52
@hvitved hvitved marked this pull request as ready for review July 10, 2025 12:47
@hvitved hvitved requested a review from a team as a code owner July 10, 2025 12:47
Copy link
Contributor

@redsun82 redsun82 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems plausible. I understand that the new missing results are left for follow up, right?

@hvitved
Copy link
Contributor Author

hvitved commented Jul 10, 2025

I understand that the new missing results are left for follow up, right?

Yes.

Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to tests and test inconsistencies look great! Changes on DCA look fine as well. I haven't checked the QL in detail but the intent sounds very reasonable.

@hvitved hvitved merged commit 1e9520c into github:main Jul 10, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy