Skip to content

Commit 9987f16

Browse files
committed
Auto merge of rust-lang#120843 - matthiaskrgr:rollup-med37z5, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#113671 (Make privacy visitor use types more (instead of HIR)) - rust-lang#120308 (core/time: avoid divisions in Duration::new) - rust-lang#120693 (Invert diagnostic lints.) - rust-lang#120704 (A drive-by rewrite of `give_region_a_name()`) - rust-lang#120809 (Use `transmute_unchecked` in `NonZero::new`.) - rust-lang#120817 (Fix more `ty::Error` ICEs in MIR passes) - rust-lang#120828 (Fix `ErrorGuaranteed` unsoundness with stash/steal.) - rust-lang#120831 (Startup objects disappearing from sysroot) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 816944b + 88dbefe commit 9987f16

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

core/src/num/nonzero.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::intrinsics;
88
use crate::marker::StructuralEq;
99
use crate::marker::StructuralPartialEq;
1010
use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem};
11-
use crate::ptr;
1211
use crate::str::FromStr;
1312

1413
use super::from_str_radix;
@@ -91,13 +90,12 @@ where
9190
/// Creates a non-zero if the given value is not zero.
9291
#[stable(feature = "nonzero", since = "1.28.0")]
9392
#[rustc_const_stable(feature = "const_nonzero_int_methods", since = "1.47.0")]
94-
#[rustc_allow_const_fn_unstable(const_refs_to_cell)]
9593
#[must_use]
9694
#[inline]
9795
pub const fn new(n: T) -> Option<Self> {
9896
// SAFETY: Memory layout optimization guarantees that `Option<NonZero<T>>` has
9997
// the same layout and size as `T`, with `0` representing `None`.
100-
unsafe { ptr::read(ptr::addr_of!(n).cast()) }
98+
unsafe { intrinsics::transmute_unchecked(n) }
10199
}
102100

103101
/// Creates a non-zero without checking whether the value is non-zero.

core/src/time.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,18 @@ impl Duration {
197197
#[must_use]
198198
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
199199
pub const fn new(secs: u64, nanos: u32) -> Duration {
200-
let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) {
201-
Some(secs) => secs,
202-
None => panic!("overflow in Duration::new"),
203-
};
204-
let nanos = nanos % NANOS_PER_SEC;
205-
// SAFETY: nanos % NANOS_PER_SEC < NANOS_PER_SEC, therefore nanos is within the valid range
206-
Duration { secs, nanos: unsafe { Nanoseconds(nanos) } }
200+
if nanos < NANOS_PER_SEC {
201+
// SAFETY: nanos < NANOS_PER_SEC, therefore nanos is within the valid range
202+
Duration { secs, nanos: unsafe { Nanoseconds(nanos) } }
203+
} else {
204+
let secs = match secs.checked_add((nanos / NANOS_PER_SEC) as u64) {
205+
Some(secs) => secs,
206+
None => panic!("overflow in Duration::new"),
207+
};
208+
let nanos = nanos % NANOS_PER_SEC;
209+
// SAFETY: nanos % NANOS_PER_SEC < NANOS_PER_SEC, therefore nanos is within the valid range
210+
Duration { secs, nanos: unsafe { Nanoseconds(nanos) } }
211+
}
207212
}
208213

209214
/// Creates a new `Duration` from the specified number of whole seconds.

0 commit comments

Comments
 (0)