Skip to content

Commit 0b274f0

Browse files
committed
Auto merge of rust-lang#88596 - m-ou-se:rollup-cidzt4v, r=m-ou-se
Rollup of 12 pull requests Successful merges: - rust-lang#88177 (Stabilize std::os::unix::fs::chroot) - rust-lang#88505 (Use `unwrap_unchecked` where possible) - rust-lang#88512 (Upgrade array_into_iter lint to include Deref-to-array types.) - rust-lang#88532 (Remove single use variables) - rust-lang#88543 (Improve closure dummy capture suggestion in macros.) - rust-lang#88560 (`fmt::Formatter::pad`: don't call chars().count() more than one time) - rust-lang#88565 (Add regression test for issue 83190) - rust-lang#88567 (Remove redundant `Span` in `QueryJobInfo`) - rust-lang#88573 (rustdoc: Don't panic on ambiguous inherent associated types) - rust-lang#88582 (Implement rust-lang#88581) - rust-lang#88589 (Correct doc comments inside `use_expr_visitor.rs`) - rust-lang#88592 (Fix ICE in const check) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 794d673 + cb68136 commit 0b274f0

File tree

10 files changed

+372
-25
lines changed

10 files changed

+372
-25
lines changed

alloc/src/collections/linked_list.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,10 @@ impl<T> LinkedList<T> {
300300
let tail = self.tail.take();
301301
let len = mem::replace(&mut self.len, 0);
302302
if let Some(head) = head {
303-
let tail = tail.unwrap_or_else(|| unsafe { core::hint::unreachable_unchecked() });
303+
// SAFETY: In a LinkedList, either both the head and tail are None because
304+
// the list is empty, or both head and tail are Some because the list is populated.
305+
// Since we have verified the head is Some, we are sure the tail is Some too.
306+
let tail = unsafe { tail.unwrap_unchecked() };
304307
Some((head, tail, len))
305308
} else {
306309
None

core/src/array/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,8 @@ where
459459
debug_assert!(N <= iter.size_hint().1.unwrap_or(usize::MAX));
460460
debug_assert!(N <= iter.size_hint().0);
461461

462-
match collect_into_array(iter) {
463-
Some(array) => array,
464-
// SAFETY: covered by the function contract.
465-
None => unsafe { crate::hint::unreachable_unchecked() },
466-
}
462+
// SAFETY: covered by the function contract.
463+
unsafe { collect_into_array(iter).unwrap_unchecked() }
467464
}
468465

469466
/// Pulls `N` items from `iter` and returns them as an array. If the iterator

core/src/fmt/mod.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ impl<'a> Arguments<'a> {
402402

403403
if self.args.is_empty() {
404404
pieces_length
405-
} else if self.pieces[0] == "" && pieces_length < 16 {
405+
} else if !self.pieces.is_empty() && self.pieces[0].is_empty() && pieces_length < 16 {
406406
// If the format string starts with an argument,
407407
// don't preallocate anything, unless length
408408
// of pieces is significant.
@@ -1163,7 +1163,7 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
11631163
}
11641164
// SAFETY: arg and args.args come from the same Arguments,
11651165
// which guarantees the indexes are always within bounds.
1166-
unsafe { run(&mut formatter, arg, &args.args) }?;
1166+
unsafe { run(&mut formatter, arg, args.args) }?;
11671167
idx += 1;
11681168
}
11691169
}
@@ -1409,7 +1409,7 @@ impl<'a> Formatter<'a> {
14091409
// we know that it can't panic. Use `get` + `unwrap_or` to avoid
14101410
// `unsafe` and otherwise don't emit any panic-related code
14111411
// here.
1412-
s.get(..i).unwrap_or(&s)
1412+
s.get(..i).unwrap_or(s)
14131413
} else {
14141414
&s
14151415
}
@@ -1421,16 +1421,21 @@ impl<'a> Formatter<'a> {
14211421
// If we're under the maximum length, and there's no minimum length
14221422
// requirements, then we can just emit the string
14231423
None => self.buf.write_str(s),
1424-
// If we're under the maximum width, check if we're over the minimum
1425-
// width, if so it's as easy as just emitting the string.
1426-
Some(width) if s.chars().count() >= width => self.buf.write_str(s),
1427-
// If we're under both the maximum and the minimum width, then fill
1428-
// up the minimum width with the specified string + some alignment.
14291424
Some(width) => {
1430-
let align = rt::v1::Alignment::Left;
1431-
let post_padding = self.padding(width - s.chars().count(), align)?;
1432-
self.buf.write_str(s)?;
1433-
post_padding.write(self.buf)
1425+
let chars_count = s.chars().count();
1426+
// If we're under the maximum width, check if we're over the minimum
1427+
// width, if so it's as easy as just emitting the string.
1428+
if chars_count >= width {
1429+
self.buf.write_str(s)
1430+
}
1431+
// If we're under both the maximum and the minimum width, then fill
1432+
// up the minimum width with the specified string + some alignment.
1433+
else {
1434+
let align = rt::v1::Alignment::Left;
1435+
let post_padding = self.padding(width - chars_count, align)?;
1436+
self.buf.write_str(s)?;
1437+
post_padding.write(self.buf)
1438+
}
14341439
}
14351440
}
14361441
}

core/src/num/int_macros.rs

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,173 @@ macro_rules! int_impl {
18341834
}
18351835
}
18361836

1837+
/// Calculates the quotient of `self` and `rhs`, rounding the result towards negative infinity.
1838+
///
1839+
/// # Panics
1840+
///
1841+
/// This function will panic if `rhs` is 0 or the division results in overflow.
1842+
///
1843+
/// # Examples
1844+
///
1845+
/// Basic usage:
1846+
///
1847+
/// ```
1848+
/// #![feature(int_roundings)]
1849+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
1850+
/// let b = 3;
1851+
///
1852+
/// assert_eq!(a.div_floor(b), 2);
1853+
/// assert_eq!(a.div_floor(-b), -3);
1854+
/// assert_eq!((-a).div_floor(b), -3);
1855+
/// assert_eq!((-a).div_floor(-b), 2);
1856+
/// ```
1857+
#[unstable(feature = "int_roundings", issue = "88581")]
1858+
#[must_use = "this returns the result of the operation, \
1859+
without modifying the original"]
1860+
#[inline]
1861+
#[rustc_inherit_overflow_checks]
1862+
pub const fn div_floor(self, rhs: Self) -> Self {
1863+
let d = self / rhs;
1864+
let r = self % rhs;
1865+
if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
1866+
d - 1
1867+
} else {
1868+
d
1869+
}
1870+
}
1871+
1872+
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
1873+
///
1874+
/// # Panics
1875+
///
1876+
/// This function will panic if `rhs` is 0 or the division results in overflow.
1877+
///
1878+
/// # Examples
1879+
///
1880+
/// Basic usage:
1881+
///
1882+
/// ```
1883+
/// #![feature(int_roundings)]
1884+
#[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
1885+
/// let b = 3;
1886+
///
1887+
/// assert_eq!(a.div_ceil(b), 3);
1888+
/// assert_eq!(a.div_ceil(-b), -2);
1889+
/// assert_eq!((-a).div_ceil(b), -2);
1890+
/// assert_eq!((-a).div_ceil(-b), 3);
1891+
/// ```
1892+
#[unstable(feature = "int_roundings", issue = "88581")]
1893+
#[must_use = "this returns the result of the operation, \
1894+
without modifying the original"]
1895+
#[inline]
1896+
#[rustc_inherit_overflow_checks]
1897+
pub const fn div_ceil(self, rhs: Self) -> Self {
1898+
let d = self / rhs;
1899+
let r = self % rhs;
1900+
if (r > 0 && rhs > 0) || (r < 0 && rhs < 0) {
1901+
d + 1
1902+
} else {
1903+
d
1904+
}
1905+
}
1906+
1907+
/// If `rhs` is positive, calculates the smallest value greater than or
1908+
/// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
1909+
/// calculates the largest value less than or equal to `self` that is a
1910+
/// multiple of `rhs`.
1911+
///
1912+
/// # Panics
1913+
///
1914+
/// This function will panic if `rhs` is 0 or the operation results in overflow.
1915+
///
1916+
/// # Examples
1917+
///
1918+
/// Basic usage:
1919+
///
1920+
/// ```
1921+
/// #![feature(int_roundings)]
1922+
#[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
1923+
#[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
1924+
#[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
1925+
#[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
1926+
#[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
1927+
#[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
1928+
#[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(-8), -16);")]
1929+
#[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(-8), -24);")]
1930+
/// ```
1931+
#[unstable(feature = "int_roundings", issue = "88581")]
1932+
#[must_use = "this returns the result of the operation, \
1933+
without modifying the original"]
1934+
#[inline]
1935+
#[rustc_inherit_overflow_checks]
1936+
pub const fn next_multiple_of(self, rhs: Self) -> Self {
1937+
// This would otherwise fail when calculating `r` when self == T::MIN.
1938+
if rhs == -1 {
1939+
return self;
1940+
}
1941+
1942+
let r = self % rhs;
1943+
let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
1944+
r + rhs
1945+
} else {
1946+
r
1947+
};
1948+
1949+
if m == 0 {
1950+
self
1951+
} else {
1952+
self + (rhs - m)
1953+
}
1954+
}
1955+
1956+
/// If `rhs` is positive, calculates the smallest value greater than or
1957+
/// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
1958+
/// calculates the largest value less than or equal to `self` that is a
1959+
/// multiple of `rhs`. Returns `None` if `rhs` is zero or the operation
1960+
/// would result in overflow.
1961+
///
1962+
/// # Examples
1963+
///
1964+
/// Basic usage:
1965+
///
1966+
/// ```
1967+
/// #![feature(int_roundings)]
1968+
#[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
1969+
#[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
1970+
#[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
1971+
#[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
1972+
#[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
1973+
#[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
1974+
#[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-16));")]
1975+
#[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-24));")]
1976+
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
1977+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
1978+
/// ```
1979+
#[unstable(feature = "int_roundings", issue = "88581")]
1980+
#[must_use = "this returns the result of the operation, \
1981+
without modifying the original"]
1982+
#[inline]
1983+
#[rustc_inherit_overflow_checks]
1984+
pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
1985+
// This would otherwise fail when calculating `r` when self == T::MIN.
1986+
if rhs == -1 {
1987+
return Some(self);
1988+
}
1989+
1990+
let r = try_opt!(self.checked_rem(rhs));
1991+
let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
1992+
try_opt!(r.checked_add(rhs))
1993+
} else {
1994+
r
1995+
};
1996+
1997+
if m == 0 {
1998+
Some(self)
1999+
} else {
2000+
self.checked_add(try_opt!(rhs.checked_sub(m)))
2001+
}
2002+
}
2003+
18372004
/// Returns the logarithm of the number with respect to an arbitrary base.
18382005
///
18392006
/// This method might not be optimized owing to implementation details;

core/src/num/uint_macros.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,110 @@ macro_rules! uint_impl {
18481848
self % rhs
18491849
}
18501850

1851+
/// Calculates the quotient of `self` and `rhs`, rounding the result towards negative infinity.
1852+
///
1853+
/// This is the same as performing `self / rhs` for all unsigned integers.
1854+
///
1855+
/// # Panics
1856+
///
1857+
/// This function will panic if `rhs` is 0.
1858+
///
1859+
/// # Examples
1860+
///
1861+
/// Basic usage:
1862+
///
1863+
/// ```
1864+
/// #![feature(int_roundings)]
1865+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".div_floor(4), 1);")]
1866+
/// ```
1867+
#[unstable(feature = "int_roundings", issue = "88581")]
1868+
#[inline(always)]
1869+
#[rustc_inherit_overflow_checks]
1870+
pub const fn div_floor(self, rhs: Self) -> Self {
1871+
self / rhs
1872+
}
1873+
1874+
/// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
1875+
///
1876+
/// # Panics
1877+
///
1878+
/// This function will panic if `rhs` is 0.
1879+
///
1880+
/// # Examples
1881+
///
1882+
/// Basic usage:
1883+
///
1884+
/// ```
1885+
/// #![feature(int_roundings)]
1886+
#[doc = concat!("assert_eq!(7_", stringify!($SelfT), ".div_ceil(4), 2);")]
1887+
/// ```
1888+
#[unstable(feature = "int_roundings", issue = "88581")]
1889+
#[inline]
1890+
#[rustc_inherit_overflow_checks]
1891+
pub const fn div_ceil(self, rhs: Self) -> Self {
1892+
let d = self / rhs;
1893+
let r = self % rhs;
1894+
if r > 0 && rhs > 0 {
1895+
d + 1
1896+
} else {
1897+
d
1898+
}
1899+
}
1900+
1901+
/// Calculates the smallest value greater than or equal to `self` that
1902+
/// is a multiple of `rhs`.
1903+
///
1904+
/// # Panics
1905+
///
1906+
/// This function will panic if `rhs` is 0 or the operation results in overflow.
1907+
///
1908+
/// # Examples
1909+
///
1910+
/// Basic usage:
1911+
///
1912+
/// ```
1913+
/// #![feature(int_roundings)]
1914+
#[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
1915+
#[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
1916+
/// ```
1917+
#[unstable(feature = "int_roundings", issue = "88581")]
1918+
#[must_use = "this returns the result of the operation, \
1919+
without modifying the original"]
1920+
#[inline]
1921+
#[rustc_inherit_overflow_checks]
1922+
pub const fn next_multiple_of(self, rhs: Self) -> Self {
1923+
match self % rhs {
1924+
0 => self,
1925+
r => self + (rhs - r)
1926+
}
1927+
}
1928+
1929+
/// Calculates the smallest value greater than or equal to `self` that
1930+
/// is a multiple of `rhs`. If `rhs` is negative,
1931+
///
1932+
/// # Examples
1933+
///
1934+
/// Basic usage:
1935+
///
1936+
/// ```
1937+
/// #![feature(int_roundings)]
1938+
#[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
1939+
#[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
1940+
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
1941+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
1942+
/// ```
1943+
#[unstable(feature = "int_roundings", issue = "88581")]
1944+
#[must_use = "this returns the result of the operation, \
1945+
without modifying the original"]
1946+
#[inline]
1947+
#[rustc_inherit_overflow_checks]
1948+
pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
1949+
match try_opt!(self.checked_rem(rhs)) {
1950+
0 => Some(self),
1951+
r => self.checked_add(try_opt!(rhs.checked_sub(r)))
1952+
}
1953+
}
1954+
18511955
/// Returns `true` if and only if `self == 2^k` for some `k`.
18521956
///
18531957
/// # Examples

core/src/option.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,11 +1198,8 @@ impl<T> Option<T> {
11981198
pub fn insert(&mut self, value: T) -> &mut T {
11991199
*self = Some(value);
12001200

1201-
match self {
1202-
Some(v) => v,
1203-
// SAFETY: the code above just filled the option
1204-
None => unsafe { hint::unreachable_unchecked() },
1205-
}
1201+
// SAFETY: the code above just filled the option
1202+
unsafe { self.as_mut().unwrap_unchecked() }
12061203
}
12071204

12081205
/// Inserts `value` into the option if it is [`None`], then

core/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#![feature(unsized_tuple_coercion)]
6565
#![feature(const_option)]
6666
#![feature(integer_atomics)]
67+
#![feature(int_roundings)]
6768
#![feature(slice_group_by)]
6869
#![feature(trusted_random_access)]
6970
#![feature(unsize)]

0 commit comments

Comments
 (0)