-
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
Nested associated type projection is overly conservative #38078
Comments
Note that the issue title may be overly specific; I haven't deeply investigated the cases where this kind of projection fails. |
In my experience at least |
FWIW, |
In terms of the compiler's implementation, this isn't really about projection. It's not actually getting that far. Rather, it fails when lowering the AST into types, which is the point where we have to pick a trait. @eddyb has been doing a lot of heroic work refactoring the type-end to remove that limitation, which is fairly tricky -- the problem is that to do it properly, one must reason about the where-clauses that are in scope and do trait selection, but to know what where-clauses are in scope, one must lower them, and that is what we are trying to do. To resolve this, @eddyb has been refactoring the compiler to cope with partially lowered sets of where-clauses, basically. |
So, is @eddyb's fix for this being developed openly somewhere it's possible to track progress (or help?) This issue is causing me some frustration, so I'd love to know what I could do to help resolve it. |
I think he wound up taking another approach. I'm not sure what's the shortest path here just now. |
Perhaps keying the |
This is a huge paper cut for me: I have a lot of code of the form There are two cases where this cuts me:
Having to disambiguate is the smaller paper cut. It would already be infinitely helpful if the error message would tell me if there is an actual ambiguity and where, or if it would tell me that there isn't an ambiguity. |
Maybe the problem is that what I understand by ambiguous (e.g. that two associated types are named equal) and what the compiler understands by ambiguous are different things. |
I've tried using syntax, that one would expect to work just fine, and have wound up with this issue. It's quite clear to any human that there exist no ambiguities in this code, yet the compiler unnecessarily requires extra (and ugly) syntax to accept the code. This is extremely inconvenient. |
I'm not sure if chiming in and saying "me too" at this point is more annoying than helpful, but me too :) I think this one's hard on novice Rustaceans because we're interpreting the situation as human error, as if we've unintentionally made the situation ambiguous and there's something we can do to correct it. |
I would also love this. I've been "shortening" it like this, but this way there is an extra associated type that structs have to implement for no reason:
|
At the very least, the error message should be improved; it gives bad advice: |
Is this issue one that someone could take on as a first issue for fixing the compiler or is this still in the area of experts? It seemed like something quite hard a while ago, but maybe progress has been made? If this is something easier than it used to be, could someone point me to the area of code that would need to be fixed? |
Sadly, not enough progress has been made for this to be readily doable yet. |
Has there been any progress on this front? Was just curious if there was/will be/is planned to have an update of some sort. |
Ah ha! No more leaking GDB internals to users*! *Assuming the user is using an architecture listed already implemented under the `arch` module, they don't have to dig through the GDB source for some obscure XML files. The unfortunate side-effect of this change is the really digusting type syntax which `Target` has to use. `<Self::Arch as Arch>::Usize` is really gnarly :sad: If rust-lang/rust#38078 ever gets fixed, the type could be re-written as `Self::Arch::Usize`, which is a _lot_ cleaner.
Will chalk resolve this? |
Still happens with Rust 1.45.2 Following code:
Outputs:
Is there any workaround? |
chalk will not resolve this without more work, though we did have some experimental ideas for how to resolve it. |
This also is a problem with non-nested types: trait T
{
type M : Sized;
}
struct S
{
some_var: u32
}
impl T for S
{
type M = u32;
}
fn main()
{
let v: S::M = 0;
} Output:
|
I'm not super convinced. I think it's reasonable to ask This kind of resolution is easy victim to upstream associated item changes. However for ergonomics it might be worth it? |
At least give me an error that tells me what to do? Because the current output is insanely bad for developer productivity; It should be telling me what options it thinks it has and why its considered ambiguous. |
Any progress on this? |
i've been dancing around this issue in some form or another for a few years and i only just realized that another workaround for this is writing a type alias to at least hide the process of walking rustc through the type relationships: pub trait ATrait {
type B: BTrait;
}
pub trait BTrait {
type C: CTrait;
}
pub trait CTrait {}
// this works! i can go simplify several crates now. yay.
pub type AToC<A> = <<A as ATrait>::B as BTrait>::C;
pub fn project<A: ATrait>() -> AToC<A> {
panic!("C");
} this is similar to tiby312's comment a few years ago but i hadn't fully considered that the type alias could be freestanding and organized elsewhere as appropriate. |
Just chiming in that it would be really nice if this was possible. I came up with a nice refactor that turned out to be not practical because it's not ergonomic to use a type nested in the trait in a trait implementation. It would be really nice if this was possible. |
Am I right in thinking that this is caused by rustc not having a notion of 'trait variables' (akin to type variables) within its solver? I've added support for trait variables to my own lang and it seems to be pretty reliably capable of arbitrary associated type projection like this. |
The ambiguous associated types error sometimes fires in cases where it shouldn't be ambiguous ([1]), which can make things clunky when working with chained associated types (e.g. `Op::FTy::Int::*` does not work). Add helper types that we can use instead of the full syntax. There aren't too many cases in-crate now but this is relevant for some open PRs. [1]: rust-lang/rust#38078
The following setup
produces the error
It would be great for the less explicit projection to work here.
cc @nikomatsakis
The text was updated successfully, but these errors were encountered: