Skip to content

Trait methods can't be found by method resolution if the self type involves projections #121643

Closed
@steffahn

Description

@steffahn

I tried this code:

pub type Id<T> = <T as IdTrait>::T;

pub trait IdTrait {
    type T: ?Sized;
}

impl<T: ?Sized> IdTrait for T {
    type T = T;
}

#[derive(Copy, Clone)]
struct Foo;
impl Foo {
    fn method(self: Id<Self>) {
        println!("hi!");
    }
}

trait FooTrait {
    fn method2(self: Id<Self>);
}
impl FooTrait for Foo {
    fn method2(self) { // or `: Id<Self>`, makes no difference here
        println!("hi!");
    }
}

pub fn main() {
    let foo = Foo;
    foo.method(); // works
    foo.method2(); // doesn't work
    Foo::method2(foo); // works
}

(playground)

I expected to see this happen: Code compiles successfully

Instead, this happened:

error[E0599]: no method named `method2` found for struct `Foo` in the current scope
  --> <source>:31:9
   |
12 | struct Foo;
   | ---------- method `method2` not found for this struct
...
20 |     fn method2(self: Id<Self>);
   |                      -------- the method might not be found because of this arbitrary self type
...
31 |     foo.method2(); // doesn't work
   |         ^^^^^^^
   |
help: there is a method `method` with a similar name
   |
31 |     foo.method(); // doesn't work
   |         ~~~~~~

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.
Compiler returned: 1

This is technically an old regression in 1.22, the code compiled fine up to Rust 1.21.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyA-trait-systemArea: Trait systemA-type-systemArea: Type systemC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions