rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.58k stars 12.74k forks source link

Sync/Send mismatch for Iter? #53625

Closed sparecycles closed 1 year ago

sparecycles commented 6 years ago

There are a few places where an Iter implements Send when the item/key/value traits are Sync. The other related types (IterMut, etc...) implement Sync for Sync items and Send for Send items.

See line 1209 in https://github.com/rust-lang/rust/blob/917945d662c42053383fe3e71cb0f313d585e459/src/liballoc/collections/linked_list.rs#L1202-L1218

and also in https://github.com/rust-lang/rust/blob/917945d662c42053383fe3e71cb0f313d585e459/src/libcore/slice/mod.rs#L2650-L2653

as well as https://github.com/rust-lang/rust/blob/917945d662c42053383fe3e71cb0f313d585e459/src/libstd/collections/hash/table.rs#L916-L917

( Noticed this pattern in https://github.com/hyperium/http/pull/239 )

qnighy commented 6 years ago

They just inherit the item type's sendability/syncability, not the generic parameter's. &mut T: Send if T: Send and &T: Send if T: Sync.

sparecycles commented 6 years ago

Thanks, I see now why Iter<Sync> could be Send, but I'm not sure about related structs like Drain or IntoIters.

It's interesting that Iter<T: Send> is not Send, but IterMut<T: Send> is.

Dylan-DPC commented 1 year ago

I am closing this issue as it is not a bug and working as intended.

sparecycles commented 1 year ago

You really are seeing Sync implying Send on, e.g., https://github.com/rust-lang/rust/blob/b6852428a8ea9728369b64b9964cad8e258403d3/library/alloc/src/collections/linked_list.rs#L1990-L1991

https://github.com/rust-lang/rust/blob/b6852428a8ea9728369b64b9964cad8e258403d3/library/alloc/src/collections/linked_list.rs#L2002-L2003

and that's fine?

sparecycles commented 1 year ago

Okay, looks like this is actually intentional, but it's still a bit cryptic.

https://github.com/rust-lang/rust/pull/27615#discussion_r36677777