Skip to content

Commit 77b305a

Browse files
committed
Auto merge of rust-lang#77630 - Dylan-DPC:rollup-kfwl55z, r=Dylan-DPC
Rollup of 11 pull requests Successful merges: - rust-lang#76784 (Add some docs to rustdoc::clean::inline and def_id functions) - rust-lang#76911 (fix VecDeque::iter_mut aliasing issues) - rust-lang#77400 (Fix suggestions for x.py setup) - rust-lang#77515 (Update to chalk 0.31) - rust-lang#77568 (inliner: use caller param_env) - rust-lang#77571 (Use matches! for core::char methods) - rust-lang#77582 (Move `EarlyOtherwiseBranch` to mir-opt-level 2) - rust-lang#77590 (Update RLS and Rustfmt) - rust-lang#77605 (Fix rustc_def_path to show the full path and not the trimmed one) - rust-lang#77614 (Let backends access span information) - rust-lang#77624 (Add c as a shorthand check alternative for new options rust-lang#77603) Failed merges: r? `@ghost`
2 parents 2874818 + af9ea35 commit 77b305a

File tree

2 files changed

+65
-47
lines changed

2 files changed

+65
-47
lines changed

alloc/src/collections/vec_deque.rs

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use core::cmp::{self, Ordering};
1414
use core::fmt;
1515
use core::hash::{Hash, Hasher};
1616
use core::iter::{repeat_with, FromIterator, FusedIterator};
17+
use core::marker::PhantomData;
1718
use core::mem::{self, replace, ManuallyDrop};
1819
use core::ops::{Index, IndexMut, Range, RangeBounds, Try};
1920
use core::ptr::{self, NonNull};
@@ -982,7 +983,14 @@ impl<T> VecDeque<T> {
982983
/// ```
983984
#[stable(feature = "rust1", since = "1.0.0")]
984985
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
985-
IterMut { tail: self.tail, head: self.head, ring: unsafe { self.buffer_as_mut_slice() } }
986+
// SAFETY: The internal `IterMut` safety invariant is established because the
987+
// `ring` we create is a dereferencable slice for lifetime '_.
988+
IterMut {
989+
tail: self.tail,
990+
head: self.head,
991+
ring: ptr::slice_from_raw_parts_mut(self.ptr(), self.cap()),
992+
phantom: PhantomData,
993+
}
986994
}
987995

988996
/// Returns a pair of slices which contain, in order, the contents of the
@@ -1170,11 +1178,14 @@ impl<T> VecDeque<T> {
11701178
R: RangeBounds<usize>,
11711179
{
11721180
let (tail, head) = self.range_tail_head(range);
1181+
1182+
// SAFETY: The internal `IterMut` safety invariant is established because the
1183+
// `ring` we create is a dereferencable slice for lifetime '_.
11731184
IterMut {
11741185
tail,
11751186
head,
1176-
// The shared reference we have in &mut self is maintained in the '_ of IterMut.
1177-
ring: unsafe { self.buffer_as_mut_slice() },
1187+
ring: ptr::slice_from_raw_parts_mut(self.ptr(), self.cap()),
1188+
phantom: PhantomData,
11781189
}
11791190
}
11801191

@@ -2493,6 +2504,25 @@ impl<T> RingSlices for &mut [T] {
24932504
}
24942505
}
24952506

2507+
impl<T> RingSlices for *mut [T] {
2508+
fn slice(self, from: usize, to: usize) -> Self {
2509+
assert!(from <= to && to < self.len());
2510+
// Not using `get_unchecked_mut` to keep this a safe operation.
2511+
let len = to - from;
2512+
ptr::slice_from_raw_parts_mut(self.as_mut_ptr().wrapping_add(from), len)
2513+
}
2514+
2515+
fn split_at(self, mid: usize) -> (Self, Self) {
2516+
let len = self.len();
2517+
let ptr = self.as_mut_ptr();
2518+
assert!(mid <= len);
2519+
(
2520+
ptr::slice_from_raw_parts_mut(ptr, mid),
2521+
ptr::slice_from_raw_parts_mut(ptr.wrapping_add(mid), len - mid),
2522+
)
2523+
}
2524+
}
2525+
24962526
/// Calculate the number of elements left to be read in the buffer
24972527
#[inline]
24982528
fn count(tail: usize, head: usize, size: usize) -> usize {
@@ -2662,15 +2692,27 @@ impl<T> FusedIterator for Iter<'_, T> {}
26622692
/// [`iter_mut`]: VecDeque::iter_mut
26632693
#[stable(feature = "rust1", since = "1.0.0")]
26642694
pub struct IterMut<'a, T: 'a> {
2665-
ring: &'a mut [T],
2695+
// Internal safety invariant: the entire slice is dereferencable.
2696+
ring: *mut [T],
26662697
tail: usize,
26672698
head: usize,
2699+
phantom: PhantomData<&'a mut [T]>,
26682700
}
26692701

2702+
// SAFETY: we do nothing thread-local and there is no interior mutability,
2703+
// so the usual structural `Send`/`Sync` apply.
2704+
#[stable(feature = "rust1", since = "1.0.0")]
2705+
unsafe impl<T: Send> Send for IterMut<'_, T> {}
2706+
#[stable(feature = "rust1", since = "1.0.0")]
2707+
unsafe impl<T: Sync> Sync for IterMut<'_, T> {}
2708+
26702709
#[stable(feature = "collection_debug", since = "1.17.0")]
26712710
impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
26722711
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2673-
let (front, back) = RingSlices::ring_slices(&*self.ring, self.head, self.tail);
2712+
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
2713+
// SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
2714+
// The `IterMut` invariant also ensures everything is dereferencable.
2715+
let (front, back) = unsafe { (&*front, &*back) };
26742716
f.debug_tuple("IterMut").field(&front).field(&back).finish()
26752717
}
26762718
}
@@ -2689,7 +2731,7 @@ impl<'a, T> Iterator for IterMut<'a, T> {
26892731

26902732
unsafe {
26912733
let elem = self.ring.get_unchecked_mut(tail);
2692-
Some(&mut *(elem as *mut _))
2734+
Some(&mut *elem)
26932735
}
26942736
}
26952737

@@ -2704,6 +2746,9 @@ impl<'a, T> Iterator for IterMut<'a, T> {
27042746
F: FnMut(Acc, Self::Item) -> Acc,
27052747
{
27062748
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
2749+
// SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
2750+
// The `IterMut` invariant also ensures everything is dereferencable.
2751+
let (front, back) = unsafe { (&mut *front, &mut *back) };
27072752
accum = front.iter_mut().fold(accum, &mut f);
27082753
back.iter_mut().fold(accum, &mut f)
27092754
}
@@ -2735,7 +2780,7 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
27352780

27362781
unsafe {
27372782
let elem = self.ring.get_unchecked_mut(self.head);
2738-
Some(&mut *(elem as *mut _))
2783+
Some(&mut *elem)
27392784
}
27402785
}
27412786

@@ -2744,6 +2789,9 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
27442789
F: FnMut(Acc, Self::Item) -> Acc,
27452790
{
27462791
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
2792+
// SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
2793+
// The `IterMut` invariant also ensures everything is dereferencable.
2794+
let (front, back) = unsafe { (&mut *front, &mut *back) };
27472795
accum = back.iter_mut().rfold(accum, &mut f);
27482796
front.iter_mut().rfold(accum, &mut f)
27492797
}

core/src/char/methods.rs

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,10 +1229,7 @@ impl char {
12291229
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
12301230
#[inline]
12311231
pub const fn is_ascii_alphabetic(&self) -> bool {
1232-
match *self {
1233-
'A'..='Z' | 'a'..='z' => true,
1234-
_ => false,
1235-
}
1232+
matches!(*self, 'A'..='Z' | 'a'..='z')
12361233
}
12371234

12381235
/// Checks if the value is an ASCII uppercase character:
@@ -1265,10 +1262,7 @@ impl char {
12651262
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
12661263
#[inline]
12671264
pub const fn is_ascii_uppercase(&self) -> bool {
1268-
match *self {
1269-
'A'..='Z' => true,
1270-
_ => false,
1271-
}
1265+
matches!(*self, 'A'..='Z')
12721266
}
12731267

12741268
/// Checks if the value is an ASCII lowercase character:
@@ -1301,10 +1295,7 @@ impl char {
13011295
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
13021296
#[inline]
13031297
pub const fn is_ascii_lowercase(&self) -> bool {
1304-
match *self {
1305-
'a'..='z' => true,
1306-
_ => false,
1307-
}
1298+
matches!(*self, 'a'..='z')
13081299
}
13091300

13101301
/// Checks if the value is an ASCII alphanumeric character:
@@ -1340,10 +1331,7 @@ impl char {
13401331
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
13411332
#[inline]
13421333
pub const fn is_ascii_alphanumeric(&self) -> bool {
1343-
match *self {
1344-
'0'..='9' | 'A'..='Z' | 'a'..='z' => true,
1345-
_ => false,
1346-
}
1334+
matches!(*self, '0'..='9' | 'A'..='Z' | 'a'..='z')
13471335
}
13481336

13491337
/// Checks if the value is an ASCII decimal digit:
@@ -1376,10 +1364,7 @@ impl char {
13761364
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
13771365
#[inline]
13781366
pub const fn is_ascii_digit(&self) -> bool {
1379-
match *self {
1380-
'0'..='9' => true,
1381-
_ => false,
1382-
}
1367+
matches!(*self, '0'..='9')
13831368
}
13841369

13851370
/// Checks if the value is an ASCII hexadecimal digit:
@@ -1415,10 +1400,7 @@ impl char {
14151400
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
14161401
#[inline]
14171402
pub const fn is_ascii_hexdigit(&self) -> bool {
1418-
match *self {
1419-
'0'..='9' | 'A'..='F' | 'a'..='f' => true,
1420-
_ => false,
1421-
}
1403+
matches!(*self, '0'..='9' | 'A'..='F' | 'a'..='f')
14221404
}
14231405

14241406
/// Checks if the value is an ASCII punctuation character:
@@ -1455,10 +1437,7 @@ impl char {
14551437
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
14561438
#[inline]
14571439
pub const fn is_ascii_punctuation(&self) -> bool {
1458-
match *self {
1459-
'!'..='/' | ':'..='@' | '['..='`' | '{'..='~' => true,
1460-
_ => false,
1461-
}
1440+
matches!(*self, '!'..='/' | ':'..='@' | '['..='`' | '{'..='~')
14621441
}
14631442

14641443
/// Checks if the value is an ASCII graphic character:
@@ -1491,10 +1470,7 @@ impl char {
14911470
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
14921471
#[inline]
14931472
pub const fn is_ascii_graphic(&self) -> bool {
1494-
match *self {
1495-
'!'..='~' => true,
1496-
_ => false,
1497-
}
1473+
matches!(*self, '!'..='~')
14981474
}
14991475

15001476
/// Checks if the value is an ASCII whitespace character:
@@ -1544,10 +1520,7 @@ impl char {
15441520
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
15451521
#[inline]
15461522
pub const fn is_ascii_whitespace(&self) -> bool {
1547-
match *self {
1548-
'\t' | '\n' | '\x0C' | '\r' | ' ' => true,
1549-
_ => false,
1550-
}
1523+
matches!(*self, '\t' | '\n' | '\x0C' | '\r' | ' ')
15511524
}
15521525

15531526
/// Checks if the value is an ASCII control character:
@@ -1582,10 +1555,7 @@ impl char {
15821555
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
15831556
#[inline]
15841557
pub const fn is_ascii_control(&self) -> bool {
1585-
match *self {
1586-
'\0'..='\x1F' | '\x7F' => true,
1587-
_ => false,
1588-
}
1558+
matches!(*self, '\0'..='\x1F' | '\x7F')
15891559
}
15901560
}
15911561

0 commit comments

Comments
 (0)