diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-01-18 22:56:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-18 22:56:40 +0100 |
| commit | 49c74e4c859a1f24c03ff4cf983802dffca7bd31 (patch) | |
| tree | 3353293511d3d6e7d981d7c8df9e5d40cc08abe0 | |
| parent | 0dd4bfa35616d43bb6b24f209baade9270afdb59 (diff) | |
| parent | c120199116439827a89a0ac0f24f91f99dbed60f (diff) | |
| download | rust-49c74e4c859a1f24c03ff4cf983802dffca7bd31.tar.gz rust-49c74e4c859a1f24c03ff4cf983802dffca7bd31.zip | |
Rollup merge of #57350 - folex:master, r=estebank
Better error note on unimplemented Index trait for string
fixes #56740
I've tried to compile suggestion from comments in the issue #56740, but unsure of it. So I'm open to advice :)
Current output will be like this:
```rust
error[E0277]: the type `str` cannot be indexed by `{integer}`
--> $DIR/str-idx.rs:3:17
|
LL | let c: u8 = s[4]; //~ ERROR the type `str` cannot be indexed by `{integer}`
| ^^^^ `str` cannot be indexed by `{integer}`
|
= help: the trait `std::ops::Index<{integer}>` is not implemented for `str`
= note: you can use `.chars().nth()` or `.bytes().nth()`
see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
```
`x.py test src/test/ui` succeeded and I've also tested output manually by compiling the following code:
```rust
fn _f() {
let s = std::string::String::from("hello");
let _c = s[0];
let s = std::string::String::from("hello");
let mut _c = s[0];
let s = "hello";
let _c = s[0];
let s = "hello";
let mut _c = &s[0];
}
```
Not sure if some docs should be changed too. I will also fix error message in the [Book :: Indexing into Strings](https://github.com/rust-lang/book/blob/db53e2e3cdf77beac853df6f29db4b3b86ea598c/src/ch08-02-strings.md#indexing-into-strings) if that PR will get approved :)
| -rw-r--r-- | src/libcore/ops/index.rs | 30 | ||||
| -rw-r--r-- | src/test/ui/str/str-idx.stderr | 2 | ||||
| -rw-r--r-- | src/test/ui/str/str-mut-idx.stderr | 2 |
3 files changed, 34 insertions, 0 deletions
diff --git a/src/libcore/ops/index.rs b/src/libcore/ops/index.rs index 4f55c68ecd4..6cfa36741d0 100644 --- a/src/libcore/ops/index.rs +++ b/src/libcore/ops/index.rs @@ -51,6 +51,21 @@ /// ``` #[lang = "index"] #[rustc_on_unimplemented( + on( + _Self="&str", + note="you can use `.chars().nth()` or `.bytes().nth()` +see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>" + ), + on( + _Self="str", + note="you can use `.chars().nth()` or `.bytes().nth()` +see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>" + ), + on( + _Self="std::string::String", + note="you can use `.chars().nth()` or `.bytes().nth()` +see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>" + ), message="the type `{Self}` cannot be indexed by `{Idx}`", label="`{Self}` cannot be indexed by `{Idx}`", )] @@ -141,6 +156,21 @@ pub trait Index<Idx: ?Sized> { /// ``` #[lang = "index_mut"] #[rustc_on_unimplemented( + on( + _Self="&str", + note="you can use `.chars().nth()` or `.bytes().nth()` +see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>" + ), + on( + _Self="str", + note="you can use `.chars().nth()` or `.bytes().nth()` +see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>" + ), + on( + _Self="std::string::String", + note="you can use `.chars().nth()` or `.bytes().nth()` +see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>" + ), message="the type `{Self}` cannot be mutably indexed by `{Idx}`", label="`{Self}` cannot be mutably indexed by `{Idx}`", )] diff --git a/src/test/ui/str/str-idx.stderr b/src/test/ui/str/str-idx.stderr index 108096df9c4..71b17474923 100644 --- a/src/test/ui/str/str-idx.stderr +++ b/src/test/ui/str/str-idx.stderr @@ -5,6 +5,8 @@ LL | let c: u8 = s[4]; //~ ERROR the type `str` cannot be indexed by `{integ | ^^^^ `str` cannot be indexed by `{integer}` | = help: the trait `std::ops::Index<{integer}>` is not implemented for `str` + = note: you can use `.chars().nth()` or `.bytes().nth()` + see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings> error: aborting due to previous error diff --git a/src/test/ui/str/str-mut-idx.stderr b/src/test/ui/str/str-mut-idx.stderr index a0c7c2c574e..a1212c5a4fe 100644 --- a/src/test/ui/str/str-mut-idx.stderr +++ b/src/test/ui/str/str-mut-idx.stderr @@ -29,6 +29,8 @@ LL | s[1usize] = bot(); | ^^^^^^^^^ `str` cannot be mutably indexed by `usize` | = help: the trait `std::ops::IndexMut<usize>` is not implemented for `str` + = note: you can use `.chars().nth()` or `.bytes().nth()` + see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings> error: aborting due to 3 previous errors |
