rust-lang / rust-lang/rust

Method probe should consider where clauses on method

Open
#129,669 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-enhancement T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Given:

use core::ops::Deref;

struct W<T>(T);
struct X;

impl<T> Deref for W<T> {
    type Target = X;
    fn deref(&self) -> &Self::Target { &X }
}

trait A {}
trait B {}

impl<T: A> W<T> {
    fn a(&self) {} // EXAMPLE A
}
impl<T> W<T> {
    fn b(&self) where T: B {}  // EXAMPLE B
}

impl X {
    fn a(&self) {}
    fn b(&self) {}
}

fn main() {
    let w = W(());
    w.a(); // Works.
    w.b(); // Doesn't work.
}

I expected this code to work. Whether I place the where clause on the impl block (like in example A) or on the method (like in example B) should not matter.

Method probing should assemble the method's "own" where clauses so we can use them. This would've prevented the regression in https://github.com/rust-lang/rust/issues/129601, since #129449 rearranged some where clause bounds for readability.


Let's not actually fix this until the new solver has landed, since it's likely to cause spurious new overflows in practice, which are fatal. In the new solver, it should be fine 👍

We could technically support this in the old solver, if we were to filter out any predicates that mention the method's generics. But this seems to be a hack that I'd need convincing is worthwhile rather than waiting to do it the "right" way...

There's also theoretically more places for incompleteness to guide inference on the args, but I expect that to not be an issue TBH, since we already (afaict) process obligations before/while doing argument checking.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Rust reproducer and compare method probing for the impl-level bound in example A with the method-level where clause in example B. Read the new solver work first, then verify that method probing assembles the method's own clauses without causing spurious overflows; the reproducer should work in both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.