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

type error method suggestions use whitelisted identity-like conversions #46461

Merged

Conversation

zackmdavis
Copy link
Member

method_jamboree_summit

Previously, on a type mismatch (and if this wasn't preëmpted by a
higher-priority suggestion), we would look for argumentless methods
returning the expected type, and list them in a help note. This had two
major shortcomings: firstly, a lot of the suggestions didn't really make
sense (if you used a &str where a String was expected,
.to_ascii_uppercase() is probably not the solution you were hoping
for). Secondly, we weren't generating suggestions from the most useful
traits! We address the first problem with an internal
#[rustc_conversion_suggestion] attribute meant to mark methods that keep
the "same value" in the relevant sense, just converting the type. We
address the second problem by making FnCtxt.probe_for_return_type pass
the ProbeScope::AllTraits to probe_op: this would seem to be safe
because grep reveals no other callers of probe_for_return_type.

Also, structured suggestions are pretty and good for RLS and friends.

Unfortunately, the trait probing is still not all one would hope for: at a
minimum, we don't know how to rule out into() in cases where it wouldn't
actually work, and we don't know how to rule in .to_owned() where it
would. Issues #46459 and #46460 have been filed and are ref'd in a FIXME.

This is hoped to resolve #42929, #44672, and #45777.

@zackmdavis
Copy link
Member Author

r? @arielb1

@rust-highfive
Copy link
Collaborator

r? @michaelwoerister

(rust_highfive has picked a reviewer for you, use r? to override)

@kennytm kennytm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 3, 2017
// "identity-like" conversion methods to be suggested here.
//
// FIXME (#46459 and #46460): ideally
// `std::convert::Into::into` and `std::borrow:ToOwned` would
Copy link
Contributor

Choose a reason for hiding this comment

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

Could into() be suggested if methods.len() == 0 with a note (not a suggestion) as a temporary hack? (Not saying that it'd be a great idea, just thinking out loud. In text.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's not.

@@ -183,7 +183,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
scope_expr_id);
let method_names =
self.probe_op(span, mode, None, Some(return_type), IsSuggestion(true),
self_ty, scope_expr_id, ProbeScope::TraitsInScope,
self_ty, scope_expr_id, ProbeScope::AllTraits,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we do this if and only if TraitsInScope doesn't yield any suggestions?

Copy link
Member Author

Choose a reason for hiding this comment

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

With no other callers, I feel OK about changing this.

@@ -19,5 +19,5 @@ error[E0308]: mismatched types
= note: expected type `&Bottom`
found type `&Top`

error: aborting due to 3 previous errors
error: aborting due to 51 previous errors
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh boy, what happened here!? Given how much of an improvement this is, I wouldn't block on this, but it is worrisome if this PR is generating this many non-emitted diagnostics.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would actually consider this blocking (changing behavior isn't always wrong, but changing behavior that no one understands is pretty bad).

Copy link
Member Author

Choose a reason for hiding this comment

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

It turns out that we don't emit exactly-equal (by hash) diagnostics, the method-probing code uses Autoderef, and Autoderef doesn't know when it's already set the recursion-limit error.

Copy link
Member Author

Choose a reason for hiding this comment

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

... or rather, it's not that Autoderef needs to know when it's already emitted the error, but rather that we instantiate Autoderef many times and hit the limit many times when searching for suggestions.

36 | f = box f.as_slice();
| ^^^^^^^^^^^
36 | f = box f.to_string();
| ^^^^^^^^^^^^
Copy link
Contributor

Choose a reason for hiding this comment

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

😍

("wasm_import_memory", Whitelisted, Gated(Stability::Unstable,
"wasm_import_memory",
"wasm_import_memory attribute is currently unstable",
cfg_fn!(wasm_import_memory))),
cfg_fn!(wasm_import_memory))),
Copy link
Contributor

Choose a reason for hiding this comment

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

Fix indentation (either change the indentation of the entire thing or revert to what it was :)

@carols10cents
Copy link
Member

r? @estebank

because you've already started reviewing :)

It looks like there are some comments for you @zackmdavis ! Do they make sense?

@rust-highfive rust-highfive assigned estebank and unassigned arielb1 Dec 12, 2017
@carols10cents carols10cents added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 12, 2017
@zackmdavis
Copy link
Member Author

Yes, thanks, I haven't forgotten about this; I'm slow to address comments because every time I look at this issue it takes me a long time to get over the existential despair of how hard this codebase is to understand (for me).

@bors
Copy link
Contributor

bors commented Dec 15, 2017

☔ The latest upstream changes (presumably #46641) made this pull request unmergeable. Please resolve the merge conflicts.

@kennytm kennytm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 20, 2017
@zackmdavis zackmdavis force-pushed the elemental_method_suggestion_jamboree branch 2 times, most recently from 06442d7 to a26b63b Compare December 24, 2017 03:53
@zackmdavis
Copy link
Member Author

(update a26b63b postdates most recent discussion)

@alexcrichton
Copy link
Member

ping @estebank, looks like this may be ready for another review!

@alexcrichton alexcrichton added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 4, 2018
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.

@bors r+

--> $DIR/conversion-methods.rs:21:42
|
21 | let _should_the_glee_glaze: String = 2; // Perhaps surprisingly, we suggest .to_string() here
| ^- help: try using a conversion method: `.to_string()`
Copy link
Contributor

Choose a reason for hiding this comment

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

Going through all of the examples, it seems to me that the output would be slightly easier to read if the help pointed at the main span, and the suggestion text included the main span's snippet:

 --> $DIR/conversion-methods.rs:24:46
   |
24 |     let _in_deaths_stiff_stare: Vec<usize> = &[1, 2, 3]; //~ ERROR mismatched types
   |                                              ^^^^^^^^^^
   |                                              |
   |                                              expected struct `std::vec::Vec`, found reference
   |                                              help: try using a conversion method: `&[1, 2, 3].to_vec()`

That being said, I think that can be done in a subsequent PR.

@bors
Copy link
Contributor

bors commented Jan 4, 2018

📌 Commit a26b63b has been approved by estebank

@@ -42,8 +42,10 @@ use slice::{Iter, IterMut};
/// instead.
pub unsafe trait FixedSizeArray<T> {
/// Converts the array to immutable slice
#[rustc_conversion_suggestion]
Copy link
Member

Choose a reason for hiding this comment

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

FixedSizeArray is unstable and not in the prelude so it souldn't be suggested.

@estebank
Copy link
Contributor

estebank commented Jan 4, 2018

@bors r-, @zackmdavis could you follow @ollie27's comment? While you're at it, could you tweak the span as suggested in the earlier review?

@ollie27 I'd like the suggestion to be maybe there in nightly, but its just easier to take it out in this PR. Thanks for catching that.

@bors
Copy link
Contributor

bors commented Jan 4, 2018

📌 Commit a26b63b has been approved by estebank

@estebank
Copy link
Contributor

estebank commented Jan 4, 2018

@bors r-

Previously, on a type mismatch (and if this wasn't preëmpted by a
higher-priority suggestion), we would look for argumentless methods
returning the expected type, and list them in a `help` note.

This had two major shortcomings. Firstly, a lot of the suggestions didn't
really make sense (if you used a &str where a String was expected,
`.to_ascii_uppercase()` is probably not the solution you were hoping
for). Secondly, we weren't generating suggestions from the most useful
traits!

We address the first problem with an internal
`#[rustc_conversion_suggestion]` attribute meant to mark methods that keep
the "same value" in the relevant sense, just converting the type. We
address the second problem by making `FnCtxt.probe_for_return_type` pass
the `ProbeScope::AllTraits` to `probe_op`: this would seem to be safe
because grep reveals no other callers of `probe_for_return_type`.

Also, structured suggestions are preferred (because they're pretty, but
also for RLS and friends).

Also also, we make the E0055 autoderef recursion limit error use the
one-time-diagnostics set, because we can potentially hit the limit a lot
during probing. (Without this,
test/ui/did_you_mean/recursion_limit_deref.rs would report "aborting due to
51 errors").

Unfortunately, the trait probing is still not all one would hope for: at a
minimum, we don't know how to rule out `into()` in cases where it wouldn't
actually work, and we don't know how to rule in `.to_owned()` where it
would. Issues rust-lang#46459 and rust-lang#46460 have been filed and are ref'd in a FIXME.

This is hoped to resolve rust-lang#42929, rust-lang#44672, and rust-lang#45777.
@zackmdavis zackmdavis force-pushed the elemental_method_suggestion_jamboree branch from a26b63b to aba56dd Compare January 7, 2018 01:17
@shepmaster
Copy link
Member

Ping from triage, @estebank. It looks like there are new commits for you to review!

@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Jan 13, 2018

📌 Commit aba56dd has been approved by estebank

@shepmaster shepmaster 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 Jan 13, 2018
@bors
Copy link
Contributor

bors commented Jan 13, 2018

⌛ Testing commit aba56dd with merge 6eff103...

bors added a commit that referenced this pull request Jan 13, 2018
…e, r=estebank

 type error method suggestions use whitelisted identity-like conversions

![method_jamboree_summit](https://user-images.githubusercontent.com/1076988/33523646-e5c43184-d7c0-11e7-98e5-1bff426ade86.png)

Previously, on a type mismatch (and if this wasn't preëmpted by a
higher-priority suggestion), we would look for argumentless methods
returning the expected type, and list them in a `help` note. This had two
major shortcomings: firstly, a lot of the suggestions didn't really make
sense (if you used a &str where a String was expected,
`.to_ascii_uppercase()` is probably not the solution you were hoping
for). Secondly, we weren't generating suggestions from the most useful
traits! We address the first problem with an internal
`#[rustc_conversion_suggestion]` attribute meant to mark methods that keep
the "same value" in the relevant sense, just converting the type. We
address the second problem by making `FnCtxt.probe_for_return_type` pass
the `ProbeScope::AllTraits` to `probe_op`: this would seem to be safe
because grep reveals no other callers of `probe_for_return_type`.

Also, structured suggestions are pretty and good for RLS and friends.

Unfortunately, the trait probing is still not all one would hope for: at a
minimum, we don't know how to rule out `into()` in cases where it wouldn't
actually work, and we don't know how to rule in `.to_owned()` where it
would. Issues #46459 and #46460 have been filed and are ref'd in a FIXME.

This is hoped to resolve #42929, #44672, and #45777.
@bors
Copy link
Contributor

bors commented Jan 13, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: estebank
Pushing 6eff103 to master...

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.

RFC: limit suggestions to a white-list of functions or to as_, to_, and into_ methods