Skip to content

Commit 01870ba

Browse files
authored
Merge pull request rust-lang#18606 from ChayimFriedman2/improve-soundness-just-a-bit
minor: Improve soundness a bit by making `TaggedArcPtr::try_as_arc_owned()` unsafe
2 parents fef7ca0 + 0d328a8 commit 01870ba

File tree

1 file changed

+17
-13
lines changed
  • src/tools/rust-analyzer/crates/intern/src

1 file changed

+17
-13
lines changed

src/tools/rust-analyzer/crates/intern/src/symbol.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,12 @@ impl TaggedArcPtr {
7777
}
7878

7979
/// Retrieves the tag.
80+
///
81+
/// # Safety
82+
///
83+
/// You can only drop the `Arc` if the instance is dropped.
8084
#[inline]
81-
pub(crate) fn try_as_arc_owned(self) -> Option<ManuallyDrop<Arc<Box<str>>>> {
85+
pub(crate) unsafe fn try_as_arc_owned(self) -> Option<ManuallyDrop<Arc<Box<str>>>> {
8286
// Unpack the tag from the alignment niche
8387
let tag = Strict::addr(self.packed.as_ptr()) & Self::BOOL_BITS;
8488
if tag != 0 {
@@ -245,16 +249,14 @@ impl Symbol {
245249
}
246250
}
247251

248-
ManuallyDrop::into_inner(
249-
match shard.raw_entry_mut().from_key_hashed_nocheck::<str>(hash, arc.as_ref()) {
250-
RawEntryMut::Occupied(occ) => occ.remove_entry(),
251-
RawEntryMut::Vacant(_) => unreachable!(),
252-
}
253-
.0
254-
.0
255-
.try_as_arc_owned()
256-
.unwrap(),
257-
);
252+
let ptr = match shard.raw_entry_mut().from_key_hashed_nocheck::<str>(hash, arc.as_ref()) {
253+
RawEntryMut::Occupied(occ) => occ.remove_entry(),
254+
RawEntryMut::Vacant(_) => unreachable!(),
255+
}
256+
.0
257+
.0;
258+
// SAFETY: We're dropping, we have ownership.
259+
ManuallyDrop::into_inner(unsafe { ptr.try_as_arc_owned().unwrap() });
258260
debug_assert_eq!(Arc::count(arc), 1);
259261

260262
// Shrink the backing storage if the shard is less than 50% occupied.
@@ -267,7 +269,8 @@ impl Symbol {
267269
impl Drop for Symbol {
268270
#[inline]
269271
fn drop(&mut self) {
270-
let Some(arc) = self.repr.try_as_arc_owned() else {
272+
// SAFETY: We're dropping, we have ownership.
273+
let Some(arc) = (unsafe { self.repr.try_as_arc_owned() }) else {
271274
return;
272275
};
273276
// When the last `Ref` is dropped, remove the object from the global map.
@@ -288,7 +291,8 @@ impl Clone for Symbol {
288291
}
289292

290293
fn increase_arc_refcount(repr: TaggedArcPtr) -> TaggedArcPtr {
291-
let Some(arc) = repr.try_as_arc_owned() else {
294+
// SAFETY: We're not dropping the `Arc`.
295+
let Some(arc) = (unsafe { repr.try_as_arc_owned() }) else {
292296
return repr;
293297
};
294298
// increase the ref count

0 commit comments

Comments
 (0)