-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Hide niches under UnsafeCell #68491
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
Merged
bors
merged 4 commits into
rust-lang:master
from
pnkfelix:hide-niches-under-unsafe-cell
Feb 11, 2020
Merged
Hide niches under UnsafeCell #68491
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// For rust-lang/rust#68303: the contents of `UnsafeCell<T>` cannot | ||
// participate in the niche-optimization for enum discriminants. This | ||
// test checks that an `Option<UnsafeCell<NonZeroU32>>` has the same | ||
// size in memory as an `Option<UnsafeCell<u32>>` (namely, 8 bytes). | ||
|
||
// run-pass | ||
|
||
#![feature(no_niche)] | ||
|
||
use std::cell::UnsafeCell; | ||
use std::mem::size_of; | ||
use std::num::NonZeroU32 as N32; | ||
|
||
struct Wrapper<T>(T); | ||
|
||
#[repr(transparent)] | ||
struct Transparent<T>(T); | ||
|
||
#[repr(no_niche)] | ||
struct NoNiche<T>(T); | ||
|
||
fn main() { | ||
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use std::num::NonZeroU8 as N8; | ||
use std::num::NonZeroU16 as N16; | ||
|
||
#[repr(no_niche)] | ||
pub struct Cloaked(N16); | ||
//~^^ ERROR the attribute `repr(no_niche)` is currently unstable [E0658] | ||
|
||
#[repr(transparent, no_niche)] | ||
pub struct Shadowy(N16); | ||
//~^^ ERROR the attribute `repr(no_niche)` is currently unstable [E0658] | ||
|
||
#[repr(no_niche)] | ||
pub enum Cloaked1 { _A(N16), } | ||
//~^^ ERROR the attribute `repr(no_niche)` is currently unstable [E0658] | ||
|
||
#[repr(no_niche)] | ||
pub enum Cloaked2 { _A(N16), _B(u8, N8) } | ||
//~^^ ERROR the attribute `repr(no_niche)` is currently unstable [E0658] | ||
|
||
fn main() { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
error[E0658]: the attribute `repr(no_niche)` is currently unstable | ||
--> $DIR/feature-gate-no-niche.rs:4:8 | ||
| | ||
LL | #[repr(no_niche)] | ||
| ^^^^^^^^ | ||
| | ||
= help: add `#![feature(no_niche)]` to the crate attributes to enable | ||
|
||
error[E0658]: the attribute `repr(no_niche)` is currently unstable | ||
--> $DIR/feature-gate-no-niche.rs:8:21 | ||
| | ||
LL | #[repr(transparent, no_niche)] | ||
| ^^^^^^^^ | ||
| | ||
= help: add `#![feature(no_niche)]` to the crate attributes to enable | ||
|
||
error[E0658]: the attribute `repr(no_niche)` is currently unstable | ||
--> $DIR/feature-gate-no-niche.rs:12:8 | ||
| | ||
LL | #[repr(no_niche)] | ||
| ^^^^^^^^ | ||
| | ||
= help: add `#![feature(no_niche)]` to the crate attributes to enable | ||
|
||
error[E0658]: the attribute `repr(no_niche)` is currently unstable | ||
--> $DIR/feature-gate-no-niche.rs:16:8 | ||
| | ||
LL | #[repr(no_niche)] | ||
| ^^^^^^^^ | ||
| | ||
= help: add `#![feature(no_niche)]` to the crate attributes to enable | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#![feature(no_niche)] | ||
|
||
use std::num::NonZeroU8 as N8; | ||
use std::num::NonZeroU16 as N16; | ||
|
||
#[repr(no_niche)] | ||
pub union Cloaked1 { _A: N16 } | ||
//~^^ ERROR attribute should be applied to struct or enum [E0517] | ||
|
||
#[repr(no_niche)] | ||
pub union Cloaked2 { _A: N16, _B: (u8, N8) } | ||
//~^^ ERROR attribute should be applied to struct or enum [E0517] | ||
|
||
fn main() { } |
19 changes: 19 additions & 0 deletions
19
src/test/ui/repr/repr-no-niche-inapplicable-to-unions.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error[E0517]: attribute should be applied to struct or enum | ||
--> $DIR/repr-no-niche-inapplicable-to-unions.rs:6:8 | ||
| | ||
LL | #[repr(no_niche)] | ||
| ^^^^^^^^ | ||
LL | pub union Cloaked1 { _A: N16 } | ||
| ------------------------------ not a struct or enum | ||
|
||
error[E0517]: attribute should be applied to struct or enum | ||
--> $DIR/repr-no-niche-inapplicable-to-unions.rs:10:8 | ||
| | ||
LL | #[repr(no_niche)] | ||
| ^^^^^^^^ | ||
LL | pub union Cloaked2 { _A: N16, _B: (u8, N8) } | ||
| -------------------------------------------- not a struct or enum | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0517`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.