Skip to content

Rust: Apply inherent method prioritization inside type inference loop #19903

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 3 commits into from
Jul 1, 2025

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Jun 27, 2025

Inherent methods are prioritized over trait methods:

trait Trait {
    fn method(&self) -> i32;
}

struct S;

impl S {
    fn method(&self) -> i32 { // <-- inherent method
        0
    }
}

impl Trait for S {
    fn method(&self) -> i32 { // <-- trait method
        0
    }
}

fn f(S s) {
    s.method(); // <-- calls inherent method
}

Previously, this prioritization was applied after the recursive type inference loop, because it involved a negative call to the method resolution (and hence type inference) relation.

However, as the added test case shows, this may lead to an explosion in inferred types, when the type signatures of the inherent/trait methods differ. This PR hence moves the restriction into the type inference loop, but in order to avoid non-monotonic recursion, we formulate the restriction as a monotonic property: A trait method only applies when all potential inherent targets have a receiver type that it not an instance of the impl type (the non-monotonic version is: there is no receiver type that is an instance of the impl type).

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Jun 27, 2025
@hvitved hvitved force-pushed the rust/type-inference-overlap2 branch 3 times, most recently from 10a6897 to 84f3f8c Compare June 27, 2025 12:37
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Jun 30, 2025
@hvitved hvitved marked this pull request as ready for review June 30, 2025 07:20
@Copilot Copilot AI review requested due to automatic review settings June 30, 2025 07:20
@hvitved hvitved requested a review from a team as a code owner June 30, 2025 07:20
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@hvitved hvitved force-pushed the rust/type-inference-overlap2 branch from 84f3f8c to 732bbb5 Compare July 1, 2025 07:53
@hvitved hvitved force-pushed the rust/type-inference-overlap2 branch from 732bbb5 to d379688 Compare July 1, 2025 08:17
@hvitved hvitved force-pushed the rust/type-inference-overlap2 branch from d379688 to 0723391 Compare July 1, 2025 08:34
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.

I've only reviewed this in quite a shallow way as I don't know the type inference code all that well. I will review the DCA run carefully when that's complete. Are we expecting a performance improvement here???

@@ -214,7 +214,7 @@ fn test_io_stdin() -> std::io::Result<()> {
{
let mut buffer = Vec::<u8>::new();
let _bytes = std::io::stdin().read_to_end(&mut buffer)?; // $ Alert[rust/summary/taint-sources]
sink(&buffer); // $ MISSING: hasTaintFlow
sink(&buffer); // $ hasTaintFlow -- @hvitved: works in CI, but not for me locally
Copy link
Contributor

Choose a reason for hiding this comment

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

That's weird. I've written an issue to look into this, my suspicion is that when we finish updating all the models and then generalize what we can to trait models, the platform specific behaviours are going to go away.

| main.rs:490:13:490:13 | x | &T.&T.&T.&T.&T.&T.&T.&T | main.rs:397:5:398:14 | S1 |
| main.rs:490:13:490:13 | x | &T.&T.&T.&T.&T.&T.&T.&T.&T | file://:0:0:0:0 | & |
| main.rs:490:13:490:13 | x | &T.&T.&T.&T.&T.&T.&T.&T.&T | main.rs:397:5:398:14 | S1 |
| main.rs:490:17:490:18 | S1 | | file://:0:0:0:0 | & |
Copy link
Contributor

Choose a reason for hiding this comment

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

Well this certainly looks cleaner now.

@hvitved
Copy link
Contributor Author

hvitved commented Jul 1, 2025

I will review the DCA run carefully when that's complete. Are we expecting a performance improvement here???

DCA run has completed, and it looks fine. No performance improvements expected.

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.

DCA run has completed, and it looks fine.

Yeah - there is one timeout, though I can't see that it's related to these changes. Some result changes that I now suspect are wobble. Path resolution inconsistencies may have increased slightly.

@hvitved
Copy link
Contributor Author

hvitved commented Jul 1, 2025

there is one timeout, though

I think that is a spurious DCA issue.

@hvitved hvitved merged commit b813010 into github:main Jul 1, 2025
42 checks passed
@hvitved hvitved deleted the rust/type-inference-overlap2 branch July 1, 2025 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants