Skip to content
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

Program fails to compile for confusing reasons #11901

Closed
alex opened this issue Jan 29, 2014 · 1 comment
Closed

Program fails to compile for confusing reasons #11901

alex opened this issue Jan 29, 2014 · 1 comment

Comments

@alex
Copy link
Member

alex commented Jan 29, 2014

(Sorry about the awful title).

The issue is that this program fails to compile:

use std::io::net::ip::SocketAddr;
use std::io::net::tcp::TcpListener;


fn main() {
    let addr: SocketAddr = from_str("127.0.0.1:8000").expect("address");
    let listener = TcpListener::bind(addr).expect("bind");
    let mut acceptor = listener.listen();
}

With the error:

x.rs:8:24: 8:42 error: type `std::io::net::tcp::TcpListener` does not implement any method in scope named `listen`

The fix for this is to add:

use std::io::{Acceptor, Listener};

This seems like a very poor UX, the user has to know to include some name that they do not use, and the compiler doesn't do anything to assist them in figuring it out.

@huonw
Copy link
Member

huonw commented Jan 29, 2014

This is a fundamental requirement of the traits; you can "add" methods to a type anywhere (including in different compilation units ("crates")), and so we need a way to make sure that we don't have name conflicts if two traits have methods of the same name and are implemented for a single type. To this end, a method from a trait is namespaced with the trait, rather than the types it is implemented on, so calling such a method requires importing the trait.

That said, it is a poor UX to just say that there's no such method, when the compiler does have enough information to be able to scan through all traits looking for methods with the appropriate name (at the very least) and preferably filtering to just traits that the current type implements. This has actually already been proposed in #7643, and so I'm going to close this issue as a dupe.

Thanks for taking the time to report this. :)

@huonw huonw closed this as completed Jan 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants