Skip to content

Commit cb7d52f

Browse files
committed
constify some methods using SliceIndex
1 parent b5230e5 commit cb7d52f

File tree

7 files changed

+33
-23
lines changed

7 files changed

+33
-23
lines changed

library/core/src/ptr/const_ptr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,10 +1524,11 @@ impl<T> *const [T] {
15241524
/// }
15251525
/// ```
15261526
#[unstable(feature = "slice_ptr_get", issue = "74265")]
1527+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
15271528
#[inline]
1528-
pub unsafe fn get_unchecked<I>(self, index: I) -> *const I::Output
1529+
pub const unsafe fn get_unchecked<I>(self, index: I) -> *const I::Output
15291530
where
1530-
I: SliceIndex<[T]>,
1531+
I: ~const SliceIndex<[T]>,
15311532
{
15321533
// SAFETY: the caller ensures that `self` is dereferenceable and `index` in-bounds.
15331534
unsafe { index.get_unchecked(self) }

library/core/src/ptr/mut_ptr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,10 +1881,11 @@ impl<T> *mut [T] {
18811881
/// }
18821882
/// ```
18831883
#[unstable(feature = "slice_ptr_get", issue = "74265")]
1884+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
18841885
#[inline(always)]
1885-
pub unsafe fn get_unchecked_mut<I>(self, index: I) -> *mut I::Output
1886+
pub const unsafe fn get_unchecked_mut<I>(self, index: I) -> *mut I::Output
18861887
where
1887-
I: SliceIndex<[T]>,
1888+
I: ~const SliceIndex<[T]>,
18881889
{
18891890
// SAFETY: the caller ensures that `self` is dereferenceable and `index` in-bounds.
18901891
unsafe { index.get_unchecked_mut(self) }

library/core/src/ptr/non_null.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,10 +1597,11 @@ impl<T> NonNull<[T]> {
15971597
/// }
15981598
/// ```
15991599
#[unstable(feature = "slice_ptr_get", issue = "74265")]
1600+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
16001601
#[inline]
1601-
pub unsafe fn get_unchecked_mut<I>(self, index: I) -> NonNull<I::Output>
1602+
pub const unsafe fn get_unchecked_mut<I>(self, index: I) -> NonNull<I::Output>
16021603
where
1603-
I: SliceIndex<[T]>,
1604+
I: ~const SliceIndex<[T]>,
16041605
{
16051606
// SAFETY: the caller ensures that `self` is dereferenceable and `index` in-bounds.
16061607
// As a consequence, the resulting pointer cannot be null.

library/core/src/slice/mod.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,10 @@ impl<T> [T] {
568568
#[rustc_no_implicit_autorefs]
569569
#[inline]
570570
#[must_use]
571-
pub fn get<I>(&self, index: I) -> Option<&I::Output>
571+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
572+
pub const fn get<I>(&self, index: I) -> Option<&I::Output>
572573
where
573-
I: SliceIndex<Self>,
574+
I: ~const SliceIndex<Self>,
574575
{
575576
index.get(self)
576577
}
@@ -594,9 +595,10 @@ impl<T> [T] {
594595
#[rustc_no_implicit_autorefs]
595596
#[inline]
596597
#[must_use]
597-
pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
598+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
599+
pub const fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
598600
where
599-
I: SliceIndex<Self>,
601+
I: ~const SliceIndex<Self>,
600602
{
601603
index.get_mut(self)
602604
}
@@ -633,9 +635,10 @@ impl<T> [T] {
633635
#[inline]
634636
#[must_use]
635637
#[track_caller]
636-
pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
638+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
639+
pub const unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
637640
where
638-
I: SliceIndex<Self>,
641+
I: ~const SliceIndex<Self>,
639642
{
640643
// SAFETY: the caller must uphold most of the safety requirements for `get_unchecked`;
641644
// the slice is dereferenceable because `self` is a safe reference.
@@ -677,9 +680,10 @@ impl<T> [T] {
677680
#[inline]
678681
#[must_use]
679682
#[track_caller]
680-
pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
683+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
684+
pub const unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
681685
where
682-
I: SliceIndex<Self>,
686+
I: ~const SliceIndex<Self>,
683687
{
684688
// SAFETY: the caller must uphold the safety requirements for `get_unchecked_mut`;
685689
// the slice is dereferenceable because `self` is a safe reference.

library/core/src/str/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,9 @@ impl str {
589589
/// assert!(v.get(..42).is_none());
590590
/// ```
591591
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
592+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
592593
#[inline]
593-
pub fn get<I: SliceIndex<str>>(&self, i: I) -> Option<&I::Output> {
594+
pub const fn get<I: ~const SliceIndex<str>>(&self, i: I) -> Option<&I::Output> {
594595
i.get(self)
595596
}
596597

@@ -621,8 +622,9 @@ impl str {
621622
/// assert_eq!("HEllo", v);
622623
/// ```
623624
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
625+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
624626
#[inline]
625-
pub fn get_mut<I: SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output> {
627+
pub const fn get_mut<I: ~const SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output> {
626628
i.get_mut(self)
627629
}
628630

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
//@ known-bug: #110395
1+
#![feature(const_index, const_trait_impl)]
22

33
const A: [(); 5] = [(), (), (), (), ()];
44

55
// Since the indexing is on a ZST, the addresses are all fine,
66
// but we should still catch the bad range.
77
const B: &[()] = unsafe { A.get_unchecked(3..1) };
8+
//~^ ERROR: slice::get_unchecked requires that the range is within the slice
89

910
fn main() {}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0015]: cannot call non-const method `core::slice::<impl [()]>::get_unchecked::<std::ops::Range<usize>>` in constants
2-
--> $DIR/ub-slice-get-unchecked.rs:7:29
1+
error[E0080]: evaluation panicked: unsafe precondition(s) violated: slice::get_unchecked requires that the range is within the slice
2+
3+
This indicates a bug in the program. This Undefined Behavior check is optional, and cannot be relied on for safety.
4+
--> $DIR/ub-slice-get-unchecked.rs:7:27
35
|
46
LL | const B: &[()] = unsafe { A.get_unchecked(3..1) };
5-
| ^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
7+
| ^^^^^^^^^^^^^^^^^^^^^ evaluation of `B` failed here
88

99
error: aborting due to 1 previous error
1010

11-
For more information about this error, try `rustc --explain E0015`.
11+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)