gpui: Remove some unnecessary unsafe code (#40483)

Release Notes:

- N/A *or* Added/Fixed/Improved ...
This commit is contained in:
Lukas Wirth
2025-10-17 14:20:13 +02:00
committed by GitHub
parent eb3f9b0ea3
commit b59a3bbd49
10 changed files with 76 additions and 92 deletions
Generated
+1
View File
@@ -7390,6 +7390,7 @@ dependencies = [
"parking", "parking",
"parking_lot", "parking_lot",
"pathfinder_geometry", "pathfinder_geometry",
"pin-project",
"postage", "postage",
"pretty_assertions", "pretty_assertions",
"profiling", "profiling",
+1
View File
@@ -135,6 +135,7 @@ waker-fn = "1.2.0"
lyon = "1.0" lyon = "1.0"
workspace-hack.workspace = true workspace-hack.workspace = true
libc.workspace = true libc.workspace = true
pin-project = "1.1.10"
[target.'cfg(target_os = "macos")'.dependencies] [target.'cfg(target_os = "macos")'.dependencies]
block = "0.1" block = "0.1"
+2 -7
View File
@@ -378,11 +378,9 @@ pub struct Entity<T> {
#[deref] #[deref]
#[deref_mut] #[deref_mut]
pub(crate) any_entity: AnyEntity, pub(crate) any_entity: AnyEntity,
pub(crate) entity_type: PhantomData<T>, pub(crate) entity_type: PhantomData<fn(T) -> T>,
} }
unsafe impl<T> Send for Entity<T> {}
unsafe impl<T> Sync for Entity<T> {}
impl<T> Sealed for Entity<T> {} impl<T> Sealed for Entity<T> {}
impl<T: 'static> Entity<T> { impl<T: 'static> Entity<T> {
@@ -657,7 +655,7 @@ pub struct WeakEntity<T> {
#[deref] #[deref]
#[deref_mut] #[deref_mut]
any_entity: AnyWeakEntity, any_entity: AnyWeakEntity,
entity_type: PhantomData<T>, entity_type: PhantomData<fn(T) -> T>,
} }
impl<T> std::fmt::Debug for WeakEntity<T> { impl<T> std::fmt::Debug for WeakEntity<T> {
@@ -669,9 +667,6 @@ impl<T> std::fmt::Debug for WeakEntity<T> {
} }
} }
unsafe impl<T> Send for WeakEntity<T> {}
unsafe impl<T> Sync for WeakEntity<T> {}
impl<T> Clone for WeakEntity<T> { impl<T> Clone for WeakEntity<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
+3 -1
View File
@@ -888,7 +888,9 @@ impl VisualTestContext {
// safety: on_quit will be called after the test has finished. // safety: on_quit will be called after the test has finished.
// the executor will ensure that all tasks related to the test have stopped. // the executor will ensure that all tasks related to the test have stopped.
// so there is no way for cx to be accessed after on_quit is called. // so there is no way for cx to be accessed after on_quit is called.
let cx = Box::leak(unsafe { Box::from_raw(ptr) }); // todo: This is unsound under stacked borrows (also tree borrows probably?)
// the mutable reference invalidates `ptr` which is later used in the closure
let cx = unsafe { &mut *ptr };
cx.on_quit(move || unsafe { cx.on_quit(move || unsafe {
drop(Box::from_raw(ptr)); drop(Box::from_raw(ptr));
}); });
+53 -65
View File
@@ -15,9 +15,7 @@ struct ArenaElement {
impl Drop for ArenaElement { impl Drop for ArenaElement {
#[inline(always)] #[inline(always)]
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe { (self.drop)(self.value) };
(self.drop)(self.value);
}
} }
} }
@@ -40,33 +38,29 @@ impl Drop for Chunk {
impl Chunk { impl Chunk {
fn new(chunk_size: NonZeroUsize) -> Self { fn new(chunk_size: NonZeroUsize) -> Self {
unsafe { // this only fails if chunk_size is unreasonably huge
// this only fails if chunk_size is unreasonably huge let layout = alloc::Layout::from_size_align(chunk_size.get(), 1).unwrap();
let layout = alloc::Layout::from_size_align(chunk_size.get(), 1).unwrap(); let start = unsafe { alloc::alloc(layout) };
let start = alloc::alloc(layout); if start.is_null() {
if start.is_null() { handle_alloc_error(layout);
handle_alloc_error(layout); }
} let end = unsafe { start.add(chunk_size.get()) };
let end = start.add(chunk_size.get()); Self {
Self { start,
start, end,
end, offset: start,
offset: start,
}
} }
} }
fn allocate(&mut self, layout: alloc::Layout) -> Option<NonNull<u8>> { fn allocate(&mut self, layout: alloc::Layout) -> Option<NonNull<u8>> {
unsafe { let aligned = unsafe { self.offset.add(self.offset.align_offset(layout.align())) };
let aligned = self.offset.add(self.offset.align_offset(layout.align())); let next = unsafe { aligned.add(layout.size()) };
let next = aligned.add(layout.size());
if next <= self.end { if next <= self.end {
self.offset = next; self.offset = next;
NonNull::new(aligned) NonNull::new(aligned)
} else { } else {
None None
}
} }
} }
@@ -122,54 +116,48 @@ impl Arena {
where where
F: FnOnce() -> T, F: FnOnce() -> T,
{ {
unsafe { unsafe { ptr::write(ptr, f()) };
ptr::write(ptr, f());
}
} }
unsafe fn drop<T>(ptr: *mut u8) { unsafe fn drop<T>(ptr: *mut u8) {
unsafe { unsafe { std::ptr::drop_in_place(ptr.cast::<T>()) };
std::ptr::drop_in_place(ptr.cast::<T>());
}
} }
unsafe { let layout = alloc::Layout::new::<T>();
let layout = alloc::Layout::new::<T>(); let mut current_chunk = &mut self.chunks[self.current_chunk_index];
let mut current_chunk = &mut self.chunks[self.current_chunk_index]; let ptr = if let Some(ptr) = current_chunk.allocate(layout) {
let ptr = if let Some(ptr) = current_chunk.allocate(layout) { ptr.as_ptr()
} else {
self.current_chunk_index += 1;
if self.current_chunk_index >= self.chunks.len() {
self.chunks.push(Chunk::new(self.chunk_size));
assert_eq!(self.current_chunk_index, self.chunks.len() - 1);
log::trace!(
"increased element arena capacity to {}kb",
self.capacity() / 1024,
);
}
current_chunk = &mut self.chunks[self.current_chunk_index];
if let Some(ptr) = current_chunk.allocate(layout) {
ptr.as_ptr() ptr.as_ptr()
} else { } else {
self.current_chunk_index += 1; panic!(
if self.current_chunk_index >= self.chunks.len() { "Arena chunk_size of {} is too small to allocate {} bytes",
self.chunks.push(Chunk::new(self.chunk_size)); self.chunk_size,
assert_eq!(self.current_chunk_index, self.chunks.len() - 1); layout.size()
log::trace!( );
"increased element arena capacity to {}kb",
self.capacity() / 1024,
);
}
current_chunk = &mut self.chunks[self.current_chunk_index];
if let Some(ptr) = current_chunk.allocate(layout) {
ptr.as_ptr()
} else {
panic!(
"Arena chunk_size of {} is too small to allocate {} bytes",
self.chunk_size,
layout.size()
);
}
};
inner_writer(ptr.cast(), f);
self.elements.push(ArenaElement {
value: ptr,
drop: drop::<T>,
});
ArenaBox {
ptr: ptr.cast(),
valid: self.valid.clone(),
} }
};
unsafe { inner_writer(ptr.cast(), f) };
self.elements.push(ArenaElement {
value: ptr,
drop: drop::<T>,
});
ArenaBox {
ptr: ptr.cast(),
valid: self.valid.clone(),
} }
} }
} }
+1 -3
View File
@@ -525,9 +525,7 @@ where
"local task dropped by a thread that didn't spawn it. Task spawned at {}", "local task dropped by a thread that didn't spawn it. Task spawned at {}",
self.location self.location
); );
unsafe { unsafe { ManuallyDrop::drop(&mut self.inner) };
ManuallyDrop::drop(&mut self.inner);
}
} }
} }
-2
View File
@@ -348,8 +348,6 @@ impl Debug for DisplayId {
} }
} }
unsafe impl Send for DisplayId {}
/// Which part of the window to resize /// Which part of the window to resize
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResizeEdge { pub enum ResizeEdge {
+8 -3
View File
@@ -69,9 +69,7 @@ impl TaffyLayoutEngine {
} else { } else {
self.taffy self.taffy
// This is safe because LayoutId is repr(transparent) to taffy::tree::NodeId. // This is safe because LayoutId is repr(transparent) to taffy::tree::NodeId.
.new_with_children(taffy_style, unsafe { .new_with_children(taffy_style, LayoutId::to_taffy_slice(children))
std::mem::transmute::<&[LayoutId], &[taffy::NodeId]>(children)
})
.expect(EXPECT_MESSAGE) .expect(EXPECT_MESSAGE)
.into() .into()
} }
@@ -265,6 +263,13 @@ impl TaffyLayoutEngine {
#[repr(transparent)] #[repr(transparent)]
pub struct LayoutId(NodeId); pub struct LayoutId(NodeId);
impl LayoutId {
fn to_taffy_slice(node_ids: &[Self]) -> &[taffy::NodeId] {
// SAFETY: LayoutId is repr(transparent) to taffy::tree::NodeId.
unsafe { std::mem::transmute::<&[LayoutId], &[taffy::NodeId]>(node_ids) }
}
}
impl std::hash::Hash for LayoutId { impl std::hash::Hash for LayoutId {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
u64::from(self.0).hash(state); u64::from(self.0).hash(state);
+6 -7
View File
@@ -83,8 +83,11 @@ impl<T: Future> FutureExt for T {
} }
} }
#[pin_project::pin_project]
pub struct WithTimeout<T> { pub struct WithTimeout<T> {
#[pin]
future: T, future: T,
#[pin]
timer: Task<()>, timer: Task<()>,
} }
@@ -97,15 +100,11 @@ impl<T: Future> Future for WithTimeout<T> {
type Output = Result<T::Output, Timeout>; type Output = Result<T::Output, Timeout>;
fn poll(self: Pin<&mut Self>, cx: &mut task::Context) -> task::Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut task::Context) -> task::Poll<Self::Output> {
// SAFETY: the fields of Timeout are private and we never move the future ourselves let this = self.project();
// And its already pinned since we are being polled (all futures need to be pinned to be polled)
let this = unsafe { &raw mut *self.get_unchecked_mut() };
let future = unsafe { Pin::new_unchecked(&mut (*this).future) };
let timer = unsafe { Pin::new_unchecked(&mut (*this).timer) };
if let task::Poll::Ready(output) = future.poll(cx) { if let task::Poll::Ready(output) = this.future.poll(cx) {
task::Poll::Ready(Ok(output)) task::Poll::Ready(Ok(output))
} else if timer.poll(cx).is_ready() { } else if this.timer.poll(cx).is_ready() {
task::Poll::Ready(Err(Timeout)) task::Poll::Ready(Err(Timeout))
} else { } else {
task::Poll::Pending task::Poll::Pending
+1 -4
View File
@@ -4650,7 +4650,7 @@ pub struct WindowHandle<V> {
#[deref] #[deref]
#[deref_mut] #[deref_mut]
pub(crate) any_handle: AnyWindowHandle, pub(crate) any_handle: AnyWindowHandle,
state_type: PhantomData<V>, state_type: PhantomData<fn(V) -> V>,
} }
impl<V> Debug for WindowHandle<V> { impl<V> Debug for WindowHandle<V> {
@@ -4786,9 +4786,6 @@ impl<V: 'static> From<WindowHandle<V>> for AnyWindowHandle {
} }
} }
unsafe impl<V> Send for WindowHandle<V> {}
unsafe impl<V> Sync for WindowHandle<V> {}
/// A handle to a window with any root view type, which can be downcast to a window with a specific root view type. /// A handle to a window with any root view type, which can be downcast to a window with a specific root view type.
#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct AnyWindowHandle { pub struct AnyWindowHandle {