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

UnsafeCell blocks niches inside its nested type from being available outside #99011

Merged
merged 19 commits into from
Jul 13, 2022

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Jul 7, 2022

fixes #87341

This implements the plan by @eddyb in #87341 (comment)

Somewhat related PR (not strictly necessary, but that cleanup made this PR simpler): #94527

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 7, 2022
@rustbot
Copy link
Collaborator

rustbot commented Jul 7, 2022

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rust-highfive
Copy link
Collaborator

r? @davidtwco

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 7, 2022
@oli-obk oli-obk changed the title UnsafeCell now has no niches, ever. UnsafeCell now has no niches, not even transitively Jul 7, 2022
@oli-obk oli-obk changed the title UnsafeCell now has no niches, not even transitively UnsafeCell blocks niches inside its nested type from being available outside Jul 7, 2022
@RalfJung
Copy link
Member

RalfJung commented Jul 7, 2022

Please make sure this is covered by tests. #87341 has a nice test set at the top.

Copy link
Member

@eddyb eddyb left a comment

Choose a reason for hiding this comment

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

LGTM (modulo the comment I left about handling all Abis, and tests ofc).

IMO limiting this to UnsafeCell is more reasonable than #[repr(no_niche)].

The question I ask myself is: why would anyone want #[repr(no_niche)] without the semantics of UnsafeCell and/or MaybeUninit?

I can only think of layout/ABI misuses (that would be defeated by layout randomization), I can't imagine that there's a way to have unsafe code that would only need #[repr(no_niche)] (and nothing else) to both have no UB, and work under layout randomization.

Comment on lines 1107 to 1113

// Update `largest_niche` if we have introduced a larger niche.
let niche = if def.repr().hide_niche() {
None
if def.is_unsafe_cell() {
match scalar {
Scalar::Initialized { value, valid_range } => {
*valid_range = WrappingRange::full(value.size(dl))
}
// Already doesn't have any niches
Scalar::Union { .. } => {}
}
st.largest_niche = None;
Copy link
Member

Choose a reason for hiding this comment

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

Looks like the original code got misplaced into the #[rustc_layout_scalar_valid_range] handling, instead of being separate.
I think it should be moved out and made to exhaustively handle all Abis.

More specifically, Abi::{Scalar,ScalarPair,Vector}) all have Scalars in them - and even if ScalarPair appears to be handled, note that you're only operating its first Scalar.

So when testing, make sure to also check UnsafeCell around pairs with niches in both sides of the pair and custom #[repr(simd)] structs with niche'd element types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

custom #[repr(simd)] structs with niche'd element types.

I tried, we have checks preventing such datastructures from existing at all. So this can't happen.

Copy link
Member

Choose a reason for hiding this comment

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

The checks are not perfect: https://godbolt.org/z/W8o3Gxhdo.

@davidtwco
Copy link
Member

r? @eddyb

@rust-highfive rust-highfive assigned eddyb and unassigned davidtwco Jul 8, 2022
@oli-obk
Copy link
Contributor Author

oli-obk commented Jul 8, 2022

has a nice test set at the top.

I didn't add those as we already had equivalent tests in

assert_eq!(size_of::<Option<Wrapper<u32>>>(), 8);
assert_eq!(size_of::<Option<Wrapper<N32>>>(), 4);
assert_eq!(size_of::<Option<Transparent<u32>>>(), 8);
assert_eq!(size_of::<Option<Transparent<N32>>>(), 4);
assert_eq!(size_of::<Option<NoNiche<u32>>>(), 8);
assert_eq!(size_of::<Option<NoNiche<N32>>>(), 8);
assert_eq!(size_of::<Option<UnsafeCell<u32>>>(), 8);
assert_eq!(size_of::<Option<UnsafeCell<N32>>>(), 8);

I'll add the libstd type tests for completeness now

@oli-obk
Copy link
Contributor Author

oli-obk commented Jul 8, 2022

@rustbot review

@RalfJung
Copy link
Member

RalfJung commented Jul 8, 2022

 I didn't add those as we already had equivalent tests in 

That test already passed before this PR. So it cannot be testing the right thing.

@oli-obk
Copy link
Contributor Author

oli-obk commented Jul 12, 2022

@bors r=eddyb

@bors
Copy link
Contributor

bors commented Jul 12, 2022

📌 Commit 7269196 has been approved by eddyb

It is now in the queue for this repository.

@bors bors 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 Jul 12, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Jul 12, 2022
`UnsafeCell` blocks niches inside its nested type from being available outside

fixes rust-lang#87341

This implements the plan by `@eddyb` in rust-lang#87341 (comment)

Somewhat related PR (not strictly necessary, but that cleanup made this PR simpler): rust-lang#94527
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Jul 13, 2022
`UnsafeCell` blocks niches inside its nested type from being available outside

fixes rust-lang#87341

This implements the plan by ``@eddyb`` in rust-lang#87341 (comment)

Somewhat related PR (not strictly necessary, but that cleanup made this PR simpler): rust-lang#94527
@Dylan-DPC
Copy link
Member

failed in rollup

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 13, 2022
@oli-obk
Copy link
Contributor Author

oli-obk commented Jul 13, 2022

@bors r=eddyb

@bors
Copy link
Contributor

bors commented Jul 13, 2022

📌 Commit 519c07b has been approved by eddyb

It is now in the queue for this repository.

@bors bors 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 13, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 13, 2022
Rollup of 5 pull requests

Successful merges:

 - rust-lang#98574 (Lower let-else in MIR)
 - rust-lang#99011 (`UnsafeCell` blocks niches inside its nested type from being available outside)
 - rust-lang#99030 (diagnostics: error messages when struct literals fail to parse)
 - rust-lang#99155 (Keep unstable target features for asm feature checking)
 - rust-lang#99199 (Refactor: remove an unnecessary `span_to_snippet`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 1e7d04b into rust-lang:master Jul 13, 2022
@rustbot rustbot added this to the 1.64.0 milestone Jul 13, 2022
flip1995 pushed a commit to flip1995/rust that referenced this pull request Jul 18, 2022
`UnsafeCell` blocks niches inside its nested type from being available outside

fixes rust-lang#87341

This implements the plan by `@eddyb` in rust-lang#87341 (comment)

Somewhat related PR (not strictly necessary, but that cleanup made this PR simpler): rust-lang#94527
flip1995 pushed a commit to flip1995/rust that referenced this pull request Jul 18, 2022
Rollup of 5 pull requests

Successful merges:

 - rust-lang#98574 (Lower let-else in MIR)
 - rust-lang#99011 (`UnsafeCell` blocks niches inside its nested type from being available outside)
 - rust-lang#99030 (diagnostics: error messages when struct literals fail to parse)
 - rust-lang#99155 (Keep unstable target features for asm feature checking)
 - rust-lang#99199 (Refactor: remove an unnecessary `span_to_snippet`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
JohnTitor pushed a commit to JohnTitor/rust that referenced this pull request Jul 26, 2022
Rollup of 5 pull requests

Successful merges:

 - rust-lang#98574 (Lower let-else in MIR)
 - rust-lang#99011 (`UnsafeCell` blocks niches inside its nested type from being available outside)
 - rust-lang#99030 (diagnostics: error messages when struct literals fail to parse)
 - rust-lang#99155 (Keep unstable target features for asm feature checking)
 - rust-lang#99199 (Refactor: remove an unnecessary `span_to_snippet`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Niches of Cell and others still not hidden
9 participants