Skip to content

Commit ad55b73

Browse files
authoredJan 22, 2019
Rollup merge of rust-lang#57604 - alercah:str-index, r=sfackler
Make `str` indexing generic on `SliceIndex`. Fixes rust-lang#55603
2 parents e3d3cff + c7d25a2 commit ad55b73

13 files changed

+239
-235
lines changed
 

‎src/libcore/ops/index.rs

-15
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,6 @@
5151
/// ```
5252
#[lang = "index"]
5353
#[rustc_on_unimplemented(
54-
on(
55-
_Self="&str",
56-
note="you can use `.chars().nth()` or `.bytes().nth()`
57-
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
58-
),
59-
on(
60-
_Self="str",
61-
note="you can use `.chars().nth()` or `.bytes().nth()`
62-
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
63-
),
64-
on(
65-
_Self="std::string::String",
66-
note="you can use `.chars().nth()` or `.bytes().nth()`
67-
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
68-
),
6954
message="the type `{Self}` cannot be indexed by `{Idx}`",
7055
label="`{Self}` cannot be indexed by `{Idx}`",
7156
)]

‎src/libcore/slice/mod.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -2383,7 +2383,6 @@ impl [u8] {
23832383
}
23842384

23852385
#[stable(feature = "rust1", since = "1.0.0")]
2386-
#[rustc_on_unimplemented = "slice indices are of type `usize` or ranges of `usize`"]
23872386
impl<T, I> ops::Index<I> for [T]
23882387
where I: SliceIndex<[T]>
23892388
{
@@ -2396,7 +2395,6 @@ impl<T, I> ops::Index<I> for [T]
23962395
}
23972396

23982397
#[stable(feature = "rust1", since = "1.0.0")]
2399-
#[rustc_on_unimplemented = "slice indices are of type `usize` or ranges of `usize`"]
24002398
impl<T, I> ops::IndexMut<I> for [T]
24012399
where I: SliceIndex<[T]>
24022400
{
@@ -2447,7 +2445,19 @@ mod private_slice_index {
24472445

24482446
/// A helper trait used for indexing operations.
24492447
#[stable(feature = "slice_get_slice", since = "1.28.0")]
2450-
#[rustc_on_unimplemented = "slice indices are of type `usize` or ranges of `usize`"]
2448+
#[rustc_on_unimplemented(
2449+
on(
2450+
T = "str",
2451+
label = "string indices are ranges of `usize`",
2452+
),
2453+
on(
2454+
all(any(T = "str", T = "&str", T = "std::string::String"), _Self="{integer}"),
2455+
note="you can use `.chars().nth()` or `.bytes().nth()`
2456+
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>"
2457+
),
2458+
message = "the type `{T}` cannot be indexed by `{Self}`",
2459+
label = "slice indices are of type `usize` or ranges of `usize`",
2460+
)]
24512461
pub trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
24522462
/// The output type returned by methods.
24532463
#[stable(feature = "slice_get_slice", since = "1.28.0")]

0 commit comments

Comments
 (0)
Please sign in to comment.