Skip to content

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

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

Open
benediktsatalia opened this issue Jan 16, 2025 · 0 comments
Open
Labels
bug Something isn't working

Comments

@benediktsatalia
Copy link

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.

@benediktsatalia benediktsatalia added the bug Something isn't working label Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant