-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
RFC: limit suggestions to a white-list of functions or to as_
, to_
, and into_
methods
#42929
Comments
I actually liked these verbose suggestions. Couldn't we just order the suggestions better? I mean place all the methods fulfilling a whitelist of prefixes on the front. In the command line diagnostics, only a few suggestions show up anyway, so most weird ones should end up being hidden. Tools using the json output could get all the suggestions and users could browse them. I agree that we should blacklist anything like |
In my opinion these suggestions should primarily come from conversion traits like
No mention of |
My intution is that we should only be suggesting "identity" conversions of various kinds. Things like |
Also, do we not suggest things from traits? If so, that's a pretty serious shortcoming. =) That was, indeed, sort of the whole idea here. Certainly |
I believe I've made some progress on this. We currently filter suggestions from A question: does anyone know how we could look up the |
(illustration of work-in-progress discussed in previous comment) Before: Extending the current suggestion list with methods from traits (zackmdavis/rust@9df1ed91b6): Using the structured diagnostic suggestions, only using suggestions from traits (zackmdavis/rust@3b1ab62363): |
Structured suggestions look cool, but I think the biggest problem we have is that the suggestions are often nonsensical. For example, I feel like suggestions I'm also pretty nervous about suggesting people invoke |
Maybe we want to add an attribute on the methods that we want to be used in suggestions, and only put it on the desirable methods? |
@arielb1 I recall that being suggested before, but don't think anyone has created a ticket or gone ahead and implemented that. I think that's gonna end up being the only reasonable option we'll have. |
@nikomatsakis I think that if we added an attribute for "safe" methods, then it'd be reasonable to use the suggestion output, otherwise I'd just stick to using the list output. |
Attributes are looking promising (zackmdavis/rust@b3f1b4a7), but can't PR yet because the UI test diffs show us overzealously (not to mention mysteriously) suggesting |
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.
…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.
The "here are some functions that may meet your needs" tips are rather broad. I think we should impose some limits. As a spin-off from #42764, consider this (same) example:
Right now this gives:
checked_abs()
andchecked_neg()
seem like very silly suggestions to me. Similarly, #42746 points out that suggestingunwrap()
is sort of inappropriate. I think it would be better if used a whitelist whitelist of approved conversion functions, or else if we limited them to the prefixesas_
,to_
, orinto_
. The latter is sort of codifying an existing convention -- I personally see no problem with that, since this is just a compiler hint. It has the plus and minus that it would apply rather broadly (i.e., to functions outside the stdlib), where a whitelist obviously wouldn't unless we standardized it.The text was updated successfully, but these errors were encountered: