Skip to content
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

Remove destructor-related restrictions from unions #38934

Merged
merged 1 commit into from
Jan 10, 2017

Conversation

Manishearth
Copy link
Member

They don't have drop glue.

This doesn't fix the rvalue promotion issues when trying to do things like static FOO: NoDrop<Bar> = NoDrop {inner: Bar}. I'm not sure if we should fix that.

@rust-highfive
Copy link
Collaborator

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

@eddyb
Copy link
Member

eddyb commented Jan 8, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Jan 8, 2017

📌 Commit efd1877 has been approved by eddyb

@eddyb
Copy link
Member

eddyb commented Jan 9, 2017

@bors r- Travis failed, you need to maybe update compile-fail tests.

@Manishearth
Copy link
Member Author

We have a test, union-copy.rs, that tests that you can't impl Copy on a union containing noncopy types. Which is sort of the opposite of what this PR is trying to do. I thought the fact that unions can't be Copy was a bug; maybe it's intentional?

Regardless, I feel that it should be possible to do this.

@Manishearth
Copy link
Member Author

Ah. #36016 (comment) mentions that there should be a test for Copy on non-copy-contents unions.

#36016 (comment) is an opposition to that.

cc @nikomatsakis @petrochenkov I'd really like to support copy being implemented on unions (even if #[derive(Copy)] doesn't work, at least so that NoDrop can work. There needs to be a way to opt out of drop glue and !Copy for generics without needing to resort to const size_of arrays.

@petrochenkov
Copy link
Contributor

Of course, the restriction is intentional, this change creates a hole in the current move/initialization semantics of unions and removes existing guarantees.
impl Copy for anything (including unions) with Copy fields should at least require unsafe.
I wanted to extend the union RFC with more details since it was implemented, maybe it's time to do it now. Can this PR wait for a week or so?

@Manishearth
Copy link
Member Author

I can remove the Copy related bits if that's the case. My main goal here was to get rid of the needs_drop requirement, the copy bit can wait.

Got an explanation of the hole?

@petrochenkov
Copy link
Contributor

petrochenkov commented Jan 9, 2017

@Manishearth

Got an explanation of the hole?

Now union can be thought as an enum with unknown discriminant, so, for example, you have a guarantee that union U { s: String } always contains a valid String that can be safely accessed. If Copy can be implemented for U then this guarantee is dropped and you have a way to create dangling Strings by copying U. With this hole other checks, like "union values must be initialized" make much less sense.

My main goal here was to get rid of the needs_drop requirement

Ha, this is much better. I believe the value returned by needs_drop intrinsic can be changed without affecting anything else.

@Manishearth
Copy link
Member Author

Split it into two commits. Are we okay with just landing the first one?

@@ -235,6 +235,11 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
})
});

if def.is_union() {
// unions don't have destructors regardless of the child types
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test checking that needs_drop still returns true on unions that implement Drop themselves?
I suspect the condition should be def.is_union() && !def.has_dtor() instead of just def.is_union().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The destructor is added below IIRC.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, that bit gets re-added below.

@petrochenkov
Copy link
Contributor

@Manishearth
I'm okay with the first commit modulo comment.

@Manishearth
Copy link
Member Author

Done.

@Manishearth
Copy link
Member Author

@eddyb r?

@eddyb
Copy link
Member

eddyb commented Jan 9, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Jan 9, 2017

📌 Commit b9b0732 has been approved by eddyb

@bors
Copy link
Contributor

bors commented Jan 9, 2017

⌛ Testing commit b9b0732 with merge 5b0ae4b...

@nikomatsakis
Copy link
Contributor

Woah. I feel that this whole discussion went by awfully fast. Can someone summarize the questions (and answers) on the tracking issue, please? (This seems to get at the philosophical debate about how best to think of unions that @petrochenkov and I were pursuing some time back.)

@bors
Copy link
Contributor

bors commented Jan 9, 2017

💔 Test failed - status-travis

@Manishearth
Copy link
Member Author

Manishearth commented Jan 9, 2017

@Manishearth
Copy link
Member Author

FWIW, the current PR only changes the fact that unions containing dtors are needs_drop (and can't be put in statics). It doesn't change the Copy thing at all, although it initially intended to.

@bors
Copy link
Contributor

bors commented Jan 10, 2017

⌛ Testing commit b9b0732 with merge 26dc969...

bors added a commit that referenced this pull request Jan 10, 2017
Remove destructor-related restrictions from unions

They don't have drop glue.

This doesn't fix the rvalue promotion issues when trying to do things like `static FOO: NoDrop<Bar> = NoDrop {inner: Bar}`. I'm not sure if we should fix that.
@bors
Copy link
Contributor

bors commented Jan 10, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: eddyb
Pushing 26dc969 to master...

@bors bors merged commit b9b0732 into rust-lang:master Jan 10, 2017
@Manishearth Manishearth deleted the nodrop branch January 10, 2017 05:12
@nikomatsakis
Copy link
Contributor

FWIW, the current PR only changes the fact that unions containing dtors are needs_drop (and can't be put in statics). It doesn't change the Copy thing at all, although it initially intended to.

OK, that seems pretty harmless.

@brson brson added the relnotes Marks issues that should be documented in the release notes of the next release. label Jan 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants