Skip to content
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

Method probe should consider where clauses on method #129669

Open
compiler-errors opened this issue Aug 27, 2024 · 2 comments
Open

Method probe should consider where clauses on method #129669

compiler-errors opened this issue Aug 27, 2024 · 2 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@compiler-errors
Copy link
Member

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 #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.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 27, 2024
@compiler-errors compiler-errors added T-types Relevant to the types team, which will review and decide on the PR/issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 27, 2024
@theemathas
Copy link
Contributor

Related issue: #127306

@theemathas
Copy link
Contributor

There's also a similar issue when checking whether there are duplicate method names.

trait SomeTrait {}
struct Thing<T>(T);
impl<T> Thing<T> where T: SomeTrait {
    fn foo(&self) {}
}
impl<T> Thing<T> {
    fn bar(&self) where T: SomeTrait {}
}
struct Dummy;
impl Thing<Dummy> {
    fn foo(&self) {} // compiles
    fn bar(&self) {} // errors
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants