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

Make assignments to Copy union fields safe #42083

Merged
merged 2 commits into from
May 26, 2017

Conversation

petrochenkov
Copy link
Contributor

This is an accompanying PR to PR #42068 stabilizing FFI unions.

This was first proposed in #32836 (comment), see subsequent comments as well.
Assignments to Copy union fields do not read any data from the union and are equivalent to whole union assignments, which are safe, so they should be safe as well. This removes a significant number of "false positive" unsafe blocks, in code dealing with FFI unions in particular.

It desirable to make this change now, together with stabilization of FFI unions, because now it affecfts only unstable code, but later it will cause warnings/errors caused by unused_unsafe lint in stable code.

cc #32836
r? @nikomatsakis

let field_ty = self.tables.expr_ty_adjusted(lhs);
let owner_def_id = self.tcx.hir.body_owner_def_id(self.body_id);
let param_env = self.tcx.parameter_environment(owner_def_id);
if field_ty.moves_by_default(self.tcx, &param_env, field.span) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a way to query "current parameter environment" or "current body id" without keeping them in the visitor, like I did here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not at present.

Copy link
Contributor

Choose a reason for hiding this comment

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

(I think what I would rather do is to have a loop over all the bodies and instantiate a distinct visitor per body, with just the appropriate tables etc. This visitor would have to walk up to find out contextual information though (since closures, at least, inherit their "unsafety" from their parents at present.))

@alexcrichton alexcrichton added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 18, 2017
@alexcrichton
Copy link
Member

I think @nikomatsakis is out today, but we'll make sure he gets around to this in the near future!

@carols10cents
Copy link
Member

ping @nikomatsakis, are you back from PTO? have some more reviews!

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented May 23, 2017

📌 Commit 59b65b3 has been approved by nikomatsakis

@nikomatsakis nikomatsakis added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 23, 2017
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 24, 2017
…akis

Make assignments to `Copy` union fields safe

This is an accompanying PR to PR rust-lang#42068 stabilizing FFI unions.

This was first proposed in rust-lang#32836 (comment), see subsequent comments as well.
Assignments to `Copy` union fields do not read any data from the union and are [equivalent](rust-lang#32836 (comment)) to whole union assignments, which are safe, so they should be safe as well. This removes a significant number of "false positive" unsafe blocks, in code dealing with FFI unions in particular.

It desirable to make this change now, together with stabilization of FFI unions, because now it affecfts only unstable code, but later it will cause warnings/errors caused by `unused_unsafe` lint in stable code.

cc rust-lang#32836
r? @nikomatsakis
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request May 24, 2017
…akis

Make assignments to `Copy` union fields safe

This is an accompanying PR to PR rust-lang#42068 stabilizing FFI unions.

This was first proposed in rust-lang#32836 (comment), see subsequent comments as well.
Assignments to `Copy` union fields do not read any data from the union and are [equivalent](rust-lang#32836 (comment)) to whole union assignments, which are safe, so they should be safe as well. This removes a significant number of "false positive" unsafe blocks, in code dealing with FFI unions in particular.

It desirable to make this change now, together with stabilization of FFI unions, because now it affecfts only unstable code, but later it will cause warnings/errors caused by `unused_unsafe` lint in stable code.

cc rust-lang#32836
r? @nikomatsakis
@Mark-Simulacrum
Copy link
Member

I suspect that this will fail at some point, the previous rollup failed with, which looks suspiciously like it comes from this PR; possibly a conflict with another PR that landed -- I seem to recall @nikomatsakis landing something that renamed parameter_environment into param_env.

[00:06:22]    Compiling rustc v0.0.0 (file:///checkout/src/librustc)
[00:06:35] error: no method named `parameter_environment` found for type `ty::context::TyCtxt<'a, 'tcx, 'tcx>` in the current scope
[00:06:35]    --> /checkout/src/librustc/middle/effect.rs:231:54
[00:06:35]     |
[00:06:35] 231 |                             let param_env = self.tcx.parameter_environment(owner_def_id);
[00:06:35]     |                                                      ^^^^^^^^^^^^^^^^^^^^^
[00:06:35] 
[00:06:35] error[E0308]: mismatched types
[00:06:35]    --> /checkout/src/librustc/middle/effect.rs:232:68
[00:06:35]     |
[00:06:35] 232 |                             if field_ty.moves_by_default(self.tcx, &param_env, field.span) {
[00:06:35]     |                                                                    ^^^^^^^^^^ expected struct `ty::ParamEnv`, found reference
[00:06:35]     |
[00:06:35]     = note: expected type `ty::ParamEnv<'_>`
[00:06:35]                found type `&_`
[00:06:35] 
[00:06:42] error: aborting due to 2 previous errors
[00:06:42] 
[00:06:42] error: Could not compile `rustc`.

@petrochenkov
Copy link
Contributor Author

@bors r-

@petrochenkov
Copy link
Contributor Author

petrochenkov commented May 25, 2017

Rebased.
@bors r=nikomatsakis

@bors
Copy link
Contributor

bors commented May 25, 2017

📌 Commit fa13cd3 has been approved by nikomatsakis

@bors
Copy link
Contributor

bors commented May 25, 2017

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Contributor

bors commented May 25, 2017

📌 Commit fa13cd3 has been approved by nikomatsakis

@bors
Copy link
Contributor

bors commented May 26, 2017

⌛ Testing commit fa13cd3 with merge 2f278c5...

bors added a commit that referenced this pull request May 26, 2017
Make assignments to `Copy` union fields safe

This is an accompanying PR to PR #42068 stabilizing FFI unions.

This was first proposed in #32836 (comment), see subsequent comments as well.
Assignments to `Copy` union fields do not read any data from the union and are [equivalent](#32836 (comment)) to whole union assignments, which are safe, so they should be safe as well. This removes a significant number of "false positive" unsafe blocks, in code dealing with FFI unions in particular.

It desirable to make this change now, together with stabilization of FFI unions, because now it affecfts only unstable code, but later it will cause warnings/errors caused by `unused_unsafe` lint in stable code.

cc #32836
r? @nikomatsakis
@bors
Copy link
Contributor

bors commented May 26, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: nikomatsakis
Pushing 2f278c5 to master...

@bors bors merged commit fa13cd3 into rust-lang:master May 26, 2017
bors added a commit that referenced this pull request May 27, 2017
Stabilize unions with `Copy` fields and no destructor

What else is needed:
- [x] Documentation (rust-lang/reference#57).
- [x] Making assignments to `Copy` union fields safe (#42083).
- [ ] Backport? (The "stabilization decision" is from [Apr 13](#32836 (comment)), it's just this PR is late.)

cc #32836
r? @nikomatsakis
@brson brson added beta-accepted Accepted for backporting to the compiler in the beta channel. beta-nominated Nominated for backporting to the compiler in the beta channel. and removed beta-accepted Accepted for backporting to the compiler in the beta channel. beta-nominated Nominated for backporting to the compiler in the beta channel. labels May 31, 2017
@petrochenkov petrochenkov deleted the safeassign branch August 26, 2017 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants