Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upMutexGuard<T> may be Sync only if T is Sync #41624
Conversation
rust-highfive
assigned
alexcrichton
Apr 29, 2017
This comment has been minimized.
This comment has been minimized.
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
bluss
added
relnotes
T-libs
labels
Apr 29, 2017
bluss
reviewed
Apr 29, 2017
| impl<'a, T: ?Sized> !marker::Send for MutexGuard<'a, T> {} | ||
| impl<'a, T: ?Sized> !Send for MutexGuard<'a, T> { } | ||
| #[stable(feature = "rust1", since = "1.18.0")] | ||
| unsafe impl<'a, T: ?Sized + Sync> Sync for MutexGuard<'a, T> { } |
This comment has been minimized.
This comment has been minimized.
bluss
Apr 29, 2017
•
Contributor
A brief test says that this is not enough to make T: !Sync → MutexGuard<T>: !Sync
Is a non-sync marker field the best way? Then opt in to Sync for this case.
Edit: Ok, sorry, that was of course a too simple test. Seems to work
This comment has been minimized.
This comment has been minimized.
eddyb
Apr 29, 2017
•
Member
So this is where I was arguing with @nikomatsakis that the current semantics for positive OIBIT impls don't make sense in terms of how they apply bounds and when, when compared with other impls.
That is, I'd expect to see these two impls:
impl<'a, T: ?Sized> !Sync for MutexGuard<'a, T> { }
unsafe impl<'a, T: ?Sized + Sync> Sync for MutexGuard<'a, T> { }Carving out a Sync subset out of a more general !Sync case.
Indeed that is what a !Sync marker field would do.
EDIT: Okay, so it's not broken but it still bugs me how it's set up.
This comment has been minimized.
This comment has been minimized.
RalfJung
Apr 29, 2017
Author
Member
@bluss: My compile-fail test seems to indicate this works, but I have to admit I don't know enough about OIBITs to say how this is supposed to be done.
@eddyb: I was not sure if that would work; are overlapping impls handed correctly? If they do, sure, I'm happy to change this. I will then also add a test checking that MutexGuard<i32> is Sync.
This comment has been minimized.
This comment has been minimized.
bluss
Apr 29, 2017
Contributor
It fooled me, for one. The positive impls are stable though (and the negative ones are not).
This comment has been minimized.
This comment has been minimized.
RalfJung
Apr 29, 2017
Author
Member
@eddyb This doesn't seem to work...
error[E0119]: conflicting implementations of trait `core::marker::Sync` for type `sync::mutex::MutexGuard<'_, _>`:
--> src/libstd/sync/mutex.rs:159:1
|
157 | impl<'a, T: ?Sized> !Sync for MutexGuard<'a, T> { }
| --------------------------------------------------- first implementation here
158 | #[stable(feature = "rust1", since = "1.18.0")]
159 | unsafe impl<'a, T: ?Sized + Sync> Sync for MutexGuard<'a, T> { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `sync::mutex::MutexGuard<'_, _>`
This comment has been minimized.
This comment has been minimized.
RalfJung
Apr 29, 2017
Author
Member
Do you mean "stable" as in "a stable Rust feature" or "stable" as in "preserved by more impls being added elsewhere"?
This comment has been minimized.
This comment has been minimized.
eddyb
Apr 29, 2017
Member
No, it doesn't, but I would expect it to be the only way to achieve this result, and this is me trying to remember @nikomatsakis about a previous discussion on the matter.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
withoutboats
Apr 29, 2017
•
Contributor
@eddyb I argued the same thing with @nikomatsakis at RustConf, but he demonstrated an example in which that would result in adding a non-blanket impl of a non-OIBIT being a breaking change, something we've worked very hard to avoid.
In any event we do need an RFC to revise and clarify these rules because they're currently mostly unstated.
This comment has been minimized.
This comment has been minimized.
|
Travis complains about the |
This comment has been minimized.
This comment has been minimized.
|
Thanks for catching this @RalfJung! I'm personally ahead going ahead and landing this regardless of the breakage. It's (a) a soundness fix and (b) seems unlikely to be relied upon in practice. We've got a lot of runway before this reaches stable to detect regressions and help upstream authors fix the issue. In any case cc @rust-lang/libs for the breakage aspect. For the tidy error here yeah you can just pick a new feature name that's not "rust1", and it can be something arbitrary. |
This comment has been minimized.
This comment has been minimized.
|
All right, I picked a new feature name ( |
carols10cents
added
the
S-waiting-on-review
label
May 1, 2017
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton - I think this is ready for merge. Friendly ping to keep this on your radar. |
This comment has been minimized.
This comment has been minimized.
|
Ok it's been a few days so I'm going to r+, but we may still back out and determine a new strategy to land if we discover this causes a number of regressions on crater. Regardless we'll get this in somehow! @bors: r+ |
This comment has been minimized.
This comment has been minimized.
|
|
alexcrichton
added
S-waiting-on-bors
and removed
S-waiting-on-review
labels
May 2, 2017
This comment has been minimized.
This comment has been minimized.
|
Ordinarily, we would do a crater run first. But I agree this seems like quite the corner case. |
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
May 3, 2017
This comment has been minimized.
This comment has been minimized.
|
|
bors
merged commit 23522f6
into
rust-lang:master
May 3, 2017
This comment has been minimized.
This comment has been minimized.
|
I think |
This comment has been minimized.
This comment has been minimized.
|
That's right, this will only land with 1.19. Is that a problem? |
This comment has been minimized.
This comment has been minimized.
|
it's fixed in master, so nightly docs show the correct Rust 1.19 |
This comment has been minimized.
This comment has been minimized.
|
Ah, cool. Actually, it's fixed even in the beta: https://doc.rust-lang.org/beta/std/sync/struct.MutexGuard.html |
This comment has been minimized.
This comment has been minimized.
|
hmm. I disagree with this fix. |
This comment has been minimized.
This comment has been minimized.
|
So But, theoretically, someone could write an unsafe abstraction that isn't thread-safe in a The thought of that is rather unsettling, and one more thing to keep in mind about unsafe code. |
This comment has been minimized.
This comment has been minimized.
If you mean the fix is to add that field and leave the existing fields unchanged, I disagree. That would restrict
That's right. |
This comment has been minimized.
This comment has been minimized.
|
@RalfJung that is a bit weird. It does feel as if |
This comment has been minimized.
This comment has been minimized.
|
Yes, and I agree with that. However, |
This comment has been minimized.
This comment has been minimized.
|
@RalfJung right, that's what I'm thinking - this seems like a design flaw with auto traits :/ you can't have a field which isn't taken into account by auto traits. |
RalfJung
deleted the
RalfJung:mutexguard-sync
branch
Jul 15, 2017
This comment has been minimized.
This comment has been minimized.
|
Would it make sense for |
RalfJung commentedApr 29, 2017
Fixes #41622
This is a breaking change. Does that imply any further process?
I am not sure whether I wrote that "compilation must fail"-test correctly, but at least it is passing here with the patch applied. Same for the
since = "1.18.0", I just picked it because I had to pick something.