-
Notifications
You must be signed in to change notification settings - Fork 13k
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
type error method suggestions use whitelisted identity-like conversions #46461
Conversation
r? @arielb1 |
(rust_highfive has picked a reviewer for you, use r? to override) |
// "identity-like" conversion methods to be suggested here. | ||
// | ||
// FIXME (#46459 and #46460): ideally | ||
// `std::convert::Into::into` and `std::borrow:ToOwned` would |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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(); | ||
| ^^^^^^^^^^^^ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍
src/libsyntax/feature_gate.rs
Outdated
("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))), |
There was a problem hiding this comment.
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 :)
r? @estebank because you've already started reviewing :) It looks like there are some comments for you @zackmdavis ! Do they make sense? |
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). |
☔ The latest upstream changes (presumably #46641) made this pull request unmergeable. Please resolve the merge conflicts. |
06442d7
to
a26b63b
Compare
(update a26b63b postdates most recent discussion) |
ping @estebank, looks like this may be ready for another review! |
There was a problem hiding this 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()` |
There was a problem hiding this comment.
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.
📌 Commit a26b63b has been approved by |
src/libcore/array.rs
Outdated
@@ -42,8 +42,10 @@ use slice::{Iter, IterMut}; | |||
/// instead. | |||
pub unsafe trait FixedSizeArray<T> { | |||
/// Converts the array to immutable slice | |||
#[rustc_conversion_suggestion] |
There was a problem hiding this comment.
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.
@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. |
📌 Commit a26b63b has been approved by |
@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.
a26b63b
to
aba56dd
Compare
Ping from triage, @estebank. It looks like there are new commits for you to review! |
@bors r+ |
📌 Commit aba56dd has been approved by |
…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.
☀️ Test successful - status-appveyor, status-travis |
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 twomajor 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 hopingfor). 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 keepthe "same value" in the relevant sense, just converting the type. We
address the second problem by making
FnCtxt.probe_for_return_type
passthe
ProbeScope::AllTraits
toprobe_op
: this would seem to be safebecause 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'tactually work, and we don't know how to rule in
.to_owned()
where itwould. Issues #46459 and #46460 have been filed and are ref'd in a FIXME.
This is hoped to resolve #42929, #44672, and #45777.