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

Conversation

Projects
None yet
6 participants
@folex
Copy link
Contributor

folex commented Jan 5, 2019

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:

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:

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 if that PR will get approved :)

@rust-highfive

This comment has been minimized.

Copy link
Collaborator

rust-highfive commented Jan 5, 2019

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cramertj (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@folex folex changed the title Show suggestion to use .char().nth() and link to The Book on unimplemented Index trait Better suggestion on unimplemented Index trait for string Jan 5, 2019

@folex folex force-pushed the folex:master branch from 41f20a4 to 493d6f1 Jan 5, 2019

@rust-highfive

This comment was marked as resolved.

Copy link
Collaborator

rust-highfive commented Jan 5, 2019

The job x86_64-gnu-llvm-6.0 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
travis_time:end:1ddf0fda:start=1546664304124929293,finish=1546664305154483987,duration=1029554694
$ git checkout -qf FETCH_HEAD
travis_fold:end:git.checkout

Encrypted environment variables have been removed for security reasons.
See https://docs.travis-ci.com/user/pull-requests/#pull-requests-and-security-restrictions
$ export SCCACHE_BUCKET=rust-lang-ci-sccache2
$ export SCCACHE_REGION=us-west-1
Setting environment variables from .travis.yml
$ export IMAGE=x86_64-gnu-llvm-6.0
---
[00:03:45]    Compiling tidy v0.1.0 (/checkout/src/tools/tidy)
[00:03:53] some tidy checks failed
[00:03:53] 
[00:03:53] 
[00:03:53] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/tidy" "/checkout/src" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "--no-vendor" "--quiet"
[00:03:53] 
[00:03:53] 
[00:03:53] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test src/tools/tidy
[00:03:53] Build completed unsuccessfully in 0:00:47
[00:03:53] Build completed unsuccessfully in 0:00:47
[00:03:53] Makefile:69: recipe for target 'tidy' failed
[00:03:53] make: *** [tidy] Error 1
The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:033bc117
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
Sat Jan  5 05:02:30 UTC 2019
---
travis_time:end:098a5dc0:start=1546664550833266014,finish=1546664550838237231,duration=4971217
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:166705bc
$ ln -s . checkout && for CORE in obj/cores/core.*; do EXE=$(echo $CORE | sed 's|obj/cores/core\.[0-9]*\.!checkout!\(.*\)|\1|;y|!|/|'); if [ -f "$EXE" ]; then printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" "$CORE"; gdb --batch -q -c "$CORE" "$EXE" -iex 'set auto-load off' -iex 'dir src/' -iex 'set sysroot .' -ex bt -ex q; echo travis_fold":"end:crashlog; fi; done || true
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:0e4850a8
travis_time:start:0e4850a8
$ cat ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
cat: ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers: No such file or directory
travis_fold:end:after_failure.5
travis_fold:start:after_failure.6
travis_time:start:06742b07
$ dmesg | grep -i kill

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@folex folex force-pushed the folex:master branch from 493d6f1 to c120199 Jan 5, 2019

@folex folex changed the title Better suggestion on unimplemented Index trait for string Better error note on unimplemented Index trait for string Jan 5, 2019

@varkor

This comment has been minimized.

Copy link
Member

varkor commented Jan 5, 2019

@rust-highfive rust-highfive assigned estebank and unassigned cramertj Jan 5, 2019

@estebank

This comment has been minimized.

Copy link
Contributor

estebank commented Jan 18, 2019

@bors r+ rollup

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Jan 18, 2019

📌 Commit c120199 has been approved by estebank

Centril added a commit to Centril/rust that referenced this pull request Jan 18, 2019

Rollup merge of rust-lang#57350 - folex:master, r=estebank
Better error note on unimplemented Index trait for string

fixes rust-lang#56740

I've tried to compile suggestion from comments in the issue rust-lang#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 :)

bors added a commit that referenced this pull request Jan 18, 2019

Auto merge of #57747 - Centril:rollup, r=Centril
Rollup of 11 pull requests

Successful merges:

 - #57107 (Add a regression test for mutating a non-mut #[thread_local])
 - #57132 (Document that `-C opt-level=0` implies `-C debug-assertions`.)
 - #57212 (docs(rustc): Link to the book's source in rustc)
 - #57302 (Fix unused_assignments false positive)
 - #57350 (Better error note on unimplemented Index trait for string)
 - #57635 (use structured macro and path resolve suggestions)
 - #57650 (librustc_metadata: Pass a default value when unwrapping a span)
 - #57657 (Add regression test to close #53787)
 - #57658 (Two HIR tweaks)
 - #57720 (Fix suggestions given mulitple bad lifetimes)
 - #57725 (Use structured suggestion to surround struct literal with parenthesis)

Failed merges:

r? @ghost

@bors bors merged commit c120199 into rust-lang:master Jan 19, 2019

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment