Skip to content

Can't call method on impl Trait without explicitly passing generic arguments #66057

@rodrimati1992

Description

@rodrimati1992

The next comment has a minimal example(the one in this comment is a bit complex).

Old example

This code:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=d001a5aeff1523dcec587c3e6e7ea81d

trait Hello<Name> {
    fn hello(&self, _: Name){}
}

struct A;
struct B;
struct C;

impl<T0, T1> Hello<A> for (T0, T1) {}
impl<T0, T1> Hello<B> for (T0, T1) {}
impl<T0, T1> Hello<C> for (T0, T1) {}

trait Tuple2:Hello<A> + Hello<B> + Hello<C>{}

impl<This> Tuple2 for This 
where 
    This:Hello<A> + Hello<B> + Hello<C>
{}


fn returns_impl() -> impl Tuple2 {
    (0, 10)
}


fn main() {
    {
        let tup = returns_impl();
        tup.hello(A); // This is the first one that errors
        tup.hello(B); // This is the second one that errors
        tup.hello(C);
        Hello::<A>::hello(&tup,A);
        Hello::<B>::hello(&tup,B);
        Hello::<C>::hello(&tup,C);
    }
    {// It all works fine with dynamic dispatch
        let tup:&dyn Tuple2= &returns_impl();
        tup.hello(A);
        tup.hello(B);
        tup.hello(C);
        Hello::<A>::hello(tup,A);
        Hello::<B>::hello(tup,B);
        Hello::<C>::hello(tup,C);
    }
}

Causes this error:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
  --> src/main.rs:29:19
   |
29 |         tup.hello(A); // This is the first one that errors
   |                   ^ expected struct `C`, found struct `A`
   |
   = note: expected type `C`
              found type `A`

error[E0308]: mismatched types
  --> src/main.rs:30:19
   |
30 |         tup.hello(B); // This is the second one that errors
   |                   ^ expected struct `C`, found struct `B`
   |
   = note: expected type `C`
              found type `B`

error: aborting due to 2 previous errors

On both the stable (1.38) and nightly (1.40.0 2019-11-02) versions in the playground.

I expected this code to compile without errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions