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

Relax Trait Coherence rules to allow the implementation of a trait on generic types where the type must impl another trait owned by the current crate #1124

Open
jnicholls opened this issue May 17, 2015 · 6 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@jnicholls
Copy link

The title is lengthy, but it boils down to this:

If I have a trait MyRead and I want to implement Read for all T: MyRead, I should be able to do so because I own MyRead and the impl of Read would (should?) only come into scope for type T: MyRead if and only if MyRead is also in scope.

There must be a major hole I am missing, but this would seriously reduce code in a lot of libraries, i.e. implementing traits onto owned types individually.

@ruuda
Copy link

ruuda commented May 17, 2015

That would not work: suppose I define a type T in a new crate my_crate. Then I can implement your_crate::MyRead and std::io::Read for it. MyRead does not have to be in scope for this. Now in a third crate, I use my_crate::T, use std::io::Read, and use your_crate::MyRead. Now there would be two conflicting implementations of io::Read.

@jnicholls
Copy link
Author

Hey, I didn't say it was perfect :) It could definitely work if we have a mechanism to explicitly specify which implementation to use. That'll come one day, because there's going to be a lot of ask for it.

@ruuda
Copy link

ruuda commented May 17, 2015

If you have a way to tell the compiler which implementation to use, I think you could circumvent all coherence rules and have multiple trait implementations for a type. Then a user of conflicting implementations would have to resolve the ambiguity, and this means that linking a new crate can cause a compile error. As described here, it was an explicit goal to avoid these problems. (Note that this does not mean these goals cannot change, I believe the point about overloading does not hold in Rust 1.0.0 due to multidispatch.)

@glaebhoerl
Copy link
Contributor

This would be probably be OK with some form of sealed traits though.

@aldanor
Copy link

aldanor commented May 24, 2015

Looks like something like sealed traits would work here indeed – there were a few preceding RFCs as well but they all seemed to be closed or died off.

@nrc nrc added the T-lang Relevant to the language team, which will review and decide on the RFC. label Aug 29, 2016
@Restioson
Copy link

Sorry for the necro, but with regards to 'which impl to choose', one could restrict the generic implementation to being a default implementation (specialisation)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

6 participants