Skip to content

Commit 2b4ede7

Browse files
committed
constify Index trait and its slice impls
1 parent cb7d52f commit 2b4ede7

File tree

8 files changed

+60
-48
lines changed

8 files changed

+60
-48
lines changed

library/core/src/array/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,10 @@ impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N] {
374374
}
375375

376376
#[stable(feature = "index_trait_on_arrays", since = "1.50.0")]
377-
impl<T, I, const N: usize> Index<I> for [T; N]
377+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
378+
impl<T, I, const N: usize> const Index<I> for [T; N]
378379
where
379-
[T]: Index<I>,
380+
[T]: ~const Index<I>,
380381
{
381382
type Output = <[T] as Index<I>>::Output;
382383

@@ -387,9 +388,10 @@ where
387388
}
388389

389390
#[stable(feature = "index_trait_on_arrays", since = "1.50.0")]
390-
impl<T, I, const N: usize> IndexMut<I> for [T; N]
391+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
392+
impl<T, I, const N: usize> const IndexMut<I> for [T; N]
391393
where
392-
[T]: IndexMut<I>,
394+
[T]: ~const IndexMut<I>,
393395
{
394396
#[inline]
395397
fn index_mut(&mut self, index: I) -> &mut Self::Output {

library/core/src/ops/index.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
#[doc(alias = "]")]
5656
#[doc(alias = "[")]
5757
#[doc(alias = "[]")]
58+
#[const_trait]
59+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
5860
pub trait Index<Idx: ?Sized> {
5961
/// The returned type after indexing.
6062
#[stable(feature = "rust1", since = "1.0.0")]
@@ -165,7 +167,9 @@ see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#ind
165167
#[doc(alias = "[")]
166168
#[doc(alias = "]")]
167169
#[doc(alias = "[]")]
168-
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
170+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
171+
#[const_trait]
172+
pub trait IndexMut<Idx: ?Sized>: ~const Index<Idx> {
169173
/// Performs the mutable indexing (`container[index]`) operation.
170174
///
171175
/// # Panics

library/core/src/slice/index.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use crate::ub_checks::assert_unsafe_precondition;
66
use crate::{ops, range};
77

88
#[stable(feature = "rust1", since = "1.0.0")]
9-
impl<T, I> ops::Index<I> for [T]
9+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
10+
impl<T, I> const ops::Index<I> for [T]
1011
where
11-
I: SliceIndex<[T]>,
12+
I: ~const SliceIndex<[T]>,
1213
{
1314
type Output = I::Output;
1415

@@ -19,9 +20,10 @@ where
1920
}
2021

2122
#[stable(feature = "rust1", since = "1.0.0")]
22-
impl<T, I> ops::IndexMut<I> for [T]
23+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
24+
impl<T, I> const ops::IndexMut<I> for [T]
2325
where
24-
I: SliceIndex<[T]>,
26+
I: ~const SliceIndex<[T]>,
2527
{
2628
#[inline(always)]
2729
fn index_mut(&mut self, index: I) -> &mut I::Output {

library/core/src/str/traits.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ impl PartialOrd for str {
4949
}
5050

5151
#[stable(feature = "rust1", since = "1.0.0")]
52-
impl<I> ops::Index<I> for str
52+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
53+
impl<I> const ops::Index<I> for str
5354
where
54-
I: SliceIndex<str>,
55+
I: ~const SliceIndex<str>,
5556
{
5657
type Output = I::Output;
5758

@@ -62,9 +63,10 @@ where
6263
}
6364

6465
#[stable(feature = "rust1", since = "1.0.0")]
65-
impl<I> ops::IndexMut<I> for str
66+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
67+
impl<I> const ops::IndexMut<I> for str
6668
where
67-
I: SliceIndex<str>,
69+
I: ~const SliceIndex<str>,
6870
{
6971
#[inline]
7072
fn index_mut(&mut self, index: I) -> &mut I::Output {

tests/ui/consts/issue-94675.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ struct Foo<'a> {
77
impl<'a> Foo<'a> {
88
const fn spam(&mut self, baz: &mut Vec<u32>) {
99
self.bar[0] = baz.len();
10-
//~^ ERROR: cannot call
10+
//~^ ERROR: `Vec<usize>: [const] Index<_>` is not satisfied
11+
//~| ERROR: `Vec<usize>: [const] Index<usize>` is not satisfied
12+
//~| ERROR: `Vec<usize>: [const] IndexMut<usize>` is not satisfied
1113
}
1214
}
1315

tests/ui/consts/issue-94675.stderr

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
error[E0015]: cannot call non-const operator in constant functions
2-
--> $DIR/issue-94675.rs:9:17
1+
error[E0277]: the trait bound `Vec<usize>: [const] Index<_>` is not satisfied
2+
--> $DIR/issue-94675.rs:9:9
33
|
44
LL | self.bar[0] = baz.len();
5-
| ^^^
5+
| ^^^^^^^^^^^
6+
7+
error[E0277]: the trait bound `Vec<usize>: [const] IndexMut<usize>` is not satisfied
8+
--> $DIR/issue-94675.rs:9:9
9+
|
10+
LL | self.bar[0] = baz.len();
11+
| ^^^^^^^^^^^
12+
13+
error[E0277]: the trait bound `Vec<usize>: [const] Index<usize>` is not satisfied
14+
--> $DIR/issue-94675.rs:9:9
15+
|
16+
LL | self.bar[0] = baz.len();
17+
| ^^^^^^^^^^^
618
|
7-
note: impl defined here, but it is not `const`
8-
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
9-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
19+
note: required by a bound in `std::ops::IndexMut::index_mut`
20+
--> $SRC_DIR/core/src/ops/index.rs:LL:COL
1021

11-
error: aborting due to 1 previous error
22+
error: aborting due to 3 previous errors
1223

13-
For more information about this error, try `rustc --explain E0015`.
24+
For more information about this error, try `rustc --explain E0277`.

tests/ui/parser/issues/issue-35813-postfix-after-cast.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ edition:2018
22
#![crate_type = "lib"]
3-
#![feature(type_ascription)]
3+
#![feature(type_ascription, const_index, const_trait_impl)]
44
use std::future::Future;
55
use std::pin::Pin;
66

@@ -129,7 +129,6 @@ pub fn inside_block() {
129129

130130
static bar: &[i32] = &(&[1,2,3] as &[i32][0..1]);
131131
//~^ ERROR: cast cannot be followed by indexing
132-
//~| ERROR: cannot call non-const operator in statics
133132

134133
static bar2: &[i32] = &(&[1i32,2,3]: &[i32; 3][0..1]);
135134
//~^ ERROR: expected one of

tests/ui/parser/issues/issue-35813-postfix-after-cast.stderr

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,13 @@ LL | static bar: &[i32] = &((&[1,2,3] as &[i32])[0..1]);
219219
| + +
220220

221221
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `:`
222-
--> $DIR/issue-35813-postfix-after-cast.rs:134:36
222+
--> $DIR/issue-35813-postfix-after-cast.rs:133:36
223223
|
224224
LL | static bar2: &[i32] = &(&[1i32,2,3]: &[i32; 3][0..1]);
225225
| ^ expected one of `)`, `,`, `.`, `?`, or an operator
226226

227227
error: cast cannot be followed by `?`
228-
--> $DIR/issue-35813-postfix-after-cast.rs:139:5
228+
--> $DIR/issue-35813-postfix-after-cast.rs:138:5
229229
|
230230
LL | Err(0u64) as Result<u64,u64>?;
231231
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -236,25 +236,25 @@ LL | (Err(0u64) as Result<u64,u64>)?;
236236
| + +
237237

238238
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `:`
239-
--> $DIR/issue-35813-postfix-after-cast.rs:141:14
239+
--> $DIR/issue-35813-postfix-after-cast.rs:140:14
240240
|
241241
LL | Err(0u64): Result<u64,u64>?;
242242
| ^ expected one of `.`, `;`, `?`, `}`, or an operator
243243

244244
error: expected identifier, found `:`
245-
--> $DIR/issue-35813-postfix-after-cast.rs:153:13
245+
--> $DIR/issue-35813-postfix-after-cast.rs:152:13
246246
|
247247
LL | drop_ptr: F();
248248
| ^ expected identifier
249249

250250
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `:`
251-
--> $DIR/issue-35813-postfix-after-cast.rs:160:13
251+
--> $DIR/issue-35813-postfix-after-cast.rs:159:13
252252
|
253253
LL | drop_ptr: fn(u8);
254254
| ^ expected one of 8 possible tokens
255255

256256
error: cast cannot be followed by a function call
257-
--> $DIR/issue-35813-postfix-after-cast.rs:166:5
257+
--> $DIR/issue-35813-postfix-after-cast.rs:165:5
258258
|
259259
LL | drop as fn(u8)(0);
260260
| ^^^^^^^^^^^^^^
@@ -265,13 +265,13 @@ LL | (drop as fn(u8))(0);
265265
| + +
266266

267267
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `:`
268-
--> $DIR/issue-35813-postfix-after-cast.rs:168:13
268+
--> $DIR/issue-35813-postfix-after-cast.rs:167:13
269269
|
270270
LL | drop_ptr: fn(u8)(0);
271271
| ^ expected one of 8 possible tokens
272272

273273
error: cast cannot be followed by `.await`
274-
--> $DIR/issue-35813-postfix-after-cast.rs:173:5
274+
--> $DIR/issue-35813-postfix-after-cast.rs:172:5
275275
|
276276
LL | Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>.await;
277277
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -282,13 +282,13 @@ LL | (Box::pin(noop()) as Pin<Box<dyn Future<Output = ()>>>).await;
282282
| + +
283283

284284
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `:`
285-
--> $DIR/issue-35813-postfix-after-cast.rs:176:21
285+
--> $DIR/issue-35813-postfix-after-cast.rs:175:21
286286
|
287287
LL | Box::pin(noop()): Pin<Box<_>>.await;
288288
| ^ expected one of `.`, `;`, `?`, `}`, or an operator
289289

290290
error: cast cannot be followed by a field access
291-
--> $DIR/issue-35813-postfix-after-cast.rs:188:5
291+
--> $DIR/issue-35813-postfix-after-cast.rs:187:5
292292
|
293293
LL | Foo::default() as Foo.bar;
294294
| ^^^^^^^^^^^^^^^^^^^^^
@@ -299,7 +299,7 @@ LL | (Foo::default() as Foo).bar;
299299
| + +
300300

301301
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `:`
302-
--> $DIR/issue-35813-postfix-after-cast.rs:190:19
302+
--> $DIR/issue-35813-postfix-after-cast.rs:189:19
303303
|
304304
LL | Foo::default(): Foo.bar;
305305
| ^ expected one of `.`, `;`, `?`, `}`, or an operator
@@ -322,21 +322,11 @@ LL | if true { 33 } else { 44 }: i32.max(0)
322322
| ^ expected one of `,`, `.`, `?`, or an operator
323323

324324
error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
325-
--> $DIR/issue-35813-postfix-after-cast.rs:151:13
325+
--> $DIR/issue-35813-postfix-after-cast.rs:150:13
326326
|
327327
LL | drop as F();
328328
| ^^^ only `Fn` traits may use parentheses
329329

330-
error[E0015]: cannot call non-const operator in statics
331-
--> $DIR/issue-35813-postfix-after-cast.rs:130:42
332-
|
333-
LL | static bar: &[i32] = &(&[1,2,3] as &[i32][0..1]);
334-
| ^^^^^^
335-
|
336-
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
337-
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
338-
339-
error: aborting due to 40 previous errors
330+
error: aborting due to 39 previous errors
340331

341-
Some errors have detailed explanations: E0015, E0214.
342-
For more information about an error, try `rustc --explain E0015`.
332+
For more information about this error, try `rustc --explain E0214`.

0 commit comments

Comments
 (0)