-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #68491 - pnkfelix:hide-niches-under-unsafe-cell, r=oli
Hide niches under UnsafeCell Hide any niche of T from type-construction context of `UnsafeCell<T>`. Fix #68303 Fix #68206
- Loading branch information
Showing
17 changed files
with
521 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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.