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

Add suggestion to use &*var when &str: From<String> is expected #59268

Merged
merged 3 commits into from
Mar 28, 2019

Conversation

estebank
Copy link
Contributor

Fix #53879.

@rust-highfive
Copy link
Collaborator

r? @GuillaumeGomez

(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 Mar 18, 2019
@GuillaumeGomez
Copy link
Member

Wo that looks nice! I'd just like confirmation from the @rust-lang/docs team but otherwise looks good to me! r=me if everything's fine for someone else.

Copy link
Member

@QuietMisdreavus QuietMisdreavus left a comment

Choose a reason for hiding this comment

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

Woah, this is nice! It would be cool if we could get this kind of message for any Deref relation, but this should take care of one of the most common cases. (Maybe one for Vec and &[T] would get the other most common Deref relation? Just thinking out loud, since we're already making changes here.)

#[rustc_on_unimplemented(
on(
all(_Self="&str", T="std::string::String"),
note="you can coerce a `{T}` into a `{Self}` by writing `&*variable`"
Copy link
Member

Choose a reason for hiding this comment

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

Is the use of "variable" in error messages something we've already done? It seems odd to have that specific default name.

Copy link
Contributor Author

@estebank estebank Mar 21, 2019

Choose a reason for hiding this comment

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

We haven't done it like this but we do have some messages that use placeholders when the snippet is not available, but they are rarely seen because we have access to the snippet most of the time. Because this is an on_unimplemented message we don't have access to the snippet at all. I'm not too happy with the copy so wording changes are totally welcome.

Copy link
Member

Choose a reason for hiding this comment

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

I skimmed through the uses of rustc_on_unimplemented to see what some other messages do, and i liked this one message in the Iterator trait:

label="borrow the array with `&` or call `.iter()` on it to iterate over it",
note="arrays are not iterators, but slices like the following are: `&[1, 2, 3]`"

To make it totally fit in, i would write something like "to coerce a `{T}` into a `{Self}`, use `&*`", but IMO your existing one can work as-is by using "foo" instead of "variable".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to "to coerce a {T} into a {Self}, use &* as a prefix" as just suggesting &* can confuse newcomers as to where it should go.

@estebank
Copy link
Contributor Author

Maybe one for Vec and &[T] would get the other most common Deref relation? Just thinking out loud, since we're already making changes here.

For arrays we already have a way of targeting any type of array (using [] as the type argument value), but for slices we don't so that would have to be added to rustc_on_unimplemented. Shouldn't be too hard though.

@QuietMisdreavus
Copy link
Member

Maybe one for Vec and &[T] would get the other most common Deref relation? Just thinking out loud, since we're already making changes here.

For arrays we already have a way of targeting any type of array (using [] as the type argument value), but for slices we don't so that would have to be added to rustc_on_unimplemented. Shouldn't be too hard though.

Ah, in that case i won't hold up this PR any more. Thanks for doing this!

@bors r+

@bors
Copy link
Contributor

bors commented Mar 26, 2019

📌 Commit e929d19 has been approved by QuietMisdreavus

@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 Mar 26, 2019
Centril added a commit to Centril/rust that referenced this pull request Mar 27, 2019
…avus

Add suggestion to use `&*var` when `&str: From<String>` is expected

Fix rust-lang#53879.
Centril added a commit to Centril/rust that referenced this pull request Mar 27, 2019
…avus

Add suggestion to use `&*var` when `&str: From<String>` is expected

Fix rust-lang#53879.
cuviper added a commit to cuviper/rust that referenced this pull request Mar 28, 2019
…avus

Add suggestion to use `&*var` when `&str: From<String>` is expected

Fix rust-lang#53879.
bors added a commit that referenced this pull request Mar 28, 2019
Rollup of 18 pull requests

Successful merges:

 - #57293 (Make some lints incremental)
 - #57565 (syntax: Remove warning for unnecessary path disambiguators)
 - #58253 (librustc_driver => 2018)
 - #58837 (librustc_interface => 2018)
 - #59268 (Add suggestion to use `&*var` when `&str: From<String>` is expected)
 - #59283 (Make ASCII case conversions more than 4× faster)
 - #59284 (adjust MaybeUninit API to discussions)
 - #59372 (add rustfix-able suggestions to trim_{left,right} deprecations)
 - #59390 (Make `ptr::eq` documentation mention fat-pointer behavior)
 - #59393 (Refactor tuple comparison tests)
 - #59420 ([CI] record docker image info for reuse)
 - #59421 (Reject integer suffix when tuple indexing)
 - #59430 (Renames `EvalContext` to `InterpretCx`)
 - #59439 (Generalize diagnostic for `x = y` where `bool` is the expected type)
 - #59449 (fix: Make incremental artifact deletion more robust)
 - #59451 (Add `Default` to `std::alloc::System`)
 - #59459 (Add some tests)
 - #59460 (Include id in Thread's Debug implementation)

Failed merges:

r? @ghost
@bors bors merged commit e929d19 into rust-lang:master Mar 28, 2019
@estebank estebank deleted the from-string branch November 9, 2023 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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.

5 participants