Skip to content

Commit 2fd049c

Browse files
committed
Auto merge of #68975 - Dylan-DPC:rollup-jzab8oh, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - #68718 (Move `rustc_hir::def_id` to `rustc_span::def_id`) - #68834 (Fix and test implementation of BTreeMap's first/last_entry, pop_first/last) - #68857 (perf: Reduce Vec allocations in normalization by passing &mut Vec) - #68918 (Don't use the word "unwrap" to describe "unwrap" methods) - #68946 (Mark several functions and methods in core::cmp as #[must_use]) - #68958 (Clean up E0277 and E0282 explanations) - #68960 (codegen: misc cleanups around debuginfo scopes and locations.) Failed merges: r? @ghost
2 parents a19edd6 + 9dabf80 commit 2fd049c

File tree

25 files changed

+310
-307
lines changed

25 files changed

+310
-307
lines changed

src/liballoc/collections/btree/map.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -675,13 +675,15 @@ impl<K: Ord, V> BTreeMap<K, V> {
675675
T: Ord,
676676
K: Borrow<T>,
677677
{
678-
match self.length {
679-
0 => None,
680-
_ => Some(OccupiedEntry {
681-
handle: self.root.as_mut().first_kv(),
678+
let front = self.root.as_mut().first_leaf_edge();
679+
if let Ok(kv) = front.right_kv() {
680+
Some(OccupiedEntry {
681+
handle: kv.forget_node_type(),
682682
length: &mut self.length,
683683
_marker: PhantomData,
684-
}),
684+
})
685+
} else {
686+
None
685687
}
686688
}
687689

@@ -736,13 +738,15 @@ impl<K: Ord, V> BTreeMap<K, V> {
736738
T: Ord,
737739
K: Borrow<T>,
738740
{
739-
match self.length {
740-
0 => None,
741-
_ => Some(OccupiedEntry {
742-
handle: self.root.as_mut().last_kv(),
741+
let back = self.root.as_mut().last_leaf_edge();
742+
if let Ok(kv) = back.left_kv() {
743+
Some(OccupiedEntry {
744+
handle: kv.forget_node_type(),
743745
length: &mut self.length,
744746
_marker: PhantomData,
745-
}),
747+
})
748+
} else {
749+
None
746750
}
747751
}
748752

src/liballoc/tests/btree/map.rs

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ fn test_basic_large() {
2323
assert_eq!(map.len(), i + 1);
2424
}
2525

26+
assert_eq!(map.first_key_value(), Some((&0, &0)));
27+
assert_eq!(map.last_key_value(), Some((&(size - 1), &(10 * (size - 1)))));
28+
assert_eq!(map.first_entry().unwrap().key(), &0);
29+
assert_eq!(map.last_entry().unwrap().key(), &(size - 1));
30+
2631
for i in 0..size {
2732
assert_eq!(map.get(&i).unwrap(), &(i * 10));
2833
}

src/liballoc/tests/btree/set.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -487,21 +487,26 @@ fn test_first_last() {
487487
a.insert(2);
488488
assert_eq!(a.first(), Some(&1));
489489
assert_eq!(a.last(), Some(&2));
490-
a.insert(3);
490+
for i in 3..=12 {
491+
a.insert(i);
492+
}
491493
assert_eq!(a.first(), Some(&1));
492-
assert_eq!(a.last(), Some(&3));
493-
494-
assert_eq!(a.len(), 3);
494+
assert_eq!(a.last(), Some(&12));
495495
assert_eq!(a.pop_first(), Some(1));
496-
assert_eq!(a.len(), 2);
497-
assert_eq!(a.pop_last(), Some(3));
498-
assert_eq!(a.len(), 1);
496+
assert_eq!(a.pop_last(), Some(12));
499497
assert_eq!(a.pop_first(), Some(2));
500-
assert_eq!(a.len(), 0);
501-
assert_eq!(a.pop_last(), None);
502-
assert_eq!(a.len(), 0);
498+
assert_eq!(a.pop_last(), Some(11));
499+
assert_eq!(a.pop_first(), Some(3));
500+
assert_eq!(a.pop_last(), Some(10));
501+
assert_eq!(a.pop_first(), Some(4));
502+
assert_eq!(a.pop_first(), Some(5));
503+
assert_eq!(a.pop_first(), Some(6));
504+
assert_eq!(a.pop_first(), Some(7));
505+
assert_eq!(a.pop_first(), Some(8));
506+
assert_eq!(a.clone().pop_last(), Some(9));
507+
assert_eq!(a.pop_first(), Some(9));
503508
assert_eq!(a.pop_first(), None);
504-
assert_eq!(a.len(), 0);
509+
assert_eq!(a.pop_last(), None);
505510
}
506511

507512
fn rand_data(len: usize) -> Vec<u32> {

src/libcore/cmp.rs

+13
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ impl Ordering {
361361
/// assert!(data == b);
362362
/// ```
363363
#[inline]
364+
#[must_use]
364365
#[stable(feature = "rust1", since = "1.0.0")]
365366
pub fn reverse(self) -> Ordering {
366367
match self {
@@ -398,6 +399,7 @@ impl Ordering {
398399
/// assert_eq!(result, Ordering::Less);
399400
/// ```
400401
#[inline]
402+
#[must_use]
401403
#[stable(feature = "ordering_chaining", since = "1.17.0")]
402404
pub fn then(self, other: Ordering) -> Ordering {
403405
match self {
@@ -435,6 +437,7 @@ impl Ordering {
435437
/// assert_eq!(result, Ordering::Less);
436438
/// ```
437439
#[inline]
440+
#[must_use]
438441
#[stable(feature = "ordering_chaining", since = "1.17.0")]
439442
pub fn then_with<F: FnOnce() -> Ordering>(self, f: F) -> Ordering {
440443
match self {
@@ -576,6 +579,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
576579
/// assert_eq!(10.cmp(&5), Ordering::Greater);
577580
/// assert_eq!(5.cmp(&5), Ordering::Equal);
578581
/// ```
582+
#[must_use]
579583
#[stable(feature = "rust1", since = "1.0.0")]
580584
fn cmp(&self, other: &Self) -> Ordering;
581585

@@ -591,6 +595,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
591595
/// ```
592596
#[stable(feature = "ord_max_min", since = "1.21.0")]
593597
#[inline]
598+
#[must_use]
594599
fn max(self, other: Self) -> Self
595600
where
596601
Self: Sized,
@@ -610,6 +615,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
610615
/// ```
611616
#[stable(feature = "ord_max_min", since = "1.21.0")]
612617
#[inline]
618+
#[must_use]
613619
fn min(self, other: Self) -> Self
614620
where
615621
Self: Sized,
@@ -635,6 +641,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
635641
/// assert!(0.clamp(-2, 1) == 0);
636642
/// assert!(2.clamp(-2, 1) == 1);
637643
/// ```
644+
#[must_use]
638645
#[unstable(feature = "clamp", issue = "44095")]
639646
fn clamp(self, min: Self, max: Self) -> Self
640647
where
@@ -915,6 +922,7 @@ pub macro PartialOrd($item:item) {
915922
/// assert_eq!(2, cmp::min(2, 2));
916923
/// ```
917924
#[inline]
925+
#[must_use]
918926
#[stable(feature = "rust1", since = "1.0.0")]
919927
pub fn min<T: Ord>(v1: T, v2: T) -> T {
920928
v1.min(v2)
@@ -935,6 +943,7 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
935943
/// assert_eq!(cmp::min_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), -2);
936944
/// ```
937945
#[inline]
946+
#[must_use]
938947
#[unstable(feature = "cmp_min_max_by", issue = "64460")]
939948
pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
940949
match compare(&v1, &v2) {
@@ -958,6 +967,7 @@ pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
958967
/// assert_eq!(cmp::min_by_key(-2, 2, |x: &i32| x.abs()), -2);
959968
/// ```
960969
#[inline]
970+
#[must_use]
961971
#[unstable(feature = "cmp_min_max_by", issue = "64460")]
962972
pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
963973
min_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
@@ -978,6 +988,7 @@ pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
978988
/// assert_eq!(2, cmp::max(2, 2));
979989
/// ```
980990
#[inline]
991+
#[must_use]
981992
#[stable(feature = "rust1", since = "1.0.0")]
982993
pub fn max<T: Ord>(v1: T, v2: T) -> T {
983994
v1.max(v2)
@@ -998,6 +1009,7 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
9981009
/// assert_eq!(cmp::max_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), 2);
9991010
/// ```
10001011
#[inline]
1012+
#[must_use]
10011013
#[unstable(feature = "cmp_min_max_by", issue = "64460")]
10021014
pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
10031015
match compare(&v1, &v2) {
@@ -1021,6 +1033,7 @@ pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
10211033
/// assert_eq!(cmp::max_by_key(-2, 2, |x: &i32| x.abs()), 2);
10221034
/// ```
10231035
#[inline]
1036+
#[must_use]
10241037
#[unstable(feature = "cmp_min_max_by", issue = "64460")]
10251038
pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
10261039
max_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))

src/libcore/option.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl<T> Option<T> {
317317
// Getting to contained values
318318
/////////////////////////////////////////////////////////////////////////
319319

320-
/// Unwraps an option, yielding the content of a [`Some`].
320+
/// Returns the contained [`Some`] value, consuming the `self` value.
321321
///
322322
/// # Panics
323323
///
@@ -348,17 +348,22 @@ impl<T> Option<T> {
348348
}
349349
}
350350

351-
/// Moves the value `v` out of the `Option<T>` if it is [`Some(v)`].
351+
/// Returns the contained [`Some`] value, consuming the `self` value.
352352
///
353-
/// In general, because this function may panic, its use is discouraged.
353+
/// Because this function may panic, its use is generally discouraged.
354354
/// Instead, prefer to use pattern matching and handle the [`None`]
355-
/// case explicitly.
355+
/// case explicitly, or call [`unwrap_or`], [`unwrap_or_else`], or
356+
/// [`unwrap_or_default`].
357+
///
358+
/// [`unwrap_or`]: #method.unwrap_or
359+
/// [`unwrap_or_else`]: #method.unwrap_or_else
360+
/// [`unwrap_or_default`]: #method.unwrap_or_default
356361
///
357362
/// # Panics
358363
///
359364
/// Panics if the self value equals [`None`].
360365
///
361-
/// [`Some(v)`]: #variant.Some
366+
/// [`Some`]: #variant.Some
362367
/// [`None`]: #variant.None
363368
///
364369
/// # Examples
@@ -382,12 +387,13 @@ impl<T> Option<T> {
382387
}
383388
}
384389

385-
/// Returns the contained value or a default.
390+
/// Returns the contained [`Some`] value or a provided default.
386391
///
387392
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
388393
/// the result of a function call, it is recommended to use [`unwrap_or_else`],
389394
/// which is lazily evaluated.
390395
///
396+
/// [`Some`]: #variant.Some
391397
/// [`unwrap_or_else`]: #method.unwrap_or_else
392398
///
393399
/// # Examples
@@ -405,7 +411,7 @@ impl<T> Option<T> {
405411
}
406412
}
407413

408-
/// Returns the contained value or computes it from a closure.
414+
/// Returns the contained [`Some`] value or computes it from a closure.
409415
///
410416
/// # Examples
411417
///
@@ -986,7 +992,7 @@ impl<T: Clone> Option<&mut T> {
986992
}
987993

988994
impl<T: fmt::Debug> Option<T> {
989-
/// Unwraps an option, expecting [`None`] and returning nothing.
995+
/// Consumes `self` while expecting [`None`] and returning nothing.
990996
///
991997
/// # Panics
992998
///
@@ -1029,7 +1035,7 @@ impl<T: fmt::Debug> Option<T> {
10291035
}
10301036
}
10311037

1032-
/// Unwraps an option, expecting [`None`] and returning nothing.
1038+
/// Consumes `self` while expecting [`None`] and returning nothing.
10331039
///
10341040
/// # Panics
10351041
///
@@ -1074,7 +1080,7 @@ impl<T: fmt::Debug> Option<T> {
10741080
}
10751081

10761082
impl<T: Default> Option<T> {
1077-
/// Returns the contained value or a default
1083+
/// Returns the contained [`Some`] value or a default
10781084
///
10791085
/// Consumes the `self` argument then, if [`Some`], returns the contained
10801086
/// value, otherwise if [`None`], returns the [default value] for that

0 commit comments

Comments
 (0)