-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Bringing a trait alias into scope doesn't allow calling methods from its component traits #56485
Comments
Thanks for opening this. @Centril, what does your intuition say about this? |
@alexreg The thing I'm currently thinking about is what makes sense when you extend it beyond this simple example; in particular, if we say: In other words, consider: #![feature(trait_alias)]
mod alpha {
pub trait A { fn foo(); }
pub trait B { fn foo(); }
pub trait C = A + B;
}
use alpha::C; (this incidentally causes an ICE) |
@nikomatsakis Thoughts on how feasible it would be to make this work, given the current state of the import system (about which I know little)? |
If I were to import an alias, I'd likely often do so to call the methods off the trait as well as to set bounds. It'd be surprising if it didn't. If there is a name conflict, we can differentiate the way we do already: using UFCS with the specific trait, instead of the alias. Is this the last thing needed to decide on for stabilization? The tracking issue makes it look that :D |
@seanmonstar That's the last "feature" that really needs to be implemented, yeah. I'd be glad to help fix it, but I'd need some advice from someone who understand the import system, really. I've not touched that before. |
I was playing around with this, trying to discover where the error happens, when I found that this doesn't happen if you make the function generic on the trait. fn generic<T: Bar>(object: T) {
// works!
object.foo();
}
fn concrete_to_generic(object: Qux) {
// works!
generic(object);
} As for if the trait alias contains multiple traits with the same name, the method conflict is already as expected: |
I'm not sure what you mean. Could you post a complete example on the playground? |
Seems like nightly on the playground is timing out for me, but works locally: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=92e4a4878c40c2a7a6af7a04f9cfd9ed |
Ah right, I see. That makes sense because the type system resolves the type alias then, and not name resolution. Nightly on the playground has been broken for a while now... |
@seanmonstar @Centril #![feature(trait_alias)]
mod alpha {
pub trait A { fn foo() {} }
pub trait B { fn foo() {} }
// pub trait C = A + B;
impl A for u8 {}
impl B for u8 {}
}
// use alpha::C;
use alpha::*;
fn main() {
u8::foo(); // ERROR multiple applicable items in scope
} , the ambiguity error is already reported during resolution of a specific method in this case. |
One interesting effect is that the traits become in scope in the module that defines (rather than imports) the alias. #![feature(trait_alias)]
mod m {
pub trait A { fn foo() {} }
pub trait B { fn bar() {} }
impl A for u8 {}
impl B for u8 {}
}
trait C = m::A + m::B;
fn main() {
u8::foo(); // OK
} |
…lexreg resolve: collect trait aliases along with traits It seems trait aliases weren't being collected as `TraitCandidates` in resolve, this should change that. (I can't compile the full compiler locally, so relying on CI...) Fixes rust-lang#56485 r? @alexreg
…lexreg resolve: collect trait aliases along with traits It seems trait aliases weren't being collected as `TraitCandidates` in resolve, this should change that. (I can't compile the full compiler locally, so relying on CI...) Fixes rust-lang#56485 r? @alexreg
(playground) currently produces an error:
cc #41517
The text was updated successfully, but these errors were encountered: