-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Ensure ErasedData only implements appropriate auto traits
#154229
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
Zoxc
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
Zoxc:erase-auto-traits
base: main
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.
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
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 |
|---|---|---|
|
|
@@ -7,9 +7,11 @@ | |
|
|
||
| use std::ffi::OsStr; | ||
| use std::intrinsics::transmute_unchecked; | ||
| use std::marker::PhantomData; | ||
| use std::mem::MaybeUninit; | ||
|
|
||
| use rustc_ast::tokenstream::TokenStream; | ||
| use rustc_data_structures::sync::{DynSend, DynSync}; | ||
| use rustc_span::{ErrorGuaranteed, Spanned}; | ||
|
|
||
| use crate::mir::interpret::EvalToValTreeResult; | ||
|
|
@@ -18,14 +20,25 @@ use crate::traits::solve; | |
| use crate::ty::{self, Ty, TyCtxt}; | ||
| use crate::{mir, traits}; | ||
|
|
||
| unsafe extern "C" { | ||
| type NoAutoTraits; | ||
| } | ||
|
|
||
| /// Internal implementation detail of [`Erased`]. | ||
| #[derive(Copy, Clone)] | ||
| pub struct ErasedData<Storage: Copy> { | ||
| /// We use `MaybeUninit` here to make sure it's legal to store a transmuted | ||
| /// value that isn't actually of type `Storage`. | ||
| data: MaybeUninit<Storage>, | ||
| /// `Storage` is an erased type, so we use an external type here to opt-out of auto traits | ||
| /// as those would be incorrect. | ||
| no_auto_traits: PhantomData<NoAutoTraits>, | ||
| } | ||
|
|
||
| // SAFETY: The bounds on `erase_val` ensure the types we erase are `DynSync` and `DynSend` | ||
| unsafe impl<Storage: Copy> DynSync for ErasedData<Storage> {} | ||
| unsafe impl<Storage: Copy> DynSend for ErasedData<Storage> {} | ||
|
|
||
| /// Trait for types that can be erased into [`Erased<Self>`]. | ||
| /// | ||
| /// Erasing and unerasing values is performed by [`erase_val`] and [`restore_val`]. | ||
|
|
@@ -55,13 +68,11 @@ pub type Erased<T: Erasable> = ErasedData<impl Copy>; | |
| /// | ||
| /// `Erased<T>` and `Erased<U>` are type-checked as distinct types, but codegen | ||
| /// can see whether they actually have the same storage type. | ||
| /// | ||
| /// FIXME: This might have soundness issues with erasable types that don't | ||
| /// implement the same auto-traits as `[u8; _]`; see | ||
| /// <https://github.com/rust-lang/rust/pull/151715#discussion_r2740113250> | ||
| #[inline(always)] | ||
| #[define_opaque(Erased)] | ||
| pub fn erase_val<T: Erasable>(value: T) -> Erased<T> { | ||
| // The `DynSend` and `DynSync` bounds on `T` are used to | ||
| // justify the safety of the implementations of these traits for `ErasedData`. | ||
| pub fn erase_val<T: Erasable + DynSend + DynSync>(value: T) -> Erased<T> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment here referring back to the unsafe |
||
| // Ensure the sizes match | ||
| const { | ||
| if size_of::<T>() != size_of::<T::Storage>() { | ||
|
|
@@ -79,6 +90,7 @@ pub fn erase_val<T: Erasable>(value: T) -> Erased<T> { | |
| // | ||
| // SAFETY: It is safe to transmute to MaybeUninit for types with the same sizes. | ||
| data: unsafe { transmute_unchecked::<T, MaybeUninit<T::Storage>>(value) }, | ||
| no_auto_traits: PhantomData, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -89,7 +101,7 @@ pub fn erase_val<T: Erasable>(value: T) -> Erased<T> { | |
| #[inline(always)] | ||
| #[define_opaque(Erased)] | ||
| pub fn restore_val<T: Erasable>(erased_value: Erased<T>) -> T { | ||
| let ErasedData { data }: ErasedData<<T as Erasable>::Storage> = erased_value; | ||
| let ErasedData { data, .. }: ErasedData<<T as Erasable>::Storage> = erased_value; | ||
| // See comment in `erase_val` for why we use `transmute_unchecked`. | ||
| // | ||
| // SAFETY: Due to the use of impl Trait in `Erased` the only way to safely create an instance | ||
|
|
||
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
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.
Storageshould beDynSyncandDynSendtooThere 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.
We don't store
Storageor create any instance of that type.