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

Closed
rodrimati1992 opened this issue Nov 3, 2019 · 3 comments
Closed
Labels
A-impl-trait C-bug T-compiler T-lang

Comments

@rodrimati1992
Copy link
Contributor

@rodrimati1992 rodrimati1992 commented Nov 3, 2019

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.

@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
@jonas-schievink jonas-schievink added A-impl-trait C-bug T-compiler T-lang labels Nov 17, 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}`.
rodrimati1992 added a commit to rodrimati1992/structural_crates that referenced this issue Mar 20, 2020
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}`.
@rodrimati1992
Copy link
Contributor Author

@rodrimati1992 rodrimati1992 commented Mar 22, 2020

Here is a minimized version of this error:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=0f0ff5d67f1019a65d19114df21cd4b0

fn make()-> impl  Extend<char> + Extend<&'static str>{
    String::new()
}
fn main(){
    make().extend(Some("string"));
    make().extend(Some('c'));
}
error[E0271]: type mismatch resolving `<std::option::Option<char> as std::iter::IntoIterator>::Item == &'static str`
 --> src/main.rs:6:12
  |
6 |     make().extend(Some('c'));
  |            ^^^^^^ expected `char`, found `&str`

error: aborting due to previous error

@benjaminp
Copy link
Contributor

@benjaminp benjaminp commented Oct 21, 2020

This was fixed by #73905.

@jyn514
Copy link
Member

@jyn514 jyn514 commented Jul 7, 2021

Both the original and reduced example now compile.

@jyn514 jyn514 closed this Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait C-bug T-compiler T-lang
Projects
None yet
Development

No branches or pull requests

4 participants