Skip to content

Commit d51b860

Browse files
committed
Auto merge of #111248 - Dylan-DPC:rollup-lbp0ui3, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - #103056 (Fix `checked_{add,sub}_duration` incorrectly returning `None` when `other` has more than `i64::MAX` seconds) - #108801 (Implement RFC 3348, `c"foo"` literals) - #110773 (Reduce MIR dump file count for MIR-opt tests) - #110876 (Added default target cpu to `--print target-cpus` output and updated docs) - #111068 (Improve check-cfg implementation) - #111238 (btree_map: `Cursor{,Mut}::peek_prev` must agree) Failed merges: - #110694 (Implement builtin # syntax and use it for offset_of!(...)) r? `@ghost` `@rustbot` modify labels: rollup
2 parents c029b14 + 920b125 commit d51b860

File tree

8 files changed

+61
-27
lines changed

8 files changed

+61
-27
lines changed

alloc/src/collections/btree/map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3079,8 +3079,8 @@ impl<'a, K, V, A> CursorMut<'a, K, V, A> {
30793079
unsafe { self.root.reborrow() }
30803080
.as_mut()?
30813081
.borrow_mut()
3082-
.first_leaf_edge()
3083-
.next_kv()
3082+
.last_leaf_edge()
3083+
.next_back_kv()
30843084
.ok()?
30853085
.into_kv_valmut()
30863086
}

alloc/src/collections/btree/map/tests.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::testing::crash_test::{CrashTestDummy, Panic};
88
use crate::testing::ord_chaos::{Cyclic3, Governed, Governor};
99
use crate::testing::rng::DeterministicRng;
1010
use crate::vec::Vec;
11+
use core::assert_matches::assert_matches;
1112
use std::cmp::Ordering;
1213
use std::iter;
1314
use std::mem;
@@ -2448,3 +2449,21 @@ fn test_cursor_mut_insert_after_4() {
24482449
let mut cur = map.upper_bound_mut(Bound::Included(&2));
24492450
cur.insert_after(4, 'd');
24502451
}
2452+
2453+
#[test]
2454+
fn cursor_peek_prev_agrees_with_cursor_mut() {
2455+
let mut map = BTreeMap::from([(1, 1), (2, 2), (3, 3)]);
2456+
2457+
let cursor = map.lower_bound(Bound::Excluded(&3));
2458+
assert!(cursor.key().is_none());
2459+
2460+
let prev = cursor.peek_prev();
2461+
assert_matches!(prev, Some((&3, _)));
2462+
2463+
// Shadow names so the two parts of this test match.
2464+
let mut cursor = map.lower_bound_mut(Bound::Excluded(&3));
2465+
assert!(cursor.key().is_none());
2466+
2467+
let prev = cursor.peek_prev();
2468+
assert_matches!(prev, Some((&3, _)));
2469+
}

core/src/ffi/c_str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ use crate::str;
7979
///
8080
/// [str]: prim@str "str"
8181
#[derive(Hash)]
82-
#[cfg_attr(not(test), rustc_diagnostic_item = "CStr")]
8382
#[stable(feature = "core_c_str", since = "1.64.0")]
8483
#[rustc_has_incoherent_inherent_impls]
84+
#[cfg_attr(not(bootstrap), lang = "CStr")]
8585
// FIXME:
8686
// `fn from` in `impl From<&CStr> for Box<CStr>` current implementation relies
8787
// on `CStr` being layout-compatible with `[u8]`.

proc_macro/src/bridge/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ pub enum LitKind {
337337
StrRaw(u8),
338338
ByteStr,
339339
ByteStrRaw(u8),
340+
CStr,
341+
CStrRaw(u8),
340342
Err,
341343
}
342344

@@ -350,6 +352,8 @@ rpc_encode_decode!(
350352
StrRaw(n),
351353
ByteStr,
352354
ByteStrRaw(n),
355+
CStr,
356+
CStrRaw(n),
353357
Err,
354358
}
355359
);

std/src/sys/hermit/time.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ impl Timespec {
4040
}
4141

4242
fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
43-
let mut secs = other
44-
.as_secs()
45-
.try_into() // <- target type would be `libc::time_t`
46-
.ok()
47-
.and_then(|secs| self.t.tv_sec.checked_add(secs))?;
43+
let mut secs = self.tv_sec.checked_add_unsigned(other.as_secs())?;
4844

4945
// Nano calculations can't overflow because nanos are <1B which fit
5046
// in a u32.
@@ -57,11 +53,7 @@ impl Timespec {
5753
}
5854

5955
fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
60-
let mut secs = other
61-
.as_secs()
62-
.try_into() // <- target type would be `libc::time_t`
63-
.ok()
64-
.and_then(|secs| self.t.tv_sec.checked_sub(secs))?;
56+
let mut secs = self.tv_sec.checked_sub_unsigned(other.as_secs())?;
6557

6658
// Similar to above, nanos can't overflow.
6759
let mut nsec = self.t.tv_nsec as i32 - other.subsec_nanos() as i32;

std/src/sys/solid/time.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ impl SystemTime {
4747
}
4848

4949
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
50-
Some(SystemTime(self.0.checked_add(other.as_secs().try_into().ok()?)?))
50+
Some(SystemTime(self.0.checked_add_unsigned(other.as_secs())?))
5151
}
5252

5353
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
54-
Some(SystemTime(self.0.checked_sub(other.as_secs().try_into().ok()?)?))
54+
Some(SystemTime(self.0.checked_sub_unsigned(other.as_secs())?))
5555
}
5656
}

std/src/sys/unix/time.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ impl Timespec {
113113
}
114114

115115
pub fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
116-
let mut secs = other
117-
.as_secs()
118-
.try_into() // <- target type would be `i64`
119-
.ok()
120-
.and_then(|secs| self.tv_sec.checked_add(secs))?;
116+
let mut secs = self.tv_sec.checked_add_unsigned(other.as_secs())?;
121117

122118
// Nano calculations can't overflow because nanos are <1B which fit
123119
// in a u32.
@@ -126,23 +122,19 @@ impl Timespec {
126122
nsec -= NSEC_PER_SEC as u32;
127123
secs = secs.checked_add(1)?;
128124
}
129-
Some(Timespec::new(secs, nsec as i64))
125+
Some(Timespec::new(secs, nsec.into()))
130126
}
131127

132128
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
133-
let mut secs = other
134-
.as_secs()
135-
.try_into() // <- target type would be `i64`
136-
.ok()
137-
.and_then(|secs| self.tv_sec.checked_sub(secs))?;
129+
let mut secs = self.tv_sec.checked_sub_unsigned(other.as_secs())?;
138130

139131
// Similar to above, nanos can't overflow.
140132
let mut nsec = self.tv_nsec.0 as i32 - other.subsec_nanos() as i32;
141133
if nsec < 0 {
142134
nsec += NSEC_PER_SEC as i32;
143135
secs = secs.checked_sub(1)?;
144136
}
145-
Some(Timespec::new(secs, nsec as i64))
137+
Some(Timespec::new(secs, nsec.into()))
146138
}
147139

148140
#[allow(dead_code)]

std/src/time/tests.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{Duration, Instant, SystemTime, UNIX_EPOCH};
2+
use core::fmt::Debug;
23
#[cfg(not(target_arch = "wasm32"))]
34
use test::{black_box, Bencher};
45

@@ -201,6 +202,32 @@ fn since_epoch() {
201202
assert!(a < hundred_twenty_years);
202203
}
203204

205+
#[test]
206+
fn big_math() {
207+
// Check that the same result occurs when adding/subtracting each duration one at a time as when
208+
// adding/subtracting them all at once.
209+
#[track_caller]
210+
fn check<T: Eq + Copy + Debug>(start: Option<T>, op: impl Fn(&T, Duration) -> Option<T>) {
211+
const DURATIONS: [Duration; 2] =
212+
[Duration::from_secs(i64::MAX as _), Duration::from_secs(50)];
213+
if let Some(start) = start {
214+
assert_eq!(
215+
op(&start, DURATIONS.into_iter().sum()),
216+
DURATIONS.into_iter().try_fold(start, |t, d| op(&t, d))
217+
)
218+
}
219+
}
220+
221+
check(SystemTime::UNIX_EPOCH.checked_sub(Duration::from_secs(100)), SystemTime::checked_add);
222+
check(SystemTime::UNIX_EPOCH.checked_add(Duration::from_secs(100)), SystemTime::checked_sub);
223+
224+
let instant = Instant::now();
225+
check(instant.checked_sub(Duration::from_secs(100)), Instant::checked_add);
226+
check(instant.checked_sub(Duration::from_secs(i64::MAX as _)), Instant::checked_add);
227+
check(instant.checked_add(Duration::from_secs(100)), Instant::checked_sub);
228+
check(instant.checked_add(Duration::from_secs(i64::MAX as _)), Instant::checked_sub);
229+
}
230+
204231
macro_rules! bench_instant_threaded {
205232
($bench_name:ident, $thread_count:expr) => {
206233
#[bench]

0 commit comments

Comments
 (0)