Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error note on unimplemented Index trait for string #57350

Merged
merged 1 commit into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/libcore/ops/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`",
)]
Expand Down Expand Up @@ -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}`",
)]
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/str/str-idx.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/str/str-mut-idx.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down