Skip to content

Diagnostics: suggest alternatives when new constructor is not available #69512

Closed

Description

@yoshuawuyts

Failing Code

Consider the following code:

use std::net::TcpStream;

fn main() {
   let stream = TcpStream::new();
}

playground

This fails with an error that simply states that new is not available:

Compiling playground v0.0.1 (/playground)
error[E0599]: no function or associated item named `new` found for type `std::net::TcpStream` in the current scope
 --> src/main.rs:4:28
  |
4 |    let stream = TcpStream::new();
  |                            ^^^ function or associated item not found in `std::net::TcpStream`

error: aborting due to previous error

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 either TcpStream::connect or
TcpStream::connect_timeout:

use std::net::TcpStream;

fn main() {
   let stream = TcpStream::connect("localhost:8080");
}

Diagnostics Suggestions

When new is not available it'd be ideal if the compiler could suggest
alternatives. A first heuristic for which methods to suggest be methods on
the same struct that don't take any self params, have Self as their
return type, and aren't implemented through any trait.

For TcpStream this would include TcpStream::connect and
TcpStream::connect_timeout. But not TcpStream::try_clone,
TcpStream::from_raw_fd, TcpStream::from_raw_socket and
std::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:

Compiling playground v0.0.1 (/playground)
error[E0599]: no function or associated item named `new` found for type `std::net::TcpStream` in the current scope
 --> src/main.rs:4:28
  |
4 |    let stream = TcpStream::new();
  |                            ^^^ function or associated item not found in `std::net::TcpStream`
  = help: Did you mean to use `std::net::TcpStream::connect` or `std::net::TcpStream::connect_timeout`?

error: aborting due to previous error

cc/ @estebank

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Feb 27, 2020
  2. Centril commented on Feb 27, 2020

    @Centril
    Contributor

    Seems like #[doc(alias(...))]es should be added to the standard library to fix this.

  3. varkor commented on Feb 29, 2020

    @varkor
    Contributor

    This seems to be the same as #51398.

  4. Centril commented on Feb 29, 2020

    @Centril
    Contributor

    Closing in favor of that.

  5. yoshuawuyts commented on Mar 2, 2020

    @yoshuawuyts
    MemberAuthor

    Seems like #[doc(alias(...))]es should be added to the standard library to fix this.

    @Centril For the example I brought up, how would an #[doc(alias(...))] work? The new method does not exist on TcpStream. So it shouldn't qualify as an alias? Additionally this is intended as an automated mechanism to improve the quality of diagnostics, rather than a manual mechanism (like attributes) which would need to be opted into.

    While both cases do touch on improving diagnostics for missing methods, I don't think these are equivalent.

  6. Centril commented on Mar 2, 2020

    @Centril
    Contributor

    No but TcpStream::connect does exist, so e.g., #[doc(alias(TcpStream::new))] on it could add the information necessary (see e.g. #[doc(alias = "memcpy")] on copy_nonoverlapping) for the compiler to provide the diagnostic.

    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.

  7. reopened this on Mar 2, 2020
  8. added
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    on Mar 3, 2020
  9. estebank commented on Oct 21, 2023

    @estebank
    Contributor

    @yoshuawuyts what do you think of the following?

    Screenshot 2023-10-20 at 6 13 57 PM
    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(); //~ ERROR no function or associated item named `new` found
        |                            ^^^ 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> {
        |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
  10. added a commit that references this issue on Oct 21, 2023
    aa030d0
  11. yoshuawuyts commented on Oct 21, 2023

    @yoshuawuyts
    MemberAuthor

    @estebank oh I love it!

  12. added 4 commits that reference this issue on Oct 21, 2023
    1158985
    d26584d
    22633fe
    593276c
  13. added a commit that references this issue on Nov 7, 2023
    f926031
  14. added a commit that references this issue on Nov 7, 2023
    4e0fb98
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions