Skip to content

Fix candidate filtering in trait associated item resolution - #164762

Open
Amanieu wants to merge 2 commits into
rust-lang:mainfrom
Amanieu:fix-assoc-candidate-filtering
Open

Amanieu wants to merge 2 commits into
rust-lang:mainfrom
Amanieu:fix-assoc-candidate-filtering

Conversation

@Amanieu

@Amanieu Amanieu commented Oct 22, 2026

Copy link
Copy Markdown
Member

This PR fixes the associated item resolution logic for both value-level and type-level resolution. In both cases, this starts from a set of possible candidates matching the item name, which we then attempt to filter down to a single candidate.

Value-level resolution

pick_method calls consider_candidates twice: first for "inherent candidates" and then for "extension candidates". The second set is only considered if consider_candidates returns no candidates for the first set.

Previously the logic for consider_candidates was:

  1. If all candidates are from the same trait (but potentially with different generic arguments, e.g. Add<u8> + Add<u16>) then pick one and return that. Only the trait is needed, type inference will later fill in the generics. If one candidate is ambiguously imported from a glob import, that candidate is returned for the ambiguous trait import lint.
  2. Filter out any candidates that are unstable (and whose feature gate isn't enabled).
  3. Apply supertrait shadowing: if all candidates are supertraits of one candidate then return that candidate. In the case of multiple subtrait candidates the first one is returned.

This had several bugs:

  • Stability checking after same-trait collapse resulted in unstable inherent candidates being preferred over stable extension candidates (unstable-same-trait.rs).
  • Supertrait shadowing not preferring the glob import candidate resulted in missed ambiguous import lints. (ambiguous-glob-imported-subtrait.rs)

The new logic fixes both issues:

  1. Filter out unavailable unstable candidates first.
  2. Apply supertrait shadowing, but only filter out supertrait candidates. Keep multiple subtrait candidates.
  3. Apply same-trait collapse last: this ensures that if an ambiguously imported candidate exists, that one is returned for the ambiguous trait import lint.

Type-level resolution

Type-level resolution is much simpler: if there are multiple candidates, then supertrait shadowing is used to select a subtrait candidate if it shadows all other candidates. But if there were multiple subtrait candidates with different generic arguments, it would return the first one instead of emitting an ambiguity error. (ambiguous-instantiations.rs)

The new logic fixes this by making supertrait shadowing preserve multiple instantiations of the same trait. If multiple candidates remain then this results in an ambiguity error.

unique_subtrait helper

The logic for supertrait shadowing has been moved to a shared helper and drastically simplified.

r? lcnr

@rustbot

rustbot commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

HIR ty lowering was modified

cc @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 22, 2026
@rust-log-analyzer

This comment has been minimized.

@Amanieu
Amanieu force-pushed the fix-assoc-candidate-filtering branch from 023ccc0 to 75f36b4 Compare October 22, 2026 19:54
@Amanieu Amanieu added the llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. label Oct 23, 2026
Comment on lines +16 to +22
|
LL + #![feature(unstable_value)]
|
help: add `#![feature(unstable_value)]` to the crate attributes to enable `value`
|
LL + #![feature(unstable_value)]
|

@lcnr lcnr Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why is this emitted 3 times. maybe it shouldn't xd

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That seems to be a pre-existing bug. I added a commit which deduplicates the diagnostic.

@lcnr

lcnr commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

lgtm, minor nit which might be worth fixing, otherwise r=me.

does the following start to warn because of this PR?

trait Local<T> {
    fn call_once(&self) -> T;
}

impl Local<u8> for fn() {
    fn call_once(&self) -> u8 { 8 }
}

impl Local<u16> for fn() {
    fn call_once(&self) -> u16 { 16 }
}

fn example<T: FnOnce() + Local<u8> + Local<u16>>(f: T) -> u8 {
    f.call_once()
}

fn main() {
    assert_eq!(example((|| {}) as fn()), 8);
}

@Amanieu

Amanieu commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Yes, your example now warns with:

warning: a method with this name may be added to the standard library in the future
  --> test.rs:14:7
   |
14 |     f.call_once()
   |       ^^^^^^^^^
   |
   = help: call with fully qualified syntax `Local::call_once(...)` to keep using the current method
   = warning: once this associated item is added to the standard library, the ambiguity may cause an error or change in behavior!
   = note: for more information, see issue #48919 <https://github.com/rust-lang/rust/issues/48919>
   = note: `#[warn(unstable_name_collisions)]` (part of `#[warn(future_incompatible)]`) on by default
help: add `#![feature(fn_traits)]` to the crate attributes to enable `call_once`
   |
 1 + #![feature(fn_traits)]
   |

warning: 1 warning emitted

@rustbot

This comment has been minimized.

@Amanieu
Amanieu force-pushed the fix-assoc-candidate-filtering branch from adedd2b to bc321f9 Compare November 23, 2026 17:23
@rustbot

rustbot commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

this.unstable_candidates.iter().map(|(candidate, feature)| {
(format!(" `{}`", tcx.def_path_str(candidate.item.def_id)), *feature)
}),
this.unstable_candidates

@lcnr lcnr Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

does this filtering make more sense in disabled_nightly_features? I feel like that may also help other callers 🤔

r=me either way

View changes since the review

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants