-
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
Associated type lifetime bounds are underconstrained #22246
Comments
cc @japaric |
The problem arises from the "implied region bounds" rules. In the older formulation (or an equivalent one, like http://is.gd/L4uIcH), you wind up with the owned type I see a couple of ways to fix this:
The difference between 1 and 2 is that 2 doesn't require inspecting the contents of the struct, just the where-clauses declared on the struct. I have a mild preference for this -- I originally designed the rules to use variance for the same reason, basically that trawling through the contents feels like it exposes more of the guts than I would like, and raises other questions. The downside of option 2 is that if there are associated types that don't represent data reachable from Anyway, just some in-progress thoughts. I think mocking up option 2 wouldn't be all that hard. |
Somewhat simplified test case: use std::ops::Deref;
pub trait ToOwned {
type Owned: Borrow<Self>;
fn to_owned(&self) -> Self::Owned;
}
pub trait Borrow<Borrowed> {
fn borrow(&self) -> &Borrowed;
}
pub struct Foo<B:ToOwned> {
owned: B::Owned
}
fn foo<B:ToOwned>(this: &Foo<B>) -> &B {
this.owned.borrow()
}
fn main() { } |
I've implemented option 2, which I think is the right call. Basically the idea is that if you have a struct with where clauses, we will require that any associated types of traits that appear in those where clauses must outlive the struct itself (as they could potentially be embedded within the struct). So if you have: trait Foo { type T; ... }
struct Bar<U:Foo> { ... } then if some instance of |
that it produces "outlives" relations for associated types. Add several tests relating to rust-lang#22246.
…soc-types, r=nikomatsakis Take 2. This PR includes a bunch of refactoring that was part of an experimental branch implementing [implied bounds]. That particular idea isn't ready to go yet, but the refactoring proved useful for fixing #22246. The implied bounds branch also exposed #22110 so a simple fix for that is included here. I still think some more refactoring would be a good idea here -- in particular I think most of the code in wf.rs is kind of duplicating the logic in implicator and should go, but I decided to post this PR and call it a day before diving into that. I'll write a bit more details about the solutions I adopted in the various bugs. I patched the two issues I was concerned about, which was the handling of supertraits and HRTB (the latter turned out to be fine, so I added a comment explaining why.) r? @pnkfelix (for now, anyway) cc @aturon [implied bounds]: http://smallcultfollowing.com/babysteps/blog/2014/07/06/implied-bounds/
…imes-of-assoc-types Take 2. This PR includes a bunch of refactoring that was part of an experimental branch implementing [implied bounds]. That particular idea isn't ready to go yet, but the refactoring proved useful for fixing rust-lang#22246. The implied bounds branch also exposed rust-lang#22110 so a simple fix for that is included here. I still think some more refactoring would be a good idea here -- in particular I think most of the code in wf.rs is kind of duplicating the logic in implicator and should go, but I decided to post this PR and call it a day before diving into that. I'll write a bit more details about the solutions I adopted in the various bugs. I patched the two issues I was concerned about, which was the handling of supertraits and HRTB (the latter turned out to be fine, so I added a comment explaining why.) r? @pnkfelix (for now, anyway) cc @aturon [implied bounds]: http://smallcultfollowing.com/babysteps/blog/2014/07/06/implied-bounds/
This can be closed since #22436 has fixed it. |
Thanks @edwardw! |
The following code:
fails with:
The same code not using associated types does not have this problem.
The text was updated successfully, but these errors were encountered: