Skip to content

Suggest &str.chars() on attempt to &str.iter() #90803

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Nov 16, 2021

Conversation

TaKO8Ki
Copy link
Member

@TaKO8Ki TaKO8Ki commented Nov 11, 2021

closes #90786

@rust-highfive
Copy link
Contributor

r? @jackh726

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 11, 2021
@wooster0
Copy link
Contributor

Here's a similar error:

error[E0277]: `&str` is not an iterator
   --> src/main.rs:2:14
    |
2   |     for x in "hello" {}
    |              ^^^^^^^ `&str` is not an iterator; try calling `.chars()` or `.bytes()`
    |
    = help: the trait `Iterator` is not implemented for `&str`
    = note: required because of the requirements on the impl of `IntoIterator` for `&str`
note: required by `into_iter`

Maybe this one should mention bytes too?

Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer it if we could have a more... generalized solution for this problem. One that I've seen presented is an #[alias("alt_name")] attribute, where we could manually annotate methods in std with common names used in other languages for the same behavior. In this case we could annotate #[alias("iter", "codepoints")] fn chars and let that machinery do the rest.

But in the meantime I'm not against landing targeted solutions, although it is additional tech debt.

@estebank
Copy link
Contributor

@r00ster91 that error occurs in a separate part of the compiler. E0599 occurs during typeck, while E0277 occurs during trait resolution. It is also using the #[rustc_on_unimplemented], which is why it doesn't have a structured suggestion, just a label.

@TaKO8Ki TaKO8Ki force-pushed the suggest-chars-on-attempt-to-iter branch from e5afb63 to ae6f7b6 Compare November 13, 2021 13:02
@jackh726
Copy link
Member

r? @estebank

@rust-highfive rust-highfive assigned estebank and unassigned jackh726 Nov 13, 2021
@TaKO8Ki TaKO8Ki requested a review from estebank November 14, 2021 13:56
Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some trailing whitespace, but after fixing that r=me

check if `String` or `&String` or `&str`

Update compiler/rustc_typeck/src/check/method/suggest.rs

Co-authored-by: Esteban Kuber <estebank@users.noreply.github.com>

remove some trailing whitespace
@TaKO8Ki TaKO8Ki force-pushed the suggest-chars-on-attempt-to-iter branch from f103b93 to d562f48 Compare November 15, 2021 03:37
@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Nov 16, 2021

📌 Commit d562f48 has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 16, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Nov 16, 2021
…-iter, r=estebank

Suggest `&str.chars()` on attempt to `&str.iter()`

closes rust-lang#90786
This was referenced Nov 16, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 16, 2021
Rollup of 8 pull requests

Successful merges:

 - rust-lang#86455 (check where-clause for explicit `Sized` before suggesting `?Sized`)
 - rust-lang#90801 (Normalize both arguments of `equate_normalized_input_or_output`)
 - rust-lang#90803 (Suggest `&str.chars()` on attempt to `&str.iter()`)
 - rust-lang#90819 (Fixes incorrect handling of TraitRefs when emitting suggestions.)
 - rust-lang#90910 (fix getting the discriminant of a zero-variant enum)
 - rust-lang#90925 (rustc_mir_build: reorder bindings)
 - rust-lang#90928 (Use a different server for checking clock drift)
 - rust-lang#90936 (Add a regression test for rust-lang#80772)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit b17de50 into rust-lang:master Nov 16, 2021
@rustbot rustbot added this to the 1.58.0 milestone Nov 16, 2021
@TaKO8Ki TaKO8Ki deleted the suggest-chars-on-attempt-to-iter branch November 16, 2021 15:06
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

suggest &str.chars() on attempt to &str.iter()
7 participants