Skip to content

Commit a68d3c6

Browse files
committed
Auto merge of rust-lang#77381 - Dylan-DPC:rollup-0sr6p5p, r=Dylan-DPC
Rollup of 12 pull requests Successful merges: - rust-lang#76909 (Add Iterator::advance_by and DoubleEndedIterator::advance_back_by) - rust-lang#77153 (Fix recursive nonterminal expansion during pretty-print/reparse check) - rust-lang#77202 (Defer Apple SDKROOT detection to link time.) - rust-lang#77303 (const evaluatable: improve `TooGeneric` handling) - rust-lang#77305 (move candidate_from_obligation_no_cache) - rust-lang#77315 (Rename AllocErr to AllocError) - rust-lang#77319 (Stable hashing: add comments and tests concerning platform-independence) - rust-lang#77324 (Don't fire `const_item_mutation` lint on writes through a pointer) - rust-lang#77343 (Validate `rustc_args_required_const`) - rust-lang#77349 (Update cargo) - rust-lang#77360 (References to ZSTs may be at arbitrary aligned addresses) - rust-lang#77371 (Remove trailing space in error message) Failed merges: r? `@ghost`
2 parents 5056e9a + 934e71d commit a68d3c6

File tree

13 files changed

+204
-59
lines changed

13 files changed

+204
-59
lines changed

alloc/src/alloc.rs

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

146146
impl Global {
147147
#[inline]
148-
fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocErr> {
148+
fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> {
149149
match layout.size() {
150150
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
151151
// SAFETY: `layout` is non-zero in size,
152152
size => unsafe {
153153
let raw_ptr = if zeroed { alloc_zeroed(layout) } else { alloc(layout) };
154-
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
154+
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
155155
Ok(NonNull::slice_from_raw_parts(ptr, size))
156156
},
157157
}
@@ -165,7 +165,7 @@ impl Global {
165165
old_layout: Layout,
166166
new_layout: Layout,
167167
zeroed: bool,
168-
) -> Result<NonNull<[u8]>, AllocErr> {
168+
) -> Result<NonNull<[u8]>, AllocError> {
169169
debug_assert!(
170170
new_layout.size() >= old_layout.size(),
171171
"`new_layout.size()` must be greater than or equal to `old_layout.size()`"
@@ -183,7 +183,7 @@ impl Global {
183183
intrinsics::assume(new_size >= old_layout.size());
184184

185185
let raw_ptr = realloc(ptr.as_ptr(), old_layout, new_size);
186-
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
186+
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
187187
if zeroed {
188188
raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
189189
}
@@ -208,12 +208,12 @@ impl Global {
208208
#[unstable(feature = "allocator_api", issue = "32838")]
209209
unsafe impl AllocRef for Global {
210210
#[inline]
211-
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
211+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
212212
self.alloc_impl(layout, false)
213213
}
214214

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

@@ -232,7 +232,7 @@ unsafe impl AllocRef for Global {
232232
ptr: NonNull<u8>,
233233
old_layout: Layout,
234234
new_layout: Layout,
235-
) -> Result<NonNull<[u8]>, AllocErr> {
235+
) -> Result<NonNull<[u8]>, AllocError> {
236236
// SAFETY: all conditions must be upheld by the caller
237237
unsafe { self.grow_impl(ptr, old_layout, new_layout, false) }
238238
}
@@ -243,7 +243,7 @@ unsafe impl AllocRef for Global {
243243
ptr: NonNull<u8>,
244244
old_layout: Layout,
245245
new_layout: Layout,
246-
) -> Result<NonNull<[u8]>, AllocErr> {
246+
) -> Result<NonNull<[u8]>, AllocError> {
247247
// SAFETY: all conditions must be upheld by the caller
248248
unsafe { self.grow_impl(ptr, old_layout, new_layout, true) }
249249
}
@@ -254,7 +254,7 @@ unsafe impl AllocRef for Global {
254254
ptr: NonNull<u8>,
255255
old_layout: Layout,
256256
new_layout: Layout,
257-
) -> Result<NonNull<[u8]>, AllocErr> {
257+
) -> Result<NonNull<[u8]>, AllocError> {
258258
debug_assert!(
259259
new_layout.size() <= old_layout.size(),
260260
"`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
@@ -273,7 +273,7 @@ unsafe impl AllocRef for Global {
273273
intrinsics::assume(new_size <= old_layout.size());
274274

275275
let raw_ptr = realloc(ptr.as_ptr(), old_layout, new_size);
276-
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
276+
let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
277277
Ok(NonNull::slice_from_raw_parts(ptr, new_size))
278278
},
279279

alloc/src/raw_vec/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::cell::Cell;
33

44
#[test]
55
fn allocator_param() {
6-
use crate::alloc::AllocErr;
6+
use crate::alloc::AllocError;
77

88
// Writing a test of integration between third-party
99
// allocators and `RawVec` is a little tricky because the `RawVec`
@@ -21,10 +21,10 @@ fn allocator_param() {
2121
fuel: Cell<usize>,
2222
}
2323
unsafe impl AllocRef for BoundedAlloc {
24-
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
24+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
2525
let size = layout.size();
2626
if size > self.fuel.get() {
27-
return Err(AllocErr);
27+
return Err(AllocError);
2828
}
2929
match Global.alloc(layout) {
3030
ok @ Ok(_) => {

alloc/src/rc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ use core::pin::Pin;
247247
use core::ptr::{self, NonNull};
248248
use core::slice::from_raw_parts_mut;
249249

250-
use crate::alloc::{box_free, handle_alloc_error, AllocErr, AllocRef, Global, Layout};
250+
use crate::alloc::{box_free, handle_alloc_error, AllocError, AllocRef, Global, Layout};
251251
use crate::borrow::{Cow, ToOwned};
252252
use crate::string::String;
253253
use crate::vec::Vec;
@@ -996,7 +996,7 @@ impl<T: ?Sized> Rc<T> {
996996
/// and must return back a (potentially fat)-pointer for the `RcBox<T>`.
997997
unsafe fn allocate_for_layout(
998998
value_layout: Layout,
999-
allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocErr>,
999+
allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocError>,
10001000
mem_to_rcbox: impl FnOnce(*mut u8) -> *mut RcBox<T>,
10011001
) -> *mut RcBox<T> {
10021002
// Calculate layout using the given value layout.

alloc/src/sync.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use core::slice::from_raw_parts_mut;
2121
use core::sync::atomic;
2222
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};
2323

24-
use crate::alloc::{box_free, handle_alloc_error, AllocErr, AllocRef, Global, Layout};
24+
use crate::alloc::{box_free, handle_alloc_error, AllocError, AllocRef, Global, Layout};
2525
use crate::borrow::{Cow, ToOwned};
2626
use crate::boxed::Box;
2727
use crate::rc::is_dangling;
@@ -969,7 +969,7 @@ impl<T: ?Sized> Arc<T> {
969969
/// and must return back a (potentially fat)-pointer for the `ArcInner<T>`.
970970
unsafe fn allocate_for_layout(
971971
value_layout: Layout,
972-
allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocErr>,
972+
allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocError>,
973973
mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner<T>,
974974
) -> *mut ArcInner<T> {
975975
// Calculate layout using the given value layout.

core/src/alloc/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ pub use self::layout::{Layout, LayoutErr};
1313
use crate::fmt;
1414
use crate::ptr::{self, NonNull};
1515

16-
/// The `AllocErr` error indicates an allocation failure
16+
/// The `AllocError` error indicates an allocation failure
1717
/// that may be due to resource exhaustion or to
1818
/// something wrong when combining the given input arguments with this
1919
/// allocator.
2020
#[unstable(feature = "allocator_api", issue = "32838")]
2121
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
22-
pub struct AllocErr;
22+
pub struct AllocError;
2323

2424
// (we need this for downstream impl of trait Error)
2525
#[unstable(feature = "allocator_api", issue = "32838")]
26-
impl fmt::Display for AllocErr {
26+
impl fmt::Display for AllocError {
2727
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2828
f.write_str("memory allocation failed")
2929
}
@@ -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(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr>;
112+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>;
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(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
129+
fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
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()) }
@@ -187,7 +187,7 @@ pub unsafe trait AllocRef {
187187
ptr: NonNull<u8>,
188188
old_layout: Layout,
189189
new_layout: Layout,
190-
) -> Result<NonNull<[u8]>, AllocErr> {
190+
) -> Result<NonNull<[u8]>, AllocError> {
191191
debug_assert!(
192192
new_layout.size() >= old_layout.size(),
193193
"`new_layout.size()` must be greater than or equal to `old_layout.size()`"
@@ -248,7 +248,7 @@ pub unsafe trait AllocRef {
248248
ptr: NonNull<u8>,
249249
old_layout: Layout,
250250
new_layout: Layout,
251-
) -> Result<NonNull<[u8]>, AllocErr> {
251+
) -> Result<NonNull<[u8]>, AllocError> {
252252
debug_assert!(
253253
new_layout.size() >= old_layout.size(),
254254
"`new_layout.size()` must be greater than or equal to `old_layout.size()`"
@@ -312,7 +312,7 @@ pub unsafe trait AllocRef {
312312
ptr: NonNull<u8>,
313313
old_layout: Layout,
314314
new_layout: Layout,
315-
) -> Result<NonNull<[u8]>, AllocErr> {
315+
) -> Result<NonNull<[u8]>, AllocError> {
316316
debug_assert!(
317317
new_layout.size() <= old_layout.size(),
318318
"`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
@@ -348,12 +348,12 @@ where
348348
A: AllocRef + ?Sized,
349349
{
350350
#[inline]
351-
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
351+
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
352352
(**self).alloc(layout)
353353
}
354354

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

@@ -369,7 +369,7 @@ where
369369
ptr: NonNull<u8>,
370370
old_layout: Layout,
371371
new_layout: Layout,
372-
) -> Result<NonNull<[u8]>, AllocErr> {
372+
) -> Result<NonNull<[u8]>, AllocError> {
373373
// SAFETY: the safety contract must be upheld by the caller
374374
unsafe { (**self).grow(ptr, old_layout, new_layout) }
375375
}
@@ -380,7 +380,7 @@ where
380380
ptr: NonNull<u8>,
381381
old_layout: Layout,
382382
new_layout: Layout,
383-
) -> Result<NonNull<[u8]>, AllocErr> {
383+
) -> Result<NonNull<[u8]>, AllocError> {
384384
// SAFETY: the safety contract must be upheld by the caller
385385
unsafe { (**self).grow_zeroed(ptr, old_layout, new_layout) }
386386
}
@@ -391,7 +391,7 @@ where
391391
ptr: NonNull<u8>,
392392
old_layout: Layout,
393393
new_layout: Layout,
394-
) -> Result<NonNull<[u8]>, AllocErr> {
394+
) -> Result<NonNull<[u8]>, AllocError> {
395395
// SAFETY: the safety contract must be upheld by the caller
396396
unsafe { (**self).shrink(ptr, old_layout, new_layout) }
397397
}

core/src/iter/adapters/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ where
124124
self.iter.size_hint()
125125
}
126126

127+
#[inline]
128+
fn advance_by(&mut self, n: usize) -> Result<(), usize> {
129+
self.iter.advance_back_by(n)
130+
}
131+
127132
#[inline]
128133
fn nth(&mut self, n: usize) -> Option<<I as Iterator>::Item> {
129134
self.iter.nth_back(n)
@@ -164,6 +169,11 @@ where
164169
self.iter.next()
165170
}
166171

172+
#[inline]
173+
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
174+
self.iter.advance_by(n)
175+
}
176+
167177
#[inline]
168178
fn nth_back(&mut self, n: usize) -> Option<<I as Iterator>::Item> {
169179
self.iter.nth(n)

core/src/iter/traits/double_ended.rs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,46 @@ pub trait DoubleEndedIterator: Iterator {
9191
#[stable(feature = "rust1", since = "1.0.0")]
9292
fn next_back(&mut self) -> Option<Self::Item>;
9393

94+
/// Advances the iterator from the back by `n` elements.
95+
///
96+
/// `advance_back_by` is the reverse version of [`advance_by`]. This method will
97+
/// eagerly skip `n` elements starting from the back by calling [`next_back`] up
98+
/// to `n` times until [`None`] is encountered.
99+
///
100+
/// `advance_back_by(n)` will return [`Ok(())`] if the iterator successfully advances by
101+
/// `n` elements, or [`Err(k)`] if [`None`] is encountered, where `k` is the number of
102+
/// elements the iterator is advanced by before running out of elements (i.e. the length
103+
/// of the iterator). Note that `k` is always less than `n`.
104+
///
105+
/// Calling `advance_back_by(0)` does not consume any elements and always returns [`Ok(())`].
106+
///
107+
/// [`advance_by`]: Iterator::advance_by
108+
/// [`next_back`]: DoubleEndedIterator::next_back
109+
///
110+
/// # Examples
111+
///
112+
/// Basic usage:
113+
///
114+
/// ```
115+
/// #![feature(iter_advance_by)]
116+
///
117+
/// let a = [3, 4, 5, 6];
118+
/// let mut iter = a.iter();
119+
///
120+
/// assert_eq!(iter.advance_back_by(2), Ok(()));
121+
/// assert_eq!(iter.next_back(), Some(&4));
122+
/// assert_eq!(iter.advance_back_by(0), Ok(()));
123+
/// assert_eq!(iter.advance_back_by(100), Err(1)); // only `&3` was skipped
124+
/// ```
125+
#[inline]
126+
#[unstable(feature = "iter_advance_by", reason = "recently added", issue = "none")]
127+
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
128+
for i in 0..n {
129+
self.next_back().ok_or(i)?;
130+
}
131+
Ok(())
132+
}
133+
94134
/// Returns the `n`th element from the end of the iterator.
95135
///
96136
/// This is essentially the reversed version of [`Iterator::nth()`].
@@ -134,14 +174,9 @@ pub trait DoubleEndedIterator: Iterator {
134174
/// ```
135175
#[inline]
136176
#[stable(feature = "iter_nth_back", since = "1.37.0")]
137-
fn nth_back(&mut self, mut n: usize) -> Option<Self::Item> {
138-
for x in self.rev() {
139-
if n == 0 {
140-
return Some(x);
141-
}
142-
n -= 1;
143-
}
144-
None
177+
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
178+
self.advance_back_by(n).ok()?;
179+
self.next_back()
145180
}
146181

147182
/// This is the reverse version of [`Iterator::try_fold()`]: it takes
@@ -318,6 +353,9 @@ impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I {
318353
fn next_back(&mut self) -> Option<I::Item> {
319354
(**self).next_back()
320355
}
356+
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
357+
(**self).advance_back_by(n)
358+
}
321359
fn nth_back(&mut self, n: usize) -> Option<I::Item> {
322360
(**self).nth_back(n)
323361
}

0 commit comments

Comments
 (0)