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

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

Open
rodrimati1992 opened this issue Nov 3, 2019 · 0 comments

Comments

@rodrimati1992
Copy link

@rodrimati1992 rodrimati1992 commented Nov 3, 2019

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.

@rodrimati1992 rodrimati1992 changed the title method resolution on impl Trait ignores generic super method without explicit type arguments Can't call method on `impl Trait` without explicitly passing generic arguments Nov 3, 2019
rodrimati1992 added a commit to rodrimati1992/structural_crates that referenced this issue Nov 22, 2019
Fixed some docs referencing default field accessibility,

Removed redundant uses of `mut move` in both `structural_alias` and the `Structural` derive macro.

Added back the examples of returning an anonymous struct from an `fn()->impl Trait_SI`,
because the new way of accessing fields in GetFieldExt doesn't hit the bug in
rust-lang/rust#66057 .

Added support for ìnitializing fields in `make_struct` with variables of the same name,
eg:`let foo=0;  make_struct!{foo}`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.