-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Diagnostics: suggest alternatives when new
constructor is not available
#69512
Comments
Seems like |
This seems to be the same as #51398. |
Closing in favor of that. |
@Centril For the example I brought up, how would an While both cases do touch on improving diagnostics for missing methods, I don't think these are equivalent. |
No but But I suppose the heuristic you've suggested might work without attributes, although I suspect it would suggest too many false positives, but let's reopen I guess. |
@yoshuawuyts what do you think of the following? ![]()
|
When we have a resolution error when looking at a fully qualified path on a type, look for all associated functions on inherent impls that return `Self` and mention them to the user. Fix rust-lang#69512.
@estebank oh I love it! |
When we have a resolution error when looking at a fully qualified path on a type, look for all associated functions on inherent impls that return `Self` and mention them to the user. Fix rust-lang#69512.
When we have a resolution error when looking at a fully qualified path on a type, look for all associated functions on inherent impls that return `Self` and mention them to the user. Fix rust-lang#69512.
When we have a resolution error when looking at a fully qualified path on a type, look for all associated functions on inherent impls that return `Self` and mention them to the user. Fix rust-lang#69512.
When we have a resolution error when looking at a fully qualified path on a type, look for all associated functions on inherent impls that return `Self` and mention them to the user. Fix rust-lang#69512.
When not finding assoc fn on type, look for builder fn When we have a resolution error when looking at a fully qualified path on a type, look for all associated functions on inherent impls that return `Self` and mention them to the user. ``` error[E0599]: no function or associated item named `new` found for struct `TcpStream` in the current scope --> tests/ui/resolve/fn-new-doesnt-exist.rs:4:28 | 4 | let stream = TcpStream::new(); | ^^^ function or associated item not found in `TcpStream` | note: if you're trying to build a new `TcpStream` consider using one of the following associated functions: TcpStream::connect TcpStream::connect_timeout --> /home/gh-estebank/rust/library/std/src/net/tcp.rs:156:5 | 156 | pub fn connect<A: ToSocketAddrs>(addr: A) -> io::Result<TcpStream> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 172 | pub fn connect_timeout(addr: &SocketAddr, timeout: Duration) -> io::Result<TcpStream> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` Fix rust-lang#69512.
Failing Code
Consider the following code:
playground
This fails with an error that simply states that
new
is not available:Working Code
The intent is fairly clear: we want to construct a new
TcpStream
using a method that follows the Rust naming conventions for constructors, but that method is not available. Instead the solution is likely to be eitherTcpStream::connect
orTcpStream::connect_timeout
:Diagnostics Suggestions
When
new
is 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
self
params, haveSelf
as theirreturn type, and aren't implemented through any trait.
For
TcpStream
this would includeTcpStream::connect
andTcpStream::connect_timeout
. But notTcpStream::try_clone
,TcpStream::from_raw_fd
,TcpStream::from_raw_socket
andstd::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
The text was updated successfully, but these errors were encountered: