-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Don't build drop shim for polymorphic types #110930
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
Merged
Changes from all commits
Commits
Show all changes
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// build-pass | ||
// compile-flags: -Zmir-opt-level=3 | ||
|
||
use std::fmt::Debug; | ||
use std::mem::ManuallyDrop; | ||
use std::ptr; | ||
|
||
pub trait BitRegister {} | ||
|
||
macro_rules! register { | ||
($($t:ty),+ $(,)?) => { $( | ||
impl BitRegister for $t { | ||
} | ||
)* }; | ||
} | ||
|
||
register!(u8, u16, u32); | ||
|
||
pub trait BitStore: Sized + Debug { | ||
/// The register type that the implementor describes. | ||
type Mem: BitRegister + Into<Self>; | ||
} | ||
|
||
macro_rules! store { | ||
($($t:ty),+ $(,)?) => { $( | ||
impl BitStore for $t { | ||
type Mem = Self; | ||
} | ||
)+ }; | ||
} | ||
|
||
store!(u8, u16, u32,); | ||
|
||
#[repr(C)] | ||
pub struct BitVec<T> | ||
where | ||
T: BitStore, | ||
{ | ||
/// Region pointer describing the live portion of the owned buffer. | ||
pointer: ptr::NonNull<T>, | ||
/// Allocated capacity, in elements `T`, of the owned buffer. | ||
capacity: usize, | ||
} | ||
|
||
impl<T> BitVec<T> | ||
where | ||
T: BitStore, | ||
{ | ||
pub fn new() -> Self { | ||
let pointer = ptr::NonNull::<T>::new(ptr::null_mut()).unwrap(); | ||
|
||
BitVec { pointer, capacity: 10 } | ||
} | ||
|
||
pub fn clear(&mut self) { | ||
unsafe { | ||
self.set_len(0); | ||
} | ||
} | ||
|
||
#[inline] | ||
pub unsafe fn set_len(&mut self, new_len: usize) {} | ||
|
||
fn with_vec<F, R>(&mut self, func: F) -> R | ||
where | ||
F: FnOnce(&mut ManuallyDrop<Vec<T::Mem>>) -> R, | ||
{ | ||
let cap = self.capacity; | ||
let elts = 10; | ||
let mut vec = ManuallyDrop::new(unsafe { Vec::from_raw_parts(ptr::null_mut(), elts, cap) }); | ||
let out = func(&mut vec); | ||
|
||
out | ||
} | ||
} | ||
|
||
impl<T> Drop for BitVec<T> | ||
where | ||
T: BitStore, | ||
{ | ||
#[inline] | ||
fn drop(&mut self) { | ||
// The buffer elements do not have destructors. | ||
self.clear(); | ||
// Run the `Vec` destructor to deällocate the buffer. | ||
self.with_vec(|vec| unsafe { ManuallyDrop::drop(vec) }); | ||
} | ||
} | ||
|
||
fn main() { | ||
let bitvec = BitVec::<u32>::new(); | ||
} |
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.
Not sure if we want to do this in a more generalized way. I believe the normalization in
move_paths_for_fields
is the only place where building drop shims can go wrong, so rejecting those types here should be sufficient (though this change feels a little hacky).Rejecting these here seemed preferable to me compared to making changes in the shim/drop elaboration code.