Skip to content

Commit

Permalink
Auto merge of #68975 - Dylan-DPC:rollup-jzab8oh, r=Dylan-DPC
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Feb 9, 2020
2 parents 64ea639 + 9dabf80 commit 6dff769
Show file tree
Hide file tree
Showing 25 changed files with 310 additions and 307 deletions.
24 changes: 14 additions & 10 deletions src/liballoc/collections/btree/map.rs
Expand Up @@ -675,13 +675,15 @@ impl<K: Ord, V> BTreeMap<K, V> {
T: Ord,
K: Borrow<T>,
{
match self.length {
0 => None,
_ => Some(OccupiedEntry {
handle: self.root.as_mut().first_kv(),
let front = self.root.as_mut().first_leaf_edge();
if let Ok(kv) = front.right_kv() {
Some(OccupiedEntry {
handle: kv.forget_node_type(),
length: &mut self.length,
_marker: PhantomData,
}),
})
} else {
None
}
}

Expand Down Expand Up @@ -736,13 +738,15 @@ impl<K: Ord, V> BTreeMap<K, V> {
T: Ord,
K: Borrow<T>,
{
match self.length {
0 => None,
_ => Some(OccupiedEntry {
handle: self.root.as_mut().last_kv(),
let back = self.root.as_mut().last_leaf_edge();
if let Ok(kv) = back.left_kv() {
Some(OccupiedEntry {
handle: kv.forget_node_type(),
length: &mut self.length,
_marker: PhantomData,
}),
})
} else {
None
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/liballoc/tests/btree/map.rs
Expand Up @@ -23,6 +23,11 @@ fn test_basic_large() {
assert_eq!(map.len(), i + 1);
}

assert_eq!(map.first_key_value(), Some((&0, &0)));
assert_eq!(map.last_key_value(), Some((&(size - 1), &(10 * (size - 1)))));
assert_eq!(map.first_entry().unwrap().key(), &0);
assert_eq!(map.last_entry().unwrap().key(), &(size - 1));

for i in 0..size {
assert_eq!(map.get(&i).unwrap(), &(i * 10));
}
Expand Down
27 changes: 16 additions & 11 deletions src/liballoc/tests/btree/set.rs
Expand Up @@ -487,21 +487,26 @@ fn test_first_last() {
a.insert(2);
assert_eq!(a.first(), Some(&1));
assert_eq!(a.last(), Some(&2));
a.insert(3);
for i in 3..=12 {
a.insert(i);
}
assert_eq!(a.first(), Some(&1));
assert_eq!(a.last(), Some(&3));

assert_eq!(a.len(), 3);
assert_eq!(a.last(), Some(&12));
assert_eq!(a.pop_first(), Some(1));
assert_eq!(a.len(), 2);
assert_eq!(a.pop_last(), Some(3));
assert_eq!(a.len(), 1);
assert_eq!(a.pop_last(), Some(12));
assert_eq!(a.pop_first(), Some(2));
assert_eq!(a.len(), 0);
assert_eq!(a.pop_last(), None);
assert_eq!(a.len(), 0);
assert_eq!(a.pop_last(), Some(11));
assert_eq!(a.pop_first(), Some(3));
assert_eq!(a.pop_last(), Some(10));
assert_eq!(a.pop_first(), Some(4));
assert_eq!(a.pop_first(), Some(5));
assert_eq!(a.pop_first(), Some(6));
assert_eq!(a.pop_first(), Some(7));
assert_eq!(a.pop_first(), Some(8));
assert_eq!(a.clone().pop_last(), Some(9));
assert_eq!(a.pop_first(), Some(9));
assert_eq!(a.pop_first(), None);
assert_eq!(a.len(), 0);
assert_eq!(a.pop_last(), None);
}

fn rand_data(len: usize) -> Vec<u32> {
Expand Down
13 changes: 13 additions & 0 deletions src/libcore/cmp.rs
Expand Up @@ -361,6 +361,7 @@ impl Ordering {
/// assert!(data == b);
/// ```
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn reverse(self) -> Ordering {
match self {
Expand Down Expand Up @@ -398,6 +399,7 @@ impl Ordering {
/// assert_eq!(result, Ordering::Less);
/// ```
#[inline]
#[must_use]
#[stable(feature = "ordering_chaining", since = "1.17.0")]
pub fn then(self, other: Ordering) -> Ordering {
match self {
Expand Down Expand Up @@ -435,6 +437,7 @@ impl Ordering {
/// assert_eq!(result, Ordering::Less);
/// ```
#[inline]
#[must_use]
#[stable(feature = "ordering_chaining", since = "1.17.0")]
pub fn then_with<F: FnOnce() -> Ordering>(self, f: F) -> Ordering {
match self {
Expand Down Expand Up @@ -576,6 +579,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
/// assert_eq!(10.cmp(&5), Ordering::Greater);
/// assert_eq!(5.cmp(&5), Ordering::Equal);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn cmp(&self, other: &Self) -> Ordering;

Expand All @@ -591,6 +595,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
/// ```
#[stable(feature = "ord_max_min", since = "1.21.0")]
#[inline]
#[must_use]
fn max(self, other: Self) -> Self
where
Self: Sized,
Expand All @@ -610,6 +615,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
/// ```
#[stable(feature = "ord_max_min", since = "1.21.0")]
#[inline]
#[must_use]
fn min(self, other: Self) -> Self
where
Self: Sized,
Expand All @@ -635,6 +641,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
/// assert!(0.clamp(-2, 1) == 0);
/// assert!(2.clamp(-2, 1) == 1);
/// ```
#[must_use]
#[unstable(feature = "clamp", issue = "44095")]
fn clamp(self, min: Self, max: Self) -> Self
where
Expand Down Expand Up @@ -915,6 +922,7 @@ pub macro PartialOrd($item:item) {
/// assert_eq!(2, cmp::min(2, 2));
/// ```
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn min<T: Ord>(v1: T, v2: T) -> T {
v1.min(v2)
Expand All @@ -935,6 +943,7 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
/// assert_eq!(cmp::min_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), -2);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "cmp_min_max_by", issue = "64460")]
pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
match compare(&v1, &v2) {
Expand All @@ -958,6 +967,7 @@ pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
/// assert_eq!(cmp::min_by_key(-2, 2, |x: &i32| x.abs()), -2);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "cmp_min_max_by", issue = "64460")]
pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
min_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
Expand All @@ -978,6 +988,7 @@ pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
/// assert_eq!(2, cmp::max(2, 2));
/// ```
#[inline]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn max<T: Ord>(v1: T, v2: T) -> T {
v1.max(v2)
Expand All @@ -998,6 +1009,7 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
/// assert_eq!(cmp::max_by(-2, 2, |x: &i32, y: &i32| x.abs().cmp(&y.abs())), 2);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "cmp_min_max_by", issue = "64460")]
pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
match compare(&v1, &v2) {
Expand All @@ -1021,6 +1033,7 @@ pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
/// assert_eq!(cmp::max_by_key(-2, 2, |x: &i32| x.abs()), 2);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "cmp_min_max_by", issue = "64460")]
pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
max_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
Expand Down
26 changes: 16 additions & 10 deletions src/libcore/option.rs
Expand Up @@ -317,7 +317,7 @@ impl<T> Option<T> {
// Getting to contained values
/////////////////////////////////////////////////////////////////////////

/// Unwraps an option, yielding the content of a [`Some`].
/// Returns the contained [`Some`] value, consuming the `self` value.
///
/// # Panics
///
Expand Down Expand Up @@ -348,17 +348,22 @@ impl<T> Option<T> {
}
}

/// Moves the value `v` out of the `Option<T>` if it is [`Some(v)`].
/// Returns the contained [`Some`] value, consuming the `self` value.
///
/// In general, because this function may panic, its use is discouraged.
/// Because this function may panic, its use is generally discouraged.
/// Instead, prefer to use pattern matching and handle the [`None`]
/// case explicitly.
/// case explicitly, or call [`unwrap_or`], [`unwrap_or_else`], or
/// [`unwrap_or_default`].
///
/// [`unwrap_or`]: #method.unwrap_or
/// [`unwrap_or_else`]: #method.unwrap_or_else
/// [`unwrap_or_default`]: #method.unwrap_or_default
///
/// # Panics
///
/// Panics if the self value equals [`None`].
///
/// [`Some(v)`]: #variant.Some
/// [`Some`]: #variant.Some
/// [`None`]: #variant.None
///
/// # Examples
Expand All @@ -382,12 +387,13 @@ impl<T> Option<T> {
}
}

/// Returns the contained value or a default.
/// Returns the contained [`Some`] value or a provided default.
///
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
/// the result of a function call, it is recommended to use [`unwrap_or_else`],
/// which is lazily evaluated.
///
/// [`Some`]: #variant.Some
/// [`unwrap_or_else`]: #method.unwrap_or_else
///
/// # Examples
Expand All @@ -405,7 +411,7 @@ impl<T> Option<T> {
}
}

/// Returns the contained value or computes it from a closure.
/// Returns the contained [`Some`] value or computes it from a closure.
///
/// # Examples
///
Expand Down Expand Up @@ -986,7 +992,7 @@ impl<T: Clone> Option<&mut T> {
}

impl<T: fmt::Debug> Option<T> {
/// Unwraps an option, expecting [`None`] and returning nothing.
/// Consumes `self` while expecting [`None`] and returning nothing.
///
/// # Panics
///
Expand Down Expand Up @@ -1029,7 +1035,7 @@ impl<T: fmt::Debug> Option<T> {
}
}

/// Unwraps an option, expecting [`None`] and returning nothing.
/// Consumes `self` while expecting [`None`] and returning nothing.
///
/// # Panics
///
Expand Down Expand Up @@ -1074,7 +1080,7 @@ impl<T: fmt::Debug> Option<T> {
}

impl<T: Default> Option<T> {
/// Returns the contained value or a default
/// Returns the contained [`Some`] value or a default
///
/// Consumes the `self` argument then, if [`Some`], returns the contained
/// value, otherwise if [`None`], returns the [default value] for that
Expand Down

0 comments on commit 6dff769

Please sign in to comment.