Compiler doesn't suggest missing method that is implemented for different type parameter #51518
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Consider the following API that allows us to control some piece of hardware, while providing some compile-time guarantees by tracking the hardware state as a type parameter:
Hardware
is created in theDisabled
state and needs to beenable
d before anything can be done with it.The following piece of code tries to
do_stuff
withoutenable
ing first:It fails with the following error message:
I think this error message is misleading, as the method does exist. It's just not available with this specific type parameter. This could be highly confusing to someone who isn't used to this kind of API.
I believe that the compiler should suggest the method, and note that it would be available, if the type parameter were different. Similarly to how it would suggest a trait method for a trait that isn't imported into the current scope. Ideally, I would like the compiler to suggest calling the
enable
method, as that would put the API into a state that would allowdo_stuff
to be called.I'm aware that method suggestions like this were removed some time ago (issue, PR). Adding an attribute to mark function that should be suggested for state transitions like this has been suggested before (@zackmdavis seemed interested in working on this). From my perspective as an API author, such an attribute would be an excellent way to improve the user experience when using APIs like this.
cc @oli-obk (We talked about this at RustFest. Sorry for taking so long to open this issue.)
The text was updated successfully, but these errors were encountered: