Skip to content

impl Trait Associated Types Do Not Leak Auto Traits #49288

Description

@cramertj

While auto trait impls of impl Trait types leak, the auto trait impls of their associated types do not:

#![feature(conservative_impl_trait)]

trait Foo {
    type Bar;
}

fn require_bar_send<F>(_: F) where F: Foo, F::Bar: Send {}

fn foo() -> impl Foo {
    struct Fooey;
    impl Foo for Fooey {
        type Bar = ();
    }
    Fooey
}

fn main() {
    require_bar_send(foo()); // ERROR
}

This can be fixed by writing impl Foo<Bar=impl Send>, but this can get annoying quickly: async traits, whose methods each return a separate associated type implementing Future, need to have a Bar=impl Send for each method:

trait Foo {
    type FooFut: Future<...>;
    fn foo(&self) -> Self::FooFut;

    type BarFut: Future<...>;
    fn bar(&self) -> Self::BarFut;

    type BazFut: Future<...>;
    fn baz(&self) -> Self::BazFut;
}

fn foo() -> impl Foo<FooFut=impl Send, BarFut=impl Send, BazFut=impl Send> { ... }

This isn't a bug, but I'm interested in seeing if there're ways we can improve here-- should associated type auto traits leak? I can see that becoming a huge problem since you'd then want the associated types of associated types auto traits to leak, etc, but there's not an obvious solution to me. I really just want an "everything you care about here is thread-safe" button, but I don't know how to supply that easily.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-langRelevant to the language team

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions