Skip to content

Commit fa65ad2

Browse files
committed
Merge from rustc
2 parents 0666bc4 + 8e986a8 commit fa65ad2

File tree

14 files changed

+228
-54
lines changed

14 files changed

+228
-54
lines changed

core/benches/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod ops;
2020
mod pattern;
2121
mod slice;
2222
mod str;
23+
mod tuple;
2324

2425
/// Returns a `rand::Rng` seeded with a consistent seed.
2526
///

core/benches/tuple.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use rand::prelude::*;
2+
use test::{black_box, Bencher};
3+
4+
#[bench]
5+
fn bench_tuple_comparison(b: &mut Bencher) {
6+
let mut rng = black_box(super::bench_rng());
7+
8+
let data = black_box([
9+
("core::iter::adapters::Chain", 123_usize),
10+
("core::iter::adapters::Clone", 456_usize),
11+
("core::iter::adapters::Copie", 789_usize),
12+
("core::iter::adapters::Cycle", 123_usize),
13+
("core::iter::adapters::Flatt", 456_usize),
14+
("core::iter::adapters::TakeN", 789_usize),
15+
]);
16+
17+
b.iter(|| {
18+
let x = data.choose(&mut rng).unwrap();
19+
let y = data.choose(&mut rng).unwrap();
20+
[x < y, x <= y, x > y, x >= y]
21+
});
22+
}

core/src/intrinsics.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,9 +1585,15 @@ extern "rust-intrinsic" {
15851585

15861586
/// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
15871587
/// if the argument is not an integer.
1588+
///
1589+
/// The stabilized version of this intrinsic is
1590+
/// [`f32::round_ties_even`](../../std/primitive.f32.html#method.round_ties_even)
15881591
pub fn rintf32(x: f32) -> f32;
15891592
/// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
15901593
/// if the argument is not an integer.
1594+
///
1595+
/// The stabilized version of this intrinsic is
1596+
/// [`f64::round_ties_even`](../../std/primitive.f64.html#method.round_ties_even)
15911597
pub fn rintf64(x: f64) -> f64;
15921598

15931599
/// Returns the nearest integer to an `f32`.
@@ -1610,6 +1616,19 @@ extern "rust-intrinsic" {
16101616
/// [`f64::round`](../../std/primitive.f64.html#method.round)
16111617
pub fn roundf64(x: f64) -> f64;
16121618

1619+
/// Returns the nearest integer to an `f32`. Rounds half-way cases to the number
1620+
/// with an even least significant digit.
1621+
///
1622+
/// This intrinsic does not have a stable counterpart.
1623+
#[cfg(not(bootstrap))]
1624+
pub fn roundevenf32(x: f32) -> f32;
1625+
/// Returns the nearest integer to an `f64`. Rounds half-way cases to the number
1626+
/// with an even least significant digit.
1627+
///
1628+
/// This intrinsic does not have a stable counterpart.
1629+
#[cfg(not(bootstrap))]
1630+
pub fn roundevenf64(x: f64) -> f64;
1631+
16131632
/// Float addition that allows optimizations based on algebraic rules.
16141633
/// May assume inputs are finite.
16151634
///

core/src/intrinsics/mir.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
//! are no resume and abort terminators, and terminators that might unwind do not have any way to
228228
//! indicate the unwind block.
229229
//!
230-
//! - [`Goto`], [`Return`], [`Unreachable`], [`Drop`](Drop()), and [`DropAndReplace`] have associated functions.
230+
//! - [`Goto`], [`Return`], [`Unreachable`] and [`Drop`](Drop()) have associated functions.
231231
//! - `match some_int_operand` becomes a `SwitchInt`. Each arm should be `literal => basic_block`
232232
//! - The exception is the last arm, which must be `_ => basic_block` and corresponds to the
233233
//! otherwise branch.
@@ -259,7 +259,6 @@ define!("mir_return", fn Return() -> BasicBlock);
259259
define!("mir_goto", fn Goto(destination: BasicBlock) -> BasicBlock);
260260
define!("mir_unreachable", fn Unreachable() -> BasicBlock);
261261
define!("mir_drop", fn Drop<T>(place: T, goto: BasicBlock));
262-
define!("mir_drop_and_replace", fn DropAndReplace<T>(place: T, value: T, goto: BasicBlock));
263262
define!("mir_call", fn Call<T>(place: T, goto: BasicBlock, call: T));
264263
define!("mir_storage_live", fn StorageLive<T>(local: T));
265264
define!("mir_storage_dead", fn StorageDead<T>(local: T));

core/src/marker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ pub trait StructuralEq {
324324
/// attempt to derive a `Copy` implementation, we'll get an error:
325325
///
326326
/// ```text
327-
/// the trait `Copy` may not be implemented for this type; field `points` does not implement `Copy`
327+
/// the trait `Copy` cannot be implemented for this type; field `points` does not implement `Copy`
328328
/// ```
329329
///
330330
/// Shared references (`&T`) are also `Copy`, so a type can be `Copy`, even when it holds

core/src/slice/index.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use crate::intrinsics::assert_unsafe_precondition;
44
use crate::intrinsics::const_eval_select;
5+
use crate::intrinsics::unchecked_sub;
56
use crate::ops;
67
use crate::ptr;
78

@@ -371,33 +372,34 @@ unsafe impl<T> const SliceIndex<[T]> for ops::Range<usize> {
371372

372373
#[inline]
373374
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
374-
let this = ops::Range { start: self.start, end: self.end };
375+
let this = ops::Range { ..self };
375376
// SAFETY: the caller guarantees that `slice` is not dangling, so it
376377
// cannot be longer than `isize::MAX`. They also guarantee that
377378
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
378-
// so the call to `add` is safe.
379-
379+
// so the call to `add` is safe and the length calculation cannot overflow.
380380
unsafe {
381381
assert_unsafe_precondition!(
382382
"slice::get_unchecked requires that the range is within the slice",
383383
[T](this: ops::Range<usize>, slice: *const [T]) =>
384384
this.end >= this.start && this.end <= slice.len()
385385
);
386-
ptr::slice_from_raw_parts(slice.as_ptr().add(self.start), self.end - self.start)
386+
let new_len = unchecked_sub(self.end, self.start);
387+
ptr::slice_from_raw_parts(slice.as_ptr().add(self.start), new_len)
387388
}
388389
}
389390

390391
#[inline]
391392
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T] {
392-
let this = ops::Range { start: self.start, end: self.end };
393+
let this = ops::Range { ..self };
393394
// SAFETY: see comments for `get_unchecked` above.
394395
unsafe {
395396
assert_unsafe_precondition!(
396397
"slice::get_unchecked_mut requires that the range is within the slice",
397398
[T](this: ops::Range<usize>, slice: *mut [T]) =>
398399
this.end >= this.start && this.end <= slice.len()
399400
);
400-
ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start), self.end - self.start)
401+
let new_len = unchecked_sub(self.end, self.start);
402+
ptr::slice_from_raw_parts_mut(slice.as_mut_ptr().add(self.start), new_len)
401403
}
402404
}
403405

core/src/slice/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1695,7 +1695,13 @@ impl<T> [T] {
16951695
let ptr = self.as_ptr();
16961696

16971697
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
1698-
unsafe { (from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), len - mid)) }
1698+
unsafe {
1699+
assert_unsafe_precondition!(
1700+
"slice::split_at_unchecked requires the index to be within the slice",
1701+
(mid: usize, len: usize) => mid <= len
1702+
);
1703+
(from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), len - mid))
1704+
}
16991705
}
17001706

17011707
/// Divides one mutable slice into two at an index, without doing bounds checking.

core/src/str/traits.rs

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Trait implementations for `str`.
22
33
use crate::cmp::Ordering;
4+
use crate::intrinsics::assert_unsafe_precondition;
45
use crate::ops;
56
use crate::ptr;
67
use crate::slice::SliceIndex;
@@ -194,15 +195,37 @@ unsafe impl const SliceIndex<str> for ops::Range<usize> {
194195
let slice = slice as *const [u8];
195196
// SAFETY: the caller guarantees that `self` is in bounds of `slice`
196197
// which satisfies all the conditions for `add`.
197-
let ptr = unsafe { slice.as_ptr().add(self.start) };
198+
let ptr = unsafe {
199+
let this = ops::Range { ..self };
200+
assert_unsafe_precondition!(
201+
"str::get_unchecked requires that the range is within the string slice",
202+
(this: ops::Range<usize>, slice: *const [u8]) =>
203+
// We'd like to check that the bounds are on char boundaries,
204+
// but there's not really a way to do so without reading
205+
// behind the pointer, which has aliasing implications.
206+
// It's also not possible to move this check up to
207+
// `str::get_unchecked` without adding a special function
208+
// to `SliceIndex` just for this.
209+
this.end >= this.start && this.end <= slice.len()
210+
);
211+
slice.as_ptr().add(self.start)
212+
};
198213
let len = self.end - self.start;
199214
ptr::slice_from_raw_parts(ptr, len) as *const str
200215
}
201216
#[inline]
202217
unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
203218
let slice = slice as *mut [u8];
204219
// SAFETY: see comments for `get_unchecked`.
205-
let ptr = unsafe { slice.as_mut_ptr().add(self.start) };
220+
let ptr = unsafe {
221+
let this = ops::Range { ..self };
222+
assert_unsafe_precondition!(
223+
"str::get_unchecked_mut requires that the range is within the string slice",
224+
(this: ops::Range<usize>, slice: *mut [u8]) =>
225+
this.end >= this.start && this.end <= slice.len()
226+
);
227+
slice.as_mut_ptr().add(self.start)
228+
};
206229
let len = self.end - self.start;
207230
ptr::slice_from_raw_parts_mut(ptr, len) as *mut str
208231
}
@@ -272,15 +295,13 @@ unsafe impl const SliceIndex<str> for ops::RangeTo<usize> {
272295
}
273296
#[inline]
274297
unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
275-
let slice = slice as *const [u8];
276-
let ptr = slice.as_ptr();
277-
ptr::slice_from_raw_parts(ptr, self.end) as *const str
298+
// SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
299+
unsafe { (0..self.end).get_unchecked(slice) }
278300
}
279301
#[inline]
280302
unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
281-
let slice = slice as *mut [u8];
282-
let ptr = slice.as_mut_ptr();
283-
ptr::slice_from_raw_parts_mut(ptr, self.end) as *mut str
303+
// SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
304+
unsafe { (0..self.end).get_unchecked_mut(slice) }
284305
}
285306
#[inline]
286307
fn index(self, slice: &str) -> &Self::Output {
@@ -343,20 +364,15 @@ unsafe impl const SliceIndex<str> for ops::RangeFrom<usize> {
343364
}
344365
#[inline]
345366
unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
346-
let slice = slice as *const [u8];
347-
// SAFETY: the caller guarantees that `self` is in bounds of `slice`
348-
// which satisfies all the conditions for `add`.
349-
let ptr = unsafe { slice.as_ptr().add(self.start) };
350-
let len = slice.len() - self.start;
351-
ptr::slice_from_raw_parts(ptr, len) as *const str
367+
let len = (slice as *const [u8]).len();
368+
// SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
369+
unsafe { (self.start..len).get_unchecked(slice) }
352370
}
353371
#[inline]
354372
unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
355-
let slice = slice as *mut [u8];
356-
// SAFETY: identical to `get_unchecked`.
357-
let ptr = unsafe { slice.as_mut_ptr().add(self.start) };
358-
let len = slice.len() - self.start;
359-
ptr::slice_from_raw_parts_mut(ptr, len) as *mut str
373+
let len = (slice as *mut [u8]).len();
374+
// SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
375+
unsafe { (self.start..len).get_unchecked_mut(slice) }
360376
}
361377
#[inline]
362378
fn index(self, slice: &str) -> &Self::Output {
@@ -452,35 +468,29 @@ unsafe impl const SliceIndex<str> for ops::RangeToInclusive<usize> {
452468
type Output = str;
453469
#[inline]
454470
fn get(self, slice: &str) -> Option<&Self::Output> {
455-
if self.end == usize::MAX { None } else { (..self.end + 1).get(slice) }
471+
(0..=self.end).get(slice)
456472
}
457473
#[inline]
458474
fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
459-
if self.end == usize::MAX { None } else { (..self.end + 1).get_mut(slice) }
475+
(0..=self.end).get_mut(slice)
460476
}
461477
#[inline]
462478
unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output {
463479
// SAFETY: the caller must uphold the safety contract for `get_unchecked`.
464-
unsafe { (..self.end + 1).get_unchecked(slice) }
480+
unsafe { (0..=self.end).get_unchecked(slice) }
465481
}
466482
#[inline]
467483
unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output {
468484
// SAFETY: the caller must uphold the safety contract for `get_unchecked_mut`.
469-
unsafe { (..self.end + 1).get_unchecked_mut(slice) }
485+
unsafe { (0..=self.end).get_unchecked_mut(slice) }
470486
}
471487
#[inline]
472488
fn index(self, slice: &str) -> &Self::Output {
473-
if self.end == usize::MAX {
474-
str_index_overflow_fail();
475-
}
476-
(..self.end + 1).index(slice)
489+
(0..=self.end).index(slice)
477490
}
478491
#[inline]
479492
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
480-
if self.end == usize::MAX {
481-
str_index_overflow_fail();
482-
}
483-
(..self.end + 1).index_mut(slice)
493+
(0..=self.end).index_mut(slice)
484494
}
485495
}
486496

core/src/tuple.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// See src/libstd/primitive_docs.rs for documentation.
22

3-
use crate::cmp::Ordering::*;
4-
use crate::cmp::*;
3+
use crate::cmp::Ordering::{self, *};
4+
use crate::mem::transmute;
55

66
// Recursive macro for implementing n-ary tuple functions and operations
77
//
@@ -61,19 +61,19 @@ macro_rules! tuple_impls {
6161
}
6262
#[inline]
6363
fn lt(&self, other: &($($T,)+)) -> bool {
64-
lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
64+
lexical_ord!(lt, Less, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
6565
}
6666
#[inline]
6767
fn le(&self, other: &($($T,)+)) -> bool {
68-
lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
68+
lexical_ord!(le, Less, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
6969
}
7070
#[inline]
7171
fn ge(&self, other: &($($T,)+)) -> bool {
72-
lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
72+
lexical_ord!(ge, Greater, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
7373
}
7474
#[inline]
7575
fn gt(&self, other: &($($T,)+)) -> bool {
76-
lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
76+
lexical_ord!(gt, Greater, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
7777
}
7878
}
7979
}
@@ -123,16 +123,38 @@ macro_rules! maybe_tuple_doc {
123123
};
124124
}
125125

126-
// Constructs an expression that performs a lexical ordering using method $rel.
126+
#[inline]
127+
const fn ordering_is_some(c: Option<Ordering>, x: Ordering) -> bool {
128+
// FIXME: Just use `==` once that's const-stable on `Option`s.
129+
// This isn't using `match` because that optimizes worse due to
130+
// making a two-step check (`Some` *then* the inner value).
131+
132+
// SAFETY: There's no public guarantee for `Option<Ordering>`,
133+
// but we're core so we know that it's definitely a byte.
134+
unsafe {
135+
let c: i8 = transmute(c);
136+
let x: i8 = transmute(Some(x));
137+
c == x
138+
}
139+
}
140+
141+
// Constructs an expression that performs a lexical ordering using method `$rel`.
127142
// The values are interleaved, so the macro invocation for
128-
// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
129-
// a3, b3)` (and similarly for `lexical_cmp`)
143+
// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, opt_is_lt, a1, b1,
144+
// a2, b2, a3, b3)` (and similarly for `lexical_cmp`)
145+
//
146+
// `$ne_rel` is only used to determine the result after checking that they're
147+
// not equal, so `lt` and `le` can both just use `Less`.
130148
macro_rules! lexical_ord {
131-
($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
132-
if $a != $b { lexical_ord!($rel, $a, $b) }
133-
else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
149+
($rel: ident, $ne_rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {{
150+
let c = PartialOrd::partial_cmp(&$a, &$b);
151+
if !ordering_is_some(c, Equal) { ordering_is_some(c, $ne_rel) }
152+
else { lexical_ord!($rel, $ne_rel, $($rest_a, $rest_b),+) }
153+
}};
154+
($rel: ident, $ne_rel: ident, $a:expr, $b:expr) => {
155+
// Use the specific method for the last element
156+
PartialOrd::$rel(&$a, &$b)
134157
};
135-
($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
136158
}
137159

138160
macro_rules! lexical_partial_cmp {

0 commit comments

Comments
 (0)