Skip to content

Commit 92c32d2

Browse files
committed
Auto merge of #47308 - frewsxcv:rollup, r=frewsxcv
Rollup of 5 pull requests - Successful merges: #46762, #46777, #47262, #47285, #47301 - Failed merges:
2 parents 107e65e + 8fbfd2c commit 92c32d2

File tree

13 files changed

+501
-278
lines changed

13 files changed

+501
-278
lines changed

src/doc/rust.css

+266-223
Large diffs are not rendered by default.

src/liballoc/benches/slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ macro_rules! rotate {
343343
fn $name(b: &mut Bencher) {
344344
let size = mem::size_of_val(&$gen(1)[0]);
345345
let mut v = $gen($len * 8 / size);
346-
b.iter(|| black_box(&mut v).rotate(($mid*8+size-1)/size));
346+
b.iter(|| black_box(&mut v).rotate_left(($mid*8+size-1)/size));
347347
b.bytes = (v.len() * size) as u64;
348348
}
349349
}

src/liballoc/slice.rs

+64-35
Original file line numberDiff line numberDiff line change
@@ -1360,24 +1360,61 @@ impl<T> [T] {
13601360
core_slice::SliceExt::sort_unstable_by_key(self, f);
13611361
}
13621362

1363-
/// Permutes the slice in-place such that `self[mid..]` moves to the
1364-
/// beginning of the slice while `self[..mid]` moves to the end of the
1365-
/// slice. Equivalently, rotates the slice `mid` places to the left
1366-
/// or `k = self.len() - mid` places to the right.
1363+
/// Rotates the slice in-place such that the first `mid` elements of the
1364+
/// slice move to the end while the last `self.len() - mid` elements move to
1365+
/// the front. After calling `rotate_left`, the element previously at index
1366+
/// `mid` will become the first element in the slice.
13671367
///
1368-
/// This is a "k-rotation", a permutation in which item `i` moves to
1369-
/// position `i + k`, modulo the length of the slice. See _Elements
1370-
/// of Programming_ [§10.4][eop].
1368+
/// # Panics
1369+
///
1370+
/// This function will panic if `mid` is greater than the length of the
1371+
/// slice. Note that `mid == self.len()` does _not_ panic and is a no-op
1372+
/// rotation.
1373+
///
1374+
/// # Complexity
1375+
///
1376+
/// Takes linear (in `self.len()`) time.
1377+
///
1378+
/// # Examples
13711379
///
1372-
/// Rotation by `mid` and rotation by `k` are inverse operations.
1380+
/// ```
1381+
/// #![feature(slice_rotate)]
13731382
///
1374-
/// [eop]: https://books.google.com/books?id=CO9ULZGINlsC&pg=PA178&q=k-rotation
1383+
/// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
1384+
/// a.rotate_left(2);
1385+
/// assert_eq!(a, ['c', 'd', 'e', 'f', 'a', 'b']);
1386+
/// ```
1387+
///
1388+
/// Rotating a subslice:
1389+
///
1390+
/// ```
1391+
/// #![feature(slice_rotate)]
1392+
///
1393+
/// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
1394+
/// a[1..5].rotate_left(1);
1395+
/// assert_eq!(a, ['a', 'c', 'd', 'e', 'b', 'f']);
1396+
/// ```
1397+
#[unstable(feature = "slice_rotate", issue = "41891")]
1398+
pub fn rotate_left(&mut self, mid: usize) {
1399+
core_slice::SliceExt::rotate_left(self, mid);
1400+
}
1401+
1402+
#[unstable(feature = "slice_rotate", issue = "41891")]
1403+
#[rustc_deprecated(since = "", reason = "renamed to `rotate_left`")]
1404+
pub fn rotate(&mut self, mid: usize) {
1405+
core_slice::SliceExt::rotate_left(self, mid);
1406+
}
1407+
1408+
/// Rotates the slice in-place such that the first `self.len() - k`
1409+
/// elements of the slice move to the end while the last `k` elements move
1410+
/// to the front. After calling `rotate_right`, the element previously at
1411+
/// index `self.len() - k` will become the first element in the slice.
13751412
///
13761413
/// # Panics
13771414
///
1378-
/// This function will panic if `mid` is greater than the length of the
1379-
/// slice. (Note that `mid == self.len()` does _not_ panic; it's a nop
1380-
/// rotation with `k == 0`, the inverse of a rotation with `mid == 0`.)
1415+
/// This function will panic if `k` is greater than the length of the
1416+
/// slice. Note that `k == self.len()` does _not_ panic and is a no-op
1417+
/// rotation.
13811418
///
13821419
/// # Complexity
13831420
///
@@ -1388,31 +1425,23 @@ impl<T> [T] {
13881425
/// ```
13891426
/// #![feature(slice_rotate)]
13901427
///
1391-
/// let mut a = [1, 2, 3, 4, 5, 6, 7];
1392-
/// let mid = 2;
1393-
/// a.rotate(mid);
1394-
/// assert_eq!(&a, &[3, 4, 5, 6, 7, 1, 2]);
1395-
/// let k = a.len() - mid;
1396-
/// a.rotate(k);
1397-
/// assert_eq!(&a, &[1, 2, 3, 4, 5, 6, 7]);
1398-
///
1399-
/// use std::ops::Range;
1400-
/// fn slide<T>(slice: &mut [T], range: Range<usize>, to: usize) {
1401-
/// if to < range.start {
1402-
/// slice[to..range.end].rotate(range.start-to);
1403-
/// } else if to > range.end {
1404-
/// slice[range.start..to].rotate(range.end-range.start);
1405-
/// }
1406-
/// }
1407-
/// let mut v: Vec<_> = (0..10).collect();
1408-
/// slide(&mut v, 1..4, 7);
1409-
/// assert_eq!(&v, &[0, 4, 5, 6, 1, 2, 3, 7, 8, 9]);
1410-
/// slide(&mut v, 6..8, 1);
1411-
/// assert_eq!(&v, &[0, 3, 7, 4, 5, 6, 1, 2, 8, 9]);
1428+
/// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
1429+
/// a.rotate_right(2);
1430+
/// assert_eq!(a, ['e', 'f', 'a', 'b', 'c', 'd']);
1431+
/// ```
1432+
///
1433+
/// Rotate a subslice:
1434+
///
1435+
/// ```
1436+
/// #![feature(slice_rotate)]
1437+
///
1438+
/// let mut a = ['a', 'b', 'c', 'd', 'e', 'f'];
1439+
/// a[1..5].rotate_right(1);
1440+
/// assert_eq!(a, ['a', 'e', 'b', 'c', 'd', 'f']);
14121441
/// ```
14131442
#[unstable(feature = "slice_rotate", issue = "41891")]
1414-
pub fn rotate(&mut self, mid: usize) {
1415-
core_slice::SliceExt::rotate(self, mid);
1443+
pub fn rotate_right(&mut self, k: usize) {
1444+
core_slice::SliceExt::rotate_right(self, k);
14161445
}
14171446

14181447
/// Copies the elements from `src` into `self`.

src/liballoc/tests/slice.rs

+43-8
Original file line numberDiff line numberDiff line change
@@ -494,37 +494,72 @@ fn test_sort_stability() {
494494
}
495495

496496
#[test]
497-
fn test_rotate() {
497+
fn test_rotate_left() {
498498
let expected: Vec<_> = (0..13).collect();
499499
let mut v = Vec::new();
500500

501501
// no-ops
502502
v.clone_from(&expected);
503-
v.rotate(0);
503+
v.rotate_left(0);
504504
assert_eq!(v, expected);
505-
v.rotate(expected.len());
505+
v.rotate_left(expected.len());
506506
assert_eq!(v, expected);
507507
let mut zst_array = [(), (), ()];
508-
zst_array.rotate(2);
508+
zst_array.rotate_left(2);
509509

510510
// happy path
511511
v = (5..13).chain(0..5).collect();
512-
v.rotate(8);
512+
v.rotate_left(8);
513513
assert_eq!(v, expected);
514514

515515
let expected: Vec<_> = (0..1000).collect();
516516

517517
// small rotations in large slice, uses ptr::copy
518518
v = (2..1000).chain(0..2).collect();
519-
v.rotate(998);
519+
v.rotate_left(998);
520520
assert_eq!(v, expected);
521521
v = (998..1000).chain(0..998).collect();
522-
v.rotate(2);
522+
v.rotate_left(2);
523523
assert_eq!(v, expected);
524524

525525
// non-small prime rotation, has a few rounds of swapping
526526
v = (389..1000).chain(0..389).collect();
527-
v.rotate(1000-389);
527+
v.rotate_left(1000-389);
528+
assert_eq!(v, expected);
529+
}
530+
531+
#[test]
532+
fn test_rotate_right() {
533+
let expected: Vec<_> = (0..13).collect();
534+
let mut v = Vec::new();
535+
536+
// no-ops
537+
v.clone_from(&expected);
538+
v.rotate_right(0);
539+
assert_eq!(v, expected);
540+
v.rotate_right(expected.len());
541+
assert_eq!(v, expected);
542+
let mut zst_array = [(), (), ()];
543+
zst_array.rotate_right(2);
544+
545+
// happy path
546+
v = (5..13).chain(0..5).collect();
547+
v.rotate_right(5);
548+
assert_eq!(v, expected);
549+
550+
let expected: Vec<_> = (0..1000).collect();
551+
552+
// small rotations in large slice, uses ptr::copy
553+
v = (2..1000).chain(0..2).collect();
554+
v.rotate_right(2);
555+
assert_eq!(v, expected);
556+
v = (998..1000).chain(0..998).collect();
557+
v.rotate_right(998);
558+
assert_eq!(v, expected);
559+
560+
// non-small prime rotation, has a few rounds of swapping
561+
v = (389..1000).chain(0..389).collect();
562+
v.rotate_right(389);
528563
assert_eq!(v, expected);
529564
}
530565

src/libcore/slice/mod.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ pub trait SliceExt {
206206
fn ends_with(&self, needle: &[Self::Item]) -> bool where Self::Item: PartialEq;
207207

208208
#[unstable(feature = "slice_rotate", issue = "41891")]
209-
fn rotate(&mut self, mid: usize);
209+
fn rotate_left(&mut self, mid: usize);
210+
211+
#[unstable(feature = "slice_rotate", issue = "41891")]
212+
fn rotate_right(&mut self, k: usize);
210213

211214
#[stable(feature = "clone_from_slice", since = "1.7.0")]
212215
fn clone_from_slice(&mut self, src: &[Self::Item]) where Self::Item: Clone;
@@ -645,7 +648,7 @@ impl<T> SliceExt for [T] {
645648
self.binary_search_by(|p| p.cmp(x))
646649
}
647650

648-
fn rotate(&mut self, mid: usize) {
651+
fn rotate_left(&mut self, mid: usize) {
649652
assert!(mid <= self.len());
650653
let k = self.len() - mid;
651654

@@ -655,6 +658,16 @@ impl<T> SliceExt for [T] {
655658
}
656659
}
657660

661+
fn rotate_right(&mut self, k: usize) {
662+
assert!(k <= self.len());
663+
let mid = self.len() - k;
664+
665+
unsafe {
666+
let p = self.as_mut_ptr();
667+
rotate::ptr_rotate(mid, p.offset(mid as isize), k);
668+
}
669+
}
670+
658671
#[inline]
659672
fn clone_from_slice(&mut self, src: &[T]) where T: Clone {
660673
assert!(self.len() == src.len(),

src/libcore/tests/slice.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -329,17 +329,32 @@ fn test_iter_folds() {
329329
}
330330

331331
#[test]
332-
fn test_rotate() {
332+
fn test_rotate_left() {
333333
const N: usize = 600;
334334
let a: &mut [_] = &mut [0; N];
335335
for i in 0..N {
336336
a[i] = i;
337337
}
338338

339-
a.rotate(42);
339+
a.rotate_left(42);
340340
let k = N - 42;
341341

342342
for i in 0..N {
343-
assert_eq!(a[(i+k)%N], i);
343+
assert_eq!(a[(i + k) % N], i);
344+
}
345+
}
346+
347+
#[test]
348+
fn test_rotate_right() {
349+
const N: usize = 600;
350+
let a: &mut [_] = &mut [0; N];
351+
for i in 0..N {
352+
a[i] = i;
353+
}
354+
355+
a.rotate_right(42);
356+
357+
for i in 0..N {
358+
assert_eq!(a[(i + 42) % N], i);
344359
}
345360
}

src/librustc_lint/builtin.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1214,8 +1214,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
12141214
// don't have anything to attach a symbol to
12151215
let msg = "const items should never be #[no_mangle]";
12161216
let mut err = cx.struct_span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg);
1217+
1218+
// account for "pub const" (#45562)
1219+
let start = cx.tcx.sess.codemap().span_to_snippet(it.span)
1220+
.map(|snippet| snippet.find("const").unwrap_or(0))
1221+
.unwrap_or(0) as u32;
12171222
// `const` is 5 chars
1218-
let const_span = it.span.with_hi(BytePos(it.span.lo().0 + 5));
1223+
let const_span = it.span.with_hi(BytePos(it.span.lo().0 + start + 5));
12191224
err.span_suggestion(const_span,
12201225
"try a static value",
12211226
"pub static".to_owned());

src/libstd/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ pub mod builtin {
283283
/// Unconditionally causes compilation to fail with the given error message when encountered.
284284
///
285285
/// This macro should be used when a crate uses a conditional compilation strategy to provide
286-
/// better error messages for errornous conditions.
286+
/// better error messages for erroneous conditions.
287287
///
288288
/// # Examples
289289
///

src/libstd/panicking.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ impl<'a> Location<'a> {
316316
/// # Examples
317317
///
318318
/// ```should_panic
319-
/// #![feature(panic_col)]
320319
/// use std::panic;
321320
///
322321
/// panic::set_hook(Box::new(|panic_info| {
@@ -329,7 +328,7 @@ impl<'a> Location<'a> {
329328
///
330329
/// panic!("Normal panic");
331330
/// ```
332-
#[unstable(feature = "panic_col", reason = "recently added", issue = "42939")]
331+
#[stable(feature = "panic_col", since = "1.25")]
333332
pub fn column(&self) -> u32 {
334333
self.col
335334
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[no_mangle] pub const RAH: usize = 5;
12+
//~^ ERROR const items should never be #[no_mangle]
13+
14+
fn main() {}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: const items should never be #[no_mangle]
2+
--> $DIR/issue-45562.rs:11:14
3+
|
4+
11 | #[no_mangle] pub const RAH: usize = 5;
5+
| ---------^^^^^^^^^^^^^^^^
6+
| |
7+
| help: try a static value: `pub static`
8+
|
9+
= note: #[deny(no_mangle_const_items)] on by default
10+
11+
error: aborting due to previous error
12+

src/tools/compiletest/src/runtest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2867,7 +2867,7 @@ fn read2_abbreviated(mut child: Child) -> io::Result<Output> {
28672867
*skipped += data.len();
28682868
if data.len() <= TAIL_LEN {
28692869
tail[..data.len()].copy_from_slice(data);
2870-
tail.rotate(data.len());
2870+
tail.rotate_left(data.len());
28712871
} else {
28722872
tail.copy_from_slice(&data[(data.len() - TAIL_LEN)..]);
28732873
}

0 commit comments

Comments
 (0)