Skip to content

Commit 5867ca6

Browse files
committed
Auto merge of rust-lang#77102 - Dylan-DPC:rollup-2jfrg3u, r=Dylan-DPC
Rollup of 9 pull requests Successful merges: - rust-lang#76898 (Record `tcx.def_span` instead of `item.span` in crate metadata) - rust-lang#76939 (emit errors during AbstractConst building) - rust-lang#76965 (Add cfg(target_has_atomic_equal_alignment) and use it for Atomic::from_mut.) - rust-lang#76993 (Changing the alloc() to accept &self instead of &mut self) - rust-lang#76994 (fix small typo in docs and comments) - rust-lang#77017 (Add missing examples on Vec iter types) - rust-lang#77042 (Improve documentation for ToSocketAddrs) - rust-lang#77047 (Miri: more informative deallocation error messages) - rust-lang#77055 (Add #[track_caller] to more panicking Cell functions) Failed merges: r? `@ghost`
2 parents 73c6ca2 + 9fa320d commit 5867ca6

File tree

10 files changed

+181
-46
lines changed

10 files changed

+181
-46
lines changed

alloc/src/alloc.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
145145

146146
impl Global {
147147
#[inline]
148-
fn alloc_impl(&mut self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
148+
fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
149149
match layout.size() {
150150
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
151151
// SAFETY: `layout` is non-zero in size,
@@ -160,7 +160,7 @@ impl Global {
160160
// SAFETY: Same as `AllocRef::grow`
161161
#[inline]
162162
unsafe fn grow_impl(
163-
&mut self,
163+
&self,
164164
ptr: NonNull<u8>,
165165
old_layout: Layout,
166166
new_layout: Layout,
@@ -208,17 +208,17 @@ impl Global {
208208
#[unstable(feature = "allocator_api", issue = "32838")]
209209
unsafe impl AllocRef for Global {
210210
#[inline]
211-
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
211+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
212212
self.alloc_impl(layout, false)
213213
}
214214

215215
#[inline]
216-
fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
216+
fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
217217
self.alloc_impl(layout, true)
218218
}
219219

220220
#[inline]
221-
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
221+
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
222222
if layout.size() != 0 {
223223
// SAFETY: `layout` is non-zero in size,
224224
// other conditions must be upheld by the caller
@@ -228,7 +228,7 @@ unsafe impl AllocRef for Global {
228228

229229
#[inline]
230230
unsafe fn grow(
231-
&mut self,
231+
&self,
232232
ptr: NonNull<u8>,
233233
old_layout: Layout,
234234
new_layout: Layout,
@@ -239,7 +239,7 @@ unsafe impl AllocRef for Global {
239239

240240
#[inline]
241241
unsafe fn grow_zeroed(
242-
&mut self,
242+
&self,
243243
ptr: NonNull<u8>,
244244
old_layout: Layout,
245245
new_layout: Layout,
@@ -250,7 +250,7 @@ unsafe impl AllocRef for Global {
250250

251251
#[inline]
252252
unsafe fn shrink(
253-
&mut self,
253+
&self,
254254
ptr: NonNull<u8>,
255255
old_layout: Layout,
256256
new_layout: Layout,

alloc/src/raw_vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
170170
Self::allocate_in(capacity, AllocInit::Zeroed, alloc)
171171
}
172172

173-
fn allocate_in(capacity: usize, init: AllocInit, mut alloc: A) -> Self {
173+
fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self {
174174
if mem::size_of::<T>() == 0 {
175175
Self::new_in(alloc)
176176
} else {

alloc/src/raw_vec/tests.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
use std::cell::Cell;
23

34
#[test]
45
fn allocator_param() {
@@ -17,32 +18,32 @@ fn allocator_param() {
1718
// A dumb allocator that consumes a fixed amount of fuel
1819
// before allocation attempts start failing.
1920
struct BoundedAlloc {
20-
fuel: usize,
21+
fuel: Cell<usize>,
2122
}
2223
unsafe impl AllocRef for BoundedAlloc {
23-
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
24+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
2425
let size = layout.size();
25-
if size > self.fuel {
26+
if size > self.fuel.get() {
2627
return Err(AllocErr);
2728
}
2829
match Global.alloc(layout) {
2930
ok @ Ok(_) => {
30-
self.fuel -= size;
31+
self.fuel.set(self.fuel.get() - size);
3132
ok
3233
}
3334
err @ Err(_) => err,
3435
}
3536
}
36-
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
37+
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
3738
unsafe { Global.dealloc(ptr, layout) }
3839
}
3940
}
4041

41-
let a = BoundedAlloc { fuel: 500 };
42+
let a = BoundedAlloc { fuel: Cell::new(500) };
4243
let mut v: RawVec<u8, _> = RawVec::with_capacity_in(50, a);
43-
assert_eq!(v.alloc.fuel, 450);
44+
assert_eq!(v.alloc.fuel.get(), 450);
4445
v.reserve(50, 150); // (causes a realloc, thus using 50 + 150 = 200 units of fuel)
45-
assert_eq!(v.alloc.fuel, 250);
46+
assert_eq!(v.alloc.fuel.get(), 250);
4647
}
4748

4849
#[test]

alloc/src/vec.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2849,6 +2849,13 @@ where
28492849
///
28502850
/// This `struct` is created by the `into_iter` method on [`Vec`] (provided
28512851
/// by the [`IntoIterator`] trait).
2852+
///
2853+
/// # Example
2854+
///
2855+
/// ```
2856+
/// let v = vec![0, 1, 2];
2857+
/// let iter: std::vec::IntoIter<_> = v.into_iter();
2858+
/// ```
28522859
#[stable(feature = "rust1", since = "1.0.0")]
28532860
pub struct IntoIter<T> {
28542861
buf: NonNull<T>,
@@ -3092,6 +3099,13 @@ impl<T> AsIntoIter for IntoIter<T> {
30923099
///
30933100
/// This `struct` is created by [`Vec::drain`].
30943101
/// See its documentation for more.
3102+
///
3103+
/// # Example
3104+
///
3105+
/// ```
3106+
/// let mut v = vec![0, 1, 2];
3107+
/// let iter: std::vec::Drain<_> = v.drain(..);
3108+
/// ```
30953109
#[stable(feature = "drain", since = "1.6.0")]
30963110
pub struct Drain<'a, T: 'a> {
30973111
/// Index of tail to preserve
@@ -3221,6 +3235,14 @@ impl<T> FusedIterator for Drain<'_, T> {}
32213235
///
32223236
/// This struct is created by [`Vec::splice()`].
32233237
/// See its documentation for more.
3238+
///
3239+
/// # Example
3240+
///
3241+
/// ```
3242+
/// let mut v = vec![0, 1, 2];
3243+
/// let new = [7, 8];
3244+
/// let iter: std::vec::Splice<_> = v.splice(1.., new.iter().cloned());
3245+
/// ```
32243246
#[derive(Debug)]
32253247
#[stable(feature = "vec_splice", since = "1.21.0")]
32263248
pub struct Splice<'a, I: Iterator + 'a> {
@@ -3337,6 +3359,15 @@ impl<T> Drain<'_, T> {
33373359
///
33383360
/// This struct is created by [`Vec::drain_filter`].
33393361
/// See its documentation for more.
3362+
///
3363+
/// # Example
3364+
///
3365+
/// ```
3366+
/// #![feature(drain_filter)]
3367+
///
3368+
/// let mut v = vec![0, 1, 2];
3369+
/// let iter: std::vec::DrainFilter<_, _> = v.drain_filter(|x| *x % 2 == 0);
3370+
/// ```
33403371
#[unstable(feature = "drain_filter", reason = "recently added", issue = "43244")]
33413372
#[derive(Debug)]
33423373
pub struct DrainFilter<'a, T, F>

alloc/tests/heap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn std_heap_overaligned_request() {
1111
check_overalign_requests(Global)
1212
}
1313

14-
fn check_overalign_requests<T: AllocRef>(mut allocator: T) {
14+
fn check_overalign_requests<T: AllocRef>(allocator: T) {
1515
for &align in &[4, 8, 16, 32] {
1616
// less than and bigger than `MIN_ALIGN`
1717
for &size in &[align / 2, align - 1] {

core/src/alloc/mod.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub unsafe trait AllocRef {
109109
/// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
110110
///
111111
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
112-
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr>;
112+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr>;
113113

114114
/// Behaves like `alloc`, but also ensures that the returned memory is zero-initialized.
115115
///
@@ -126,7 +126,7 @@ pub unsafe trait AllocRef {
126126
/// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
127127
///
128128
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
129-
fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
129+
fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
130130
let ptr = self.alloc(layout)?;
131131
// SAFETY: `alloc` returns a valid memory block
132132
unsafe { ptr.as_non_null_ptr().as_ptr().write_bytes(0, ptr.len()) }
@@ -142,7 +142,7 @@ pub unsafe trait AllocRef {
142142
///
143143
/// [*currently allocated*]: #currently-allocated-memory
144144
/// [*fit*]: #memory-fitting
145-
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout);
145+
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout);
146146

147147
/// Attempts to extend the memory block.
148148
///
@@ -183,7 +183,7 @@ pub unsafe trait AllocRef {
183183
///
184184
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
185185
unsafe fn grow(
186-
&mut self,
186+
&self,
187187
ptr: NonNull<u8>,
188188
old_layout: Layout,
189189
new_layout: Layout,
@@ -244,7 +244,7 @@ pub unsafe trait AllocRef {
244244
///
245245
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
246246
unsafe fn grow_zeroed(
247-
&mut self,
247+
&self,
248248
ptr: NonNull<u8>,
249249
old_layout: Layout,
250250
new_layout: Layout,
@@ -308,7 +308,7 @@ pub unsafe trait AllocRef {
308308
///
309309
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
310310
unsafe fn shrink(
311-
&mut self,
311+
&self,
312312
ptr: NonNull<u8>,
313313
old_layout: Layout,
314314
new_layout: Layout,
@@ -337,35 +337,35 @@ pub unsafe trait AllocRef {
337337
///
338338
/// The returned adaptor also implements `AllocRef` and will simply borrow this.
339339
#[inline(always)]
340-
fn by_ref(&mut self) -> &mut Self {
340+
fn by_ref(&mut self) -> &Self {
341341
self
342342
}
343343
}
344344

345345
#[unstable(feature = "allocator_api", issue = "32838")]
346-
unsafe impl<A> AllocRef for &mut A
346+
unsafe impl<A> AllocRef for &A
347347
where
348348
A: AllocRef + ?Sized,
349349
{
350350
#[inline]
351-
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
351+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
352352
(**self).alloc(layout)
353353
}
354354

355355
#[inline]
356-
fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
356+
fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
357357
(**self).alloc_zeroed(layout)
358358
}
359359

360360
#[inline]
361-
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
361+
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
362362
// SAFETY: the safety contract must be upheld by the caller
363363
unsafe { (**self).dealloc(ptr, layout) }
364364
}
365365

366366
#[inline]
367367
unsafe fn grow(
368-
&mut self,
368+
&self,
369369
ptr: NonNull<u8>,
370370
old_layout: Layout,
371371
new_layout: Layout,
@@ -376,7 +376,7 @@ where
376376

377377
#[inline]
378378
unsafe fn grow_zeroed(
379-
&mut self,
379+
&self,
380380
ptr: NonNull<u8>,
381381
old_layout: Layout,
382382
new_layout: Layout,
@@ -387,7 +387,7 @@ where
387387

388388
#[inline]
389389
unsafe fn shrink(
390-
&mut self,
390+
&self,
391391
ptr: NonNull<u8>,
392392
old_layout: Layout,
393393
new_layout: Layout,

core/src/cell.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ impl<T> RefCell<T> {
697697
/// ```
698698
#[inline]
699699
#[stable(feature = "refcell_replace", since = "1.24.0")]
700+
#[track_caller]
700701
pub fn replace(&self, t: T) -> T {
701702
mem::replace(&mut *self.borrow_mut(), t)
702703
}
@@ -719,6 +720,7 @@ impl<T> RefCell<T> {
719720
/// ```
720721
#[inline]
721722
#[stable(feature = "refcell_replace_swap", since = "1.35.0")]
723+
#[track_caller]
722724
pub fn replace_with<F: FnOnce(&mut T) -> T>(&self, f: F) -> T {
723725
let mut_borrow = &mut *self.borrow_mut();
724726
let replacement = f(mut_borrow);
@@ -1052,6 +1054,7 @@ impl<T: Clone> Clone for RefCell<T> {
10521054
///
10531055
/// Panics if the value is currently mutably borrowed.
10541056
#[inline]
1057+
#[track_caller]
10551058
fn clone(&self) -> RefCell<T> {
10561059
RefCell::new(self.borrow().clone())
10571060
}

0 commit comments

Comments
 (0)