You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
When using
What I tried to do
Return an
impl Trait
with an associated type and rely on the leakage of auto traits likeSend
andSync
on the associated type.Simplified example:
What happened
The version using
outer2
compiles fine and does the leakage, while the version withouter1
does not leak and therefore the associated type does not implementSend
.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.
The text was updated successfully, but these errors were encountered: