-
Notifications
You must be signed in to change notification settings - Fork 13.9k
transmutability: Support types with restricted validity ranges #148449
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
Open
joshlf
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
joshlf:transmute-nonzero
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+144
−9
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #![feature(transmutability)] | ||
| #![feature(rustc_attrs)] | ||
| #![allow(dead_code)] | ||
|
|
||
| mod assert { | ||
| use std::mem::{Assume, TransmuteFrom}; | ||
|
|
||
| pub fn is_transmutable_assume_nothing<Src, Dst>() | ||
| where | ||
| Dst: TransmuteFrom<Src, { Assume::NOTHING }>, | ||
| { | ||
| } | ||
|
|
||
| pub fn is_transmutable_assume_safety<Src, Dst>() | ||
| where | ||
| Dst: TransmuteFrom<Src, { Assume::SAFETY }>, | ||
| { | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| // FIXME: Replace this with `core::num::NonZeroU8` once we support it. | ||
| #[rustc_layout_scalar_valid_range_start(1)] | ||
| #[repr(transparent)] | ||
| struct NonZeroU8(u8); | ||
|
|
||
| // FIXME: Replace this with `core::num::NonZeroU16` once we support it. | ||
| #[rustc_layout_scalar_valid_range_start(1)] | ||
| #[repr(transparent)] | ||
| struct NonZeroU16(u16); | ||
|
|
||
| assert::is_transmutable_assume_safety::<u8, NonZeroU8>(); //~ ERROR: cannot be safely transmuted | ||
| assert::is_transmutable_assume_safety::<NonZeroU8, u8>(); | ||
|
|
||
| assert::is_transmutable_assume_nothing::<NonZeroU8, NonZeroU8>(); //~ ERROR: cannot be safely transmuted | ||
| assert::is_transmutable_assume_safety::<NonZeroU8, NonZeroU8>(); | ||
|
|
||
| assert::is_transmutable_assume_safety::<u16, NonZeroU16>(); //~ ERROR: cannot be safely transmuted | ||
| assert::is_transmutable_assume_safety::<NonZeroU16, u16>(); | ||
|
|
||
| assert::is_transmutable_assume_nothing::<NonZeroU16, NonZeroU16>(); //~ ERROR: cannot be safely transmuted | ||
| assert::is_transmutable_assume_safety::<NonZeroU16, NonZeroU16>(); | ||
| } | ||
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,63 @@ | ||
| error[E0277]: `u8` cannot be safely transmuted into `NonZeroU8` | ||
| --> $DIR/nonzero.rs:32:49 | ||
| | | ||
| LL | assert::is_transmutable_assume_safety::<u8, NonZeroU8>(); | ||
| | ^^^^^^^^^ at least one value of `u8` isn't a bit-valid value of `NonZeroU8` | ||
| | | ||
| note: required by a bound in `is_transmutable_assume_safety` | ||
| --> $DIR/nonzero.rs:16:14 | ||
| | | ||
| LL | pub fn is_transmutable_assume_safety<Src, Dst>() | ||
| | ----------------------------- required by a bound in this function | ||
| LL | where | ||
| LL | Dst: TransmuteFrom<Src, { Assume::SAFETY }>, | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_transmutable_assume_safety` | ||
|
|
||
| error[E0277]: `NonZeroU8` cannot be safely transmuted into `NonZeroU8` | ||
| --> $DIR/nonzero.rs:35:57 | ||
| | | ||
| LL | assert::is_transmutable_assume_nothing::<NonZeroU8, NonZeroU8>(); | ||
| | ^^^^^^^^^ `NonZeroU8` may carry safety invariants | ||
| | | ||
| note: required by a bound in `is_transmutable_assume_nothing` | ||
| --> $DIR/nonzero.rs:10:14 | ||
| | | ||
| LL | pub fn is_transmutable_assume_nothing<Src, Dst>() | ||
| | ------------------------------ required by a bound in this function | ||
| LL | where | ||
| LL | Dst: TransmuteFrom<Src, { Assume::NOTHING }>, | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_transmutable_assume_nothing` | ||
|
|
||
| error[E0277]: `u16` cannot be safely transmuted into `NonZeroU16` | ||
| --> $DIR/nonzero.rs:38:50 | ||
| | | ||
| LL | assert::is_transmutable_assume_safety::<u16, NonZeroU16>(); | ||
| | ^^^^^^^^^^ at least one value of `u16` isn't a bit-valid value of `NonZeroU16` | ||
| | | ||
| note: required by a bound in `is_transmutable_assume_safety` | ||
| --> $DIR/nonzero.rs:16:14 | ||
| | | ||
| LL | pub fn is_transmutable_assume_safety<Src, Dst>() | ||
| | ----------------------------- required by a bound in this function | ||
| LL | where | ||
| LL | Dst: TransmuteFrom<Src, { Assume::SAFETY }>, | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_transmutable_assume_safety` | ||
|
|
||
| error[E0277]: `NonZeroU16` cannot be safely transmuted into `NonZeroU16` | ||
| --> $DIR/nonzero.rs:41:58 | ||
| | | ||
| LL | assert::is_transmutable_assume_nothing::<NonZeroU16, NonZeroU16>(); | ||
| | ^^^^^^^^^^ `NonZeroU16` may carry safety invariants | ||
| | | ||
| note: required by a bound in `is_transmutable_assume_nothing` | ||
| --> $DIR/nonzero.rs:10:14 | ||
| | | ||
| LL | pub fn is_transmutable_assume_nothing<Src, Dst>() | ||
| | ------------------------------ required by a bound in this function | ||
| LL | where | ||
| LL | Dst: TransmuteFrom<Src, { Assume::NOTHING }>, | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_transmutable_assume_nothing` | ||
|
|
||
| error: aborting due to 4 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0277`. |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably want to
Assume::SAFETYhere, otherwise you're just getting sniped by field privacy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't look like it – see the
.stderroutput.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well that's not good; indicates we need to thread in the field visibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done – added a test to make sure we don't regress this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you're poking around here, can you check that we didn't regress this on regular structs too? I think we might've.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do the existing tests in the
safetydirectory cover this?