Skip to content

Commit 689fca0

Browse files
committed
Auto merge of #68311 - Dylan-DPC:rollup-wzgqw9t, r=Dylan-DPC
Rollup of 4 pull requests Successful merges: - #66564 (Document unsafe blocks in core::{cell, str, sync}) - #67791 (Implement Lift using interners instead of in_arena) - #68278 ([self-profiler] Add example to `-Z help` to turn on query key recording) - #68300 (Allow added string.insert benchmarks to compile) Failed merges: r? @ghost
2 parents 71c6346 + 87a2896 commit 689fca0

File tree

16 files changed

+187
-169
lines changed

16 files changed

+187
-169
lines changed

src/liballoc/benches/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::iter::repeat;
2-
use test::Bencher;
2+
use test::{black_box, Bencher};
33

44
#[bench]
55
fn bench_with_capacity(b: &mut Bencher) {

src/libarena/lib.rs

-73
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
extern crate alloc;
2222

2323
use rustc_data_structures::cold_path;
24-
use rustc_data_structures::sync::MTLock;
2524
use smallvec::SmallVec;
2625

2726
use std::cell::{Cell, RefCell};
@@ -116,11 +115,6 @@ impl<T> Default for TypedArena<T> {
116115
}
117116

118117
impl<T> TypedArena<T> {
119-
pub fn in_arena(&self, ptr: *const T) -> bool {
120-
let ptr = ptr as *const T as *mut T;
121-
122-
self.chunks.borrow().iter().any(|chunk| chunk.start() <= ptr && ptr < chunk.end())
123-
}
124118
/// Allocates an object in the `TypedArena`, returning a reference to it.
125119
#[inline]
126120
pub fn alloc(&self, object: T) -> &mut T {
@@ -334,12 +328,6 @@ impl Default for DroplessArena {
334328
}
335329

336330
impl DroplessArena {
337-
pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
338-
let ptr = ptr as *const u8 as *mut u8;
339-
340-
self.chunks.borrow().iter().any(|chunk| chunk.start() <= ptr && ptr < chunk.end())
341-
}
342-
343331
#[inline]
344332
fn align(&self, align: usize) {
345333
let final_address = ((self.ptr.get() as usize) + align - 1) & !(align - 1);
@@ -500,66 +488,5 @@ impl DroplessArena {
500488
}
501489
}
502490

503-
#[derive(Default)]
504-
// FIXME(@Zoxc): this type is entirely unused in rustc
505-
pub struct SyncTypedArena<T> {
506-
lock: MTLock<TypedArena<T>>,
507-
}
508-
509-
impl<T> SyncTypedArena<T> {
510-
#[inline(always)]
511-
pub fn alloc(&self, object: T) -> &mut T {
512-
// Extend the lifetime of the result since it's limited to the lock guard
513-
unsafe { &mut *(self.lock.lock().alloc(object) as *mut T) }
514-
}
515-
516-
#[inline(always)]
517-
pub fn alloc_slice(&self, slice: &[T]) -> &mut [T]
518-
where
519-
T: Copy,
520-
{
521-
// Extend the lifetime of the result since it's limited to the lock guard
522-
unsafe { &mut *(self.lock.lock().alloc_slice(slice) as *mut [T]) }
523-
}
524-
525-
#[inline(always)]
526-
pub fn clear(&mut self) {
527-
self.lock.get_mut().clear();
528-
}
529-
}
530-
531-
#[derive(Default)]
532-
pub struct SyncDroplessArena {
533-
lock: MTLock<DroplessArena>,
534-
}
535-
536-
impl SyncDroplessArena {
537-
#[inline(always)]
538-
pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
539-
self.lock.lock().in_arena(ptr)
540-
}
541-
542-
#[inline(always)]
543-
pub fn alloc_raw(&self, bytes: usize, align: usize) -> &mut [u8] {
544-
// Extend the lifetime of the result since it's limited to the lock guard
545-
unsafe { &mut *(self.lock.lock().alloc_raw(bytes, align) as *mut [u8]) }
546-
}
547-
548-
#[inline(always)]
549-
pub fn alloc<T>(&self, object: T) -> &mut T {
550-
// Extend the lifetime of the result since it's limited to the lock guard
551-
unsafe { &mut *(self.lock.lock().alloc(object) as *mut T) }
552-
}
553-
554-
#[inline(always)]
555-
pub fn alloc_slice<T>(&self, slice: &[T]) -> &mut [T]
556-
where
557-
T: Copy,
558-
{
559-
// Extend the lifetime of the result since it's limited to the lock guard
560-
unsafe { &mut *(self.lock.lock().alloc_slice(slice) as *mut [T]) }
561-
}
562-
}
563-
564491
#[cfg(test)]
565492
mod tests;

src/libcore/cell.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,6 @@
187187
//! ```
188188
//!
189189
190-
// ignore-tidy-undocumented-unsafe
191-
192190
#![stable(feature = "rust1", since = "1.0.0")]
193191

194192
use crate::cmp::Ordering;
@@ -368,6 +366,10 @@ impl<T> Cell<T> {
368366
if ptr::eq(self, other) {
369367
return;
370368
}
369+
// SAFETY: This can be risky if called from separate threads, but `Cell`
370+
// is `!Sync` so this won't happen. This also won't invalidate any
371+
// pointers since `Cell` makes sure nothing else will be pointing into
372+
// either of these `Cell`s.
371373
unsafe {
372374
ptr::swap(self.value.get(), other.value.get());
373375
}
@@ -387,6 +389,8 @@ impl<T> Cell<T> {
387389
/// ```
388390
#[stable(feature = "move_cell", since = "1.17.0")]
389391
pub fn replace(&self, val: T) -> T {
392+
// SAFETY: This can cause data races if called from a separate thread,
393+
// but `Cell` is `!Sync` so this won't happen.
390394
mem::replace(unsafe { &mut *self.value.get() }, val)
391395
}
392396

@@ -423,6 +427,8 @@ impl<T: Copy> Cell<T> {
423427
#[inline]
424428
#[stable(feature = "rust1", since = "1.0.0")]
425429
pub fn get(&self) -> T {
430+
// SAFETY: This can cause data races if called from a separate thread,
431+
// but `Cell` is `!Sync` so this won't happen.
426432
unsafe { *self.value.get() }
427433
}
428434

@@ -491,6 +497,9 @@ impl<T: ?Sized> Cell<T> {
491497
#[inline]
492498
#[stable(feature = "cell_get_mut", since = "1.11.0")]
493499
pub fn get_mut(&mut self) -> &mut T {
500+
// SAFETY: This can cause data races if called from a separate thread,
501+
// but `Cell` is `!Sync` so this won't happen, and `&mut` guarantees
502+
// unique access.
494503
unsafe { &mut *self.value.get() }
495504
}
496505

@@ -510,6 +519,7 @@ impl<T: ?Sized> Cell<T> {
510519
#[inline]
511520
#[stable(feature = "as_cell", since = "1.37.0")]
512521
pub fn from_mut(t: &mut T) -> &Cell<T> {
522+
// SAFETY: `&mut` ensures unique access.
513523
unsafe { &*(t as *mut T as *const Cell<T>) }
514524
}
515525
}
@@ -553,6 +563,7 @@ impl<T> Cell<[T]> {
553563
/// ```
554564
#[stable(feature = "as_cell", since = "1.37.0")]
555565
pub fn as_slice_of_cells(&self) -> &[Cell<T>] {
566+
// SAFETY: `Cell<T>` has the same memory layout as `T`.
556567
unsafe { &*(self as *const Cell<[T]> as *const [Cell<T>]) }
557568
}
558569
}
@@ -816,6 +827,8 @@ impl<T: ?Sized> RefCell<T> {
816827
#[inline]
817828
pub fn try_borrow(&self) -> Result<Ref<'_, T>, BorrowError> {
818829
match BorrowRef::new(&self.borrow) {
830+
// SAFETY: `BorrowRef` ensures that there is only immutable access
831+
// to the value while borrowed.
819832
Some(b) => Ok(Ref { value: unsafe { &*self.value.get() }, borrow: b }),
820833
None => Err(BorrowError { _private: () }),
821834
}
@@ -891,6 +904,7 @@ impl<T: ?Sized> RefCell<T> {
891904
#[inline]
892905
pub fn try_borrow_mut(&self) -> Result<RefMut<'_, T>, BorrowMutError> {
893906
match BorrowRefMut::new(&self.borrow) {
907+
// SAFETY: `BorrowRef` guarantees unique access.
894908
Some(b) => Ok(RefMut { value: unsafe { &mut *self.value.get() }, borrow: b }),
895909
None => Err(BorrowMutError { _private: () }),
896910
}
@@ -940,6 +954,7 @@ impl<T: ?Sized> RefCell<T> {
940954
#[inline]
941955
#[stable(feature = "cell_get_mut", since = "1.11.0")]
942956
pub fn get_mut(&mut self) -> &mut T {
957+
// SAFETY: `&mut` guarantees unique access.
943958
unsafe { &mut *self.value.get() }
944959
}
945960

src/libcore/str/lossy.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::fmt::{self, Write};
33
use crate::mem;
44
use crate::str as core_str;
55

6-
// ignore-tidy-undocumented-unsafe
7-
86
/// Lossy UTF-8 string.
97
#[unstable(feature = "str_internals", issue = "none")]
108
pub struct Utf8Lossy {
@@ -17,6 +15,7 @@ impl Utf8Lossy {
1715
}
1816

1917
pub fn from_bytes(bytes: &[u8]) -> &Utf8Lossy {
18+
// SAFETY: Both use the same memory layout, and UTF-8 correctness isn't required.
2019
unsafe { mem::transmute(bytes) }
2120
}
2221

@@ -60,6 +59,8 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
6059
while i < self.source.len() {
6160
let i_ = i;
6261

62+
// SAFETY: `i` starts at `0`, is less than `self.source.len()`, and
63+
// only increases, so `0 <= i < self.source.len()`.
6364
let byte = unsafe { *self.source.get_unchecked(i) };
6465
i += 1;
6566

@@ -69,6 +70,7 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
6970

7071
macro_rules! error {
7172
() => {{
73+
// SAFETY: We have checked up to `i` that source is valid UTF-8.
7274
unsafe {
7375
let r = Utf8LossyChunk {
7476
valid: core_str::from_utf8_unchecked(&self.source[0..i_]),
@@ -130,6 +132,7 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
130132
}
131133

132134
let r = Utf8LossyChunk {
135+
// SAFETY: We have checked that the entire source is valid UTF-8.
133136
valid: unsafe { core_str::from_utf8_unchecked(self.source) },
134137
broken: &[],
135138
};

0 commit comments

Comments
 (0)