Skip to content

GAT & TAIT with lifetime type parameters fail std::marker::Sized requirement #89008

Closed
@rustrial

Description

@rustrial

Playing around with (the not yet stable) GAT and TAIT features I stumbled over the below problem, where type parameters to nested TAIT/GAT with lifetime parameters cause a "this type parameter needs to be std::marker::Sized" compiler error.

The following code fails to compile with below compiler error (Playground):

#![feature(type_alias_impl_trait)]
#![feature(generic_associated_types)]

extern crate futures;
use futures::Stream;
use std::future::Future;

trait X {
    type LineStream<'a, Repr>: Stream<Item = Repr> where Self: 'a;

    type LineStreamFut<'a,Repr>: Future<Output = Self::LineStream<'a, Repr>> where Self: 'a;

    fn line_stream<'a,Repr>(&'a self) -> Self::LineStreamFut<'a,Repr>;
}

struct Y;

impl X for Y {
    type LineStream<'a, Repr> = impl Stream<Item = Repr>;

    type LineStreamFut<'a, Repr> = impl Future<Output = Self::LineStream<'a, Repr>> ;

    fn line_stream<'a, Repr>(&'a self) -> Self::LineStreamFut<'a, Repr> {
        async {futures::stream::empty()}
    }
}

fn main() {}
warning: the cargo feature `edition2021` has been stabilized in the 1.56 release and is no longer necessary to be listed in the manifest
  See https://doc.rust-lang.org/nightly/cargo/reference/manifest.html#the-edition-field for more information about using this feature.
   Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `Repr` cannot be known at compilation time
  --> src/main.rs:23:43
   |
23 |     fn line_stream<'a, Repr>(&'a self) -> Self::LineStreamFut<'a, Repr> {
   |                        ----               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |                        |
   |                        this type parameter needs to be `std::marker::Sized`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error

In contrast, the following code, without lifetime type parameters compiles without any error.

#![feature(type_alias_impl_trait)]
#![feature(generic_associated_types)]

extern crate futures;
use futures::Stream;
use std::future::Future;

trait X {
    type LineStream<Repr>: Stream<Item = Repr>;

    type LineStreamFut<Repr>: Future<Output = Self::LineStream<Repr>>;

    fn line_stream<Repr>(&self) -> Self::LineStreamFut<Repr>;
}

struct Y;

impl X for Y {
    type LineStream<Repr> = impl Stream<Item = Repr>;

    type LineStreamFut<Repr> = impl Future<Output = Self::LineStream<Repr>> ;

    fn line_stream<Repr>(&self) -> Self::LineStreamFut<Repr> {
        async {futures::stream::empty()}
    }
}

fn main() {}

Meta

cargo version --verbose:

cargo 1.56.0-nightly (e515c3277 2021-09-08)
release: 1.56.0
commit-hash: e515c3277bf0681bfc79a9e763861bfe26bb05db
commit-date: 2021-09-08

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsF-type_alias_impl_trait`#[feature(type_alias_impl_trait)]`GATs-triagedIssues using the `generic_associated_types` feature that have been triagedS-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions