-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Diagnostics: suggest alternatives when new constructor is not available #69512
Copy link
Copy link
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Failing Code
Consider the following code:
playground
This fails with an error that simply states that
newis not available:Working Code
The intent is fairly clear: we want to construct a new
TcpStreamusing a method that follows the Rust naming conventions for constructors, but that method is not available. Instead the solution is likely to be eitherTcpStream::connectorTcpStream::connect_timeout:Diagnostics Suggestions
When
newis not available it'd be ideal if the compiler could suggestalternatives. A first heuristic for which methods to suggest be methods on
the same struct that don't take any
selfparams, haveSelfas theirreturn type, and aren't implemented through any trait.
For
TcpStreamthis would includeTcpStream::connectandTcpStream::connect_timeout. But notTcpStream::try_clone,TcpStream::from_raw_fd,TcpStream::from_raw_socketandstd::net::Incoming::next.I'm not too sure how to structure the help message exactly, but I could
imagine something along these lines might work:
cc/ @estebank