Skip to content

Commit b43eb42

Browse files
committed
Auto merge of #62355 - Centril:rollup-xnxtcgm, r=Centril
Rollup of 16 pull requests Successful merges: - #62039 (Remove needless lifetimes (rustc)) - #62173 (rename InterpretCx -> InterpCx) - #62240 (wfcheck: resolve the type-vars in `AdtField` types) - #62249 (Use mem::take instead of mem::replace with default) - #62252 (Update mem::replace example to not be identical to mem::take) - #62258 (syntax: Unsupport `foo! bar { ... }` macros in the parser) - #62268 (Clean up inherent_impls) - #62287 (Use link attributes on extern "C" blocks with llvm-libuwind) - #62295 (miri realloc: do not require giving old size+align) - #62297 (refactor check_for_substitution) - #62316 (When possible without changing semantics, implement Iterator::last in terms of DoubleEndedIterator::next_back for types in liballoc and libcore.) - #62317 (Migrate `compile-pass` annotations to `build-pass`) - #62337 (Fix bucket in CPU usage script) - #62344 (simplify Option::get_or_insert) - #62346 (enable a few more tests in Miri and update the comment for others) - #62351 (remove bogus example from drop_in_place) Failed merges: r? @ghost
2 parents 088b987 + 6363a58 commit b43eb42

File tree

919 files changed

+1638
-1664
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

919 files changed

+1638
-1664
lines changed

src/etc/cpu-usage-over-time-plot.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
set -ex
1818

19-
bucket=rust-lang-ci-evalazure
19+
bucket=rust-lang-ci2
2020
commit=$1
2121
builder=$2
2222

src/liballoc/boxed.rs

+8
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,14 @@ impl<I: Iterator + ?Sized> Iterator for Box<I> {
728728
(**self).nth(n)
729729
}
730730
}
731+
732+
#[stable(feature = "rust1", since = "1.0.0")]
733+
impl<I: Iterator + Sized> Iterator for Box<I> {
734+
fn last(self) -> Option<I::Item> where I: Sized {
735+
(*self).last()
736+
}
737+
}
738+
731739
#[stable(feature = "rust1", since = "1.0.0")]
732740
impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> {
733741
fn next_back(&mut self) -> Option<I::Item> {

src/liballoc/collections/binary_heap.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,11 @@ impl<'a, T> Iterator for Iter<'a, T> {
10351035
fn size_hint(&self) -> (usize, Option<usize>) {
10361036
self.iter.size_hint()
10371037
}
1038+
1039+
#[inline]
1040+
fn last(self) -> Option<&'a T> {
1041+
self.iter.last()
1042+
}
10381043
}
10391044

10401045
#[stable(feature = "rust1", since = "1.0.0")]

src/liballoc/collections/btree/map.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
770770
}
771771

772772
// First, we merge `self` and `other` into a sorted sequence in linear time.
773-
let self_iter = mem::replace(self, BTreeMap::new()).into_iter();
774-
let other_iter = mem::replace(other, BTreeMap::new()).into_iter();
773+
let self_iter = mem::take(self).into_iter();
774+
let other_iter = mem::take(other).into_iter();
775775
let iter = MergeIter {
776776
left: self_iter.peekable(),
777777
right: other_iter.peekable(),
@@ -1193,6 +1193,10 @@ impl<'a, K: 'a, V: 'a> Iterator for Iter<'a, K, V> {
11931193
fn size_hint(&self) -> (usize, Option<usize>) {
11941194
(self.length, Some(self.length))
11951195
}
1196+
1197+
fn last(mut self) -> Option<(&'a K, &'a V)> {
1198+
self.next_back()
1199+
}
11961200
}
11971201

11981202
#[stable(feature = "fused", since = "1.26.0")]
@@ -1253,6 +1257,10 @@ impl<'a, K: 'a, V: 'a> Iterator for IterMut<'a, K, V> {
12531257
fn size_hint(&self) -> (usize, Option<usize>) {
12541258
(self.length, Some(self.length))
12551259
}
1260+
1261+
fn last(mut self) -> Option<(&'a K, &'a mut V)> {
1262+
self.next_back()
1263+
}
12561264
}
12571265

12581266
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1421,6 +1429,10 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
14211429
fn size_hint(&self) -> (usize, Option<usize>) {
14221430
self.inner.size_hint()
14231431
}
1432+
1433+
fn last(mut self) -> Option<&'a K> {
1434+
self.next_back()
1435+
}
14241436
}
14251437

14261438
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1458,6 +1470,10 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
14581470
fn size_hint(&self) -> (usize, Option<usize>) {
14591471
self.inner.size_hint()
14601472
}
1473+
1474+
fn last(mut self) -> Option<&'a V> {
1475+
self.next_back()
1476+
}
14611477
}
14621478

14631479
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1495,6 +1511,10 @@ impl<'a, K, V> Iterator for Range<'a, K, V> {
14951511
unsafe { Some(self.next_unchecked()) }
14961512
}
14971513
}
1514+
1515+
fn last(mut self) -> Option<(&'a K, &'a V)> {
1516+
self.next_back()
1517+
}
14981518
}
14991519

15001520
#[stable(feature = "map_values_mut", since = "1.10.0")]
@@ -1508,6 +1528,10 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
15081528
fn size_hint(&self) -> (usize, Option<usize>) {
15091529
self.inner.size_hint()
15101530
}
1531+
1532+
fn last(mut self) -> Option<&'a mut V> {
1533+
self.next_back()
1534+
}
15111535
}
15121536

15131537
#[stable(feature = "map_values_mut", since = "1.10.0")]
@@ -1626,6 +1650,10 @@ impl<'a, K, V> Iterator for RangeMut<'a, K, V> {
16261650
unsafe { Some(self.next_unchecked()) }
16271651
}
16281652
}
1653+
1654+
fn last(mut self) -> Option<(&'a K, &'a mut V)> {
1655+
self.next_back()
1656+
}
16291657
}
16301658

16311659
impl<'a, K, V> RangeMut<'a, K, V> {

src/liballoc/collections/btree/set.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,9 @@ impl<'a, T> Iterator for Iter<'a, T> {
10191019
fn size_hint(&self) -> (usize, Option<usize>) {
10201020
self.iter.size_hint()
10211021
}
1022+
fn last(mut self) -> Option<&'a T> {
1023+
self.next_back()
1024+
}
10221025
}
10231026
#[stable(feature = "rust1", since = "1.0.0")]
10241027
impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
@@ -1073,6 +1076,10 @@ impl<'a, T> Iterator for Range<'a, T> {
10731076
fn next(&mut self) -> Option<&'a T> {
10741077
self.iter.next().map(|(k, _)| k)
10751078
}
1079+
1080+
fn last(mut self) -> Option<&'a T> {
1081+
self.next_back()
1082+
}
10761083
}
10771084

10781085
#[stable(feature = "btree_range", since = "1.17.0")]

src/liballoc/collections/linked_list.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ impl<T> LinkedList<T> {
708708
let len = self.len();
709709
assert!(at <= len, "Cannot split off at a nonexistent index");
710710
if at == 0 {
711-
return mem::replace(self, Self::new());
711+
return mem::take(self);
712712
} else if at == len {
713713
return Self::new();
714714
}
@@ -832,6 +832,11 @@ impl<'a, T> Iterator for Iter<'a, T> {
832832
fn size_hint(&self) -> (usize, Option<usize>) {
833833
(self.len, Some(self.len))
834834
}
835+
836+
#[inline]
837+
fn last(mut self) -> Option<&'a T> {
838+
self.next_back()
839+
}
835840
}
836841

837842
#[stable(feature = "rust1", since = "1.0.0")]
@@ -881,6 +886,11 @@ impl<'a, T> Iterator for IterMut<'a, T> {
881886
fn size_hint(&self) -> (usize, Option<usize>) {
882887
(self.len, Some(self.len))
883888
}
889+
890+
#[inline]
891+
fn last(mut self) -> Option<&'a mut T> {
892+
self.next_back()
893+
}
884894
}
885895

886896
#[stable(feature = "rust1", since = "1.0.0")]

src/liballoc/collections/vec_deque.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,11 @@ impl<'a, T> Iterator for Iter<'a, T> {
22062206
self.tail = self.head - iter.len();
22072207
final_res
22082208
}
2209+
2210+
#[inline]
2211+
fn last(mut self) -> Option<&'a T> {
2212+
self.next_back()
2213+
}
22092214
}
22102215

22112216
#[stable(feature = "rust1", since = "1.0.0")]
@@ -2319,6 +2324,11 @@ impl<'a, T> Iterator for IterMut<'a, T> {
23192324
accum = front.iter_mut().fold(accum, &mut f);
23202325
back.iter_mut().fold(accum, &mut f)
23212326
}
2327+
2328+
#[inline]
2329+
fn last(mut self) -> Option<&'a mut T> {
2330+
self.next_back()
2331+
}
23222332
}
23232333

23242334
#[stable(feature = "rust1", since = "1.0.0")]

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
#![feature(maybe_uninit_extra, maybe_uninit_slice, maybe_uninit_array)]
113113
#![feature(alloc_layout_extra)]
114114
#![feature(try_trait)]
115+
#![feature(mem_take)]
115116

116117
// Allow testing this library
117118

src/liballoc/str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl ToOwned for str {
203203
}
204204

205205
fn clone_into(&self, target: &mut String) {
206-
let mut b = mem::replace(target, String::new()).into_bytes();
206+
let mut b = mem::take(target).into_bytes();
207207
self.as_bytes().clone_into(&mut b);
208208
*target = unsafe { String::from_utf8_unchecked(b) }
209209
}

src/liballoc/string.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,11 @@ impl Iterator for Drain<'_> {
23852385
fn size_hint(&self) -> (usize, Option<usize>) {
23862386
self.iter.size_hint()
23872387
}
2388+
2389+
#[inline]
2390+
fn last(mut self) -> Option<char> {
2391+
self.next_back()
2392+
}
23882393
}
23892394

23902395
#[stable(feature = "drain", since = "1.6.0")]

src/liballoc/tests/vec.rs

-1
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,6 @@ fn from_into_inner() {
761761
it.next().unwrap();
762762
let vec = it.collect::<Vec<_>>();
763763
assert_eq!(vec, [2, 3]);
764-
#[cfg(not(miri))] // Miri does not support comparing dangling pointers
765764
assert!(ptr != vec.as_ptr());
766765
}
767766

src/libcore/ascii.rs

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ impl Iterator for EscapeDefault {
117117
type Item = u8;
118118
fn next(&mut self) -> Option<u8> { self.range.next().map(|i| self.data[i]) }
119119
fn size_hint(&self) -> (usize, Option<usize>) { self.range.size_hint() }
120+
fn last(mut self) -> Option<u8> { self.next_back() }
120121
}
121122
#[stable(feature = "rust1", since = "1.0.0")]
122123
impl DoubleEndedIterator for EscapeDefault {

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
#![feature(adx_target_feature)]
127127
#![feature(maybe_uninit_slice, maybe_uninit_array)]
128128
#![feature(external_doc)]
129+
#![feature(mem_take)]
129130

130131
#[prelude_import]
131132
#[allow(unused)]

src/libcore/mem/mod.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,12 @@ pub fn swap<T>(x: &mut T, y: &mut T) {
552552
/// mem::take(&mut self.buf)
553553
/// }
554554
/// }
555+
///
556+
/// let mut buffer = Buffer { buf: vec![0, 1] };
557+
/// assert_eq!(buffer.buf.len(), 2);
558+
///
559+
/// assert_eq!(buffer.get_and_reset(), vec![0, 1]);
560+
/// assert_eq!(buffer.buf.len(), 0);
555561
/// ```
556562
///
557563
/// [`Clone`]: ../../std/clone/trait.Clone.html
@@ -586,17 +592,17 @@ pub fn take<T: Default>(dest: &mut T) -> T {
586592
/// struct Buffer<T> { buf: Vec<T> }
587593
///
588594
/// impl<T> Buffer<T> {
589-
/// fn get_and_reset(&mut self) -> Vec<T> {
595+
/// fn replace_index(&mut self, i: usize, v: T) -> T {
590596
/// // error: cannot move out of dereference of `&mut`-pointer
591-
/// let buf = self.buf;
592-
/// self.buf = Vec::new();
593-
/// buf
597+
/// let t = self.buf[i];
598+
/// self.buf[i] = v;
599+
/// t
594600
/// }
595601
/// }
596602
/// ```
597603
///
598-
/// Note that `T` does not necessarily implement [`Clone`], so it can't even clone and reset
599-
/// `self.buf`. But `replace` can be used to disassociate the original value of `self.buf` from
604+
/// Note that `T` does not necessarily implement [`Clone`], so we can't even clone `self.buf[i]` to
605+
/// avoid the move. But `replace` can be used to disassociate the original value at that index from
600606
/// `self`, allowing it to be returned:
601607
///
602608
/// ```
@@ -605,10 +611,16 @@ pub fn take<T: Default>(dest: &mut T) -> T {
605611
///
606612
/// # struct Buffer<T> { buf: Vec<T> }
607613
/// impl<T> Buffer<T> {
608-
/// fn get_and_reset(&mut self) -> Vec<T> {
609-
/// mem::replace(&mut self.buf, Vec::new())
614+
/// fn replace_index(&mut self, i: usize, v: T) -> T {
615+
/// mem::replace(&mut self.buf[i], v)
610616
/// }
611617
/// }
618+
///
619+
/// let mut buffer = Buffer { buf: vec![0, 1] };
620+
/// assert_eq!(buffer.buf[0], 0);
621+
///
622+
/// assert_eq!(buffer.replace_index(0, 2), 0);
623+
/// assert_eq!(buffer.buf[0], 2);
612624
/// ```
613625
///
614626
/// [`Clone`]: ../../std/clone/trait.Clone.html

src/libcore/option.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -777,15 +777,7 @@ impl<T> Option<T> {
777777
#[inline]
778778
#[stable(feature = "option_entry", since = "1.20.0")]
779779
pub fn get_or_insert(&mut self, v: T) -> &mut T {
780-
match *self {
781-
None => *self = Some(v),
782-
_ => (),
783-
}
784-
785-
match *self {
786-
Some(ref mut v) => v,
787-
None => unsafe { hint::unreachable_unchecked() },
788-
}
780+
self.get_or_insert_with(|| v)
789781
}
790782

791783
/// Inserts a value computed from `f` into the option if it is [`None`], then
@@ -845,7 +837,7 @@ impl<T> Option<T> {
845837
#[inline]
846838
#[stable(feature = "rust1", since = "1.0.0")]
847839
pub fn take(&mut self) -> Option<T> {
848-
mem::replace(self, None)
840+
mem::take(self)
849841
}
850842

851843
/// Replaces the actual value in the option by the value given in parameter,

src/libcore/ptr/mod.rs

+5-27
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,19 @@ pub use unique::Unique;
100100
/// as the compiler doesn't need to prove that it's sound to elide the
101101
/// copy.
102102
///
103+
/// Unaligned values cannot be dropped in place, they must be copied to an aligned
104+
/// location first using [`ptr::read_unaligned`].
105+
///
103106
/// [`ptr::read`]: ../ptr/fn.read.html
107+
/// [`ptr::read_unaligned`]: ../ptr/fn.read_unaligned.html
104108
///
105109
/// # Safety
106110
///
107111
/// Behavior is undefined if any of the following conditions are violated:
108112
///
109113
/// * `to_drop` must be [valid] for reads.
110114
///
111-
/// * `to_drop` must be properly aligned. See the example below for how to drop
112-
/// an unaligned pointer.
115+
/// * `to_drop` must be properly aligned.
113116
///
114117
/// Additionally, if `T` is not [`Copy`], using the pointed-to value after
115118
/// calling `drop_in_place` can cause undefined behavior. Note that `*to_drop =
@@ -153,31 +156,6 @@ pub use unique::Unique;
153156
/// assert!(weak.upgrade().is_none());
154157
/// ```
155158
///
156-
/// Unaligned values cannot be dropped in place, they must be copied to an aligned
157-
/// location first:
158-
/// ```
159-
/// use std::ptr;
160-
/// use std::mem::{self, MaybeUninit};
161-
///
162-
/// unsafe fn drop_after_copy<T>(to_drop: *mut T) {
163-
/// let mut copy: MaybeUninit<T> = MaybeUninit::uninit();
164-
/// ptr::copy(to_drop, copy.as_mut_ptr(), 1);
165-
/// drop(copy.assume_init());
166-
/// }
167-
///
168-
/// #[repr(packed, C)]
169-
/// struct Packed {
170-
/// _padding: u8,
171-
/// unaligned: Vec<i32>,
172-
/// }
173-
///
174-
/// let mut p = Packed { _padding: 0, unaligned: vec![42] };
175-
/// unsafe {
176-
/// drop_after_copy(&mut p.unaligned as *mut _);
177-
/// mem::forget(p);
178-
/// }
179-
/// ```
180-
///
181159
/// Notice that the compiler performs this copy automatically when dropping packed structs,
182160
/// i.e., you do not usually have to worry about such issues unless you call `drop_in_place`
183161
/// manually.

0 commit comments

Comments
 (0)