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

Using associated types in async fn type break typing #60414

Open
Ekleog opened this issue Apr 30, 2019 · 3 comments

Comments

Projects
None yet
6 participants
@Ekleog
Copy link

commented Apr 30, 2019

With the following code:

trait Trait {
    type Assoc;
}

async fn foo<T: Trait<Assoc = ()>>() -> T::Assoc {
    ()
}

comes the following error:

error[E0271]: type mismatch resolving `<impl std::future::Future as std::future::Future>::Output == <T as Trait>::Assoc`
 --> src/lib.rs:7:41
  |
7 | async fn foo<T: Trait<Assoc = ()>>() -> T::Assoc {
  |                                         ^^^^^^^^ expected (), found associated type
  |
  = note: expected type `()`
             found type `<T as Trait>::Assoc`
  = note: the return type of a function must have a statically known size

error: aborting due to previous error

(playground link)

However, this function compiles correctly without the async keyword.

@Ekleog

This comment has been minimized.

Copy link
Author

commented Apr 30, 2019

cc #50547

@cramertj

This comment has been minimized.

Copy link
Member

commented Apr 30, 2019

You can trigger this without async/await using impl Trait:

struct Foo<T>(T);

trait FooLike { type Output; }
impl<T> FooLike for Foo<T> {
    type Output = T;
}


trait Trait {
    type Assoc;
}

fn foo<T: Trait<Assoc = ()>>() -> impl FooLike<Output = T::Assoc> {
    Foo(())
}
error[E0271]: type mismatch resolving `<Foo<()> as FooLike>::Output == <T as Trait>::Assoc`
  --> src/main.rs:13:35
   |
13 | fn foo<T: Trait<Assoc = ()>>() -> impl FooLike<Output = T::Assoc> {
   |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found associated type
   |
   = note: expected type `()`
              found type `<T as Trait>::Assoc`
   = note: the return type of a function must have a statically known size

cc @oli-obk @nikomatsakis

@oli-obk

This comment has been minimized.

Copy link
Contributor

commented May 2, 2019

Some more reduction in genericity and clearing up a few things:

#![feature(impl_trait_in_bindings)]

struct Foo;

trait FooLike { type Output; }
impl FooLike for Foo {
    type Output = u32;
}

trait Trait {
    type Assoc;
}

fn foo<T: Trait<Assoc = i32>>() {
    let _: impl FooLike<Output = T::Assoc> = Foo;
}
error[E0271]: type mismatch resolving `<Foo as FooLike>::Output == <T as Trait>::Assoc`
  --> src/main.rs:15:12
   |
15 |     let _: impl FooLike<Output = T::Assoc> = Foo;
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u32, found associated type
   |
   = note: expected type `u32`
              found type `<T as Trait>::Assoc`
   = note: the return type of a function must have a statically known size

The problem is that T::Assoc = i32 is not know in impl trait items. It's also not related to the return position, but refers to any impl trait in the function. Outside of impl trait T::Assoc = i32 can be resolved without a problem, so it must be something related to the way impl trait reuses the generics of its parent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.