Skip to content

Auto trait leakage on associated types of impl Trait return value #21

@benediktsatalia

Description

@benediktsatalia

When using

rustc 1.86.0-nightly (419b3e2d3 2025-01-15)

What I tried to do

Return an impl Trait with an associated type and rely on the leakage of auto traits like Send and Sync on the associated type.

Simplified example:

trait Inner {}

trait Outer {
    type I: Inner;
    
    fn get_inner(&self) -> Self::I;
}

impl Inner for () {}

impl Outer for () {
    type I = ();
    
    fn get_inner(&self) -> Self::I { 
        ()
    }
}

fn outer1() -> impl Outer {
    ()
}

fn outer2() -> impl Outer<I = impl Inner> {
    ()
}

fn requires_send(_: impl Send) {}

fn main() {
    
    // uncommenting the following does not compile!
    // requires_send(outer1().get_inner());
    
    // this does compile!
    requires_send(outer2().get_inner());
}

What happened

The version using outer2 compiles fine and does the leakage, while the version with outer1 does not leak and therefore the associated type does not implement Send.

What I expected

I would have expected that both versions behave the same with regards to auto trait leakage. Maybe this is intended behavior, but it was surprising to me and I also couldn't find any documentation about this behavior.

PS: I first posted this on the user forum: https://users.rust-lang.org/t/impl-trait-return-value-with-associated-type-that-should-be-send-sync/124089 where it got suggested that I post an issue here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions