-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow Self
to appear in the where clause of trait impls
#1647
Conversation
Slightly related: #1588 This seems like a good idea. The only backcompat thing I could come up with is this, and it already doesn't compile, so no issue: trait Foo {
fn foo(&self);
}
trait Bar {}
struct Baz;
struct Quux;
impl Foo for Baz {
fn foo(&self) {
impl Bar for Quux where Self: Sized {}
// before this RFC: erroneous attempt to use outer `Self` (i.e. `Baz`)
// after this RFC: `Self` here refers to `Quux`
}
}
fn main() {} |
IIUC, the associated type thing is weird in that |
Not really any different than having |
Say a different impl with a different associated type succeeds? |
If |
|
} | ||
|
||
impl<T, U, V> MyTrait for SomeStruct<T, U, V> where | ||
SomeOtherStruct<T, U, V>: SomeBound, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you could change this from SomeOtherStruct<T, U, V>
to <Self as MyTrait>::MyType
, that would make clear the current verbosity is not quite as bad :).
@sgrif ah, sorry for the noise then. I think the goal is clear enough, but I commented on one line you might consider changing then to reflect the status-quo as you just clarified it to be. All that straightened out, I am definitely for this! |
In general we already allow When it comes to associated types, though, I am a bit dubious about this paragraph:
This seems to suggest some kind of distinct pathway for handling associated types in this location. I am not too keen on that, I would prefer for this to fall out from normalization (and indeed I think it very well might, though there may be some issues with resolving recursive references -- we could probably do better in this respect than we do in general). |
@rfcbot fcp merge (I hope I'm doing this right!) I agree with @nikomatsakis that this seems like a bug. The 'type header' of an impl block (its where clauses, etc) should be considered a part of that impl block, and the |
@rfcbot concern "Include type parameters" I think this should also be accepted: trait Foo<T> { }
struct Bar;
impl Foo<Self> for Bar { }
// equivalent to:
impl Foo<Bar> for Bar { } And this: trait Foo { }
struct Bar<T>(Box<T>);
impl Foo for Bar<Self> { }
// equivalent to
impl Foo for Bar<Bar> { } |
EDIT: @withoutboats was faster at typing, but I'll still write this. Technically,
The only question is "How confusing this may be?", especially if the impl lives in another impl with its own |
|
@petrochenkov of course 😅 it should fail with some recursive type definition error, and not a "use of |
That... sounds very reasonable. |
Team member @withoutboats has proposed to merge this. The next step is review by the rest of the tagged teams: Concerns:
Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Wait, I am confused. The RFC concerns using |
@nikomatsakis I would like to see the RFC amended to support that. |
For reference, trait items, unlike impl items, already permit
|
@withoutboats huh, really? Interesting. I guess it seems intuitive enough what it means, even if I find it a bit surprising. Presumably e.g., impl<T: Foo<Self>> Bar for u8 |
@nikomatsakis Yea. I agree that it seems weird to actually do, but we have a history of |
@withoutboats yeah, seems ok. I can get behind it. |
@rfcbot resolved "Include type parameters" |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
@sgrif Could you please write a patch to the Rust Reference for this change? Perhaps it would be an empty patch since the Rust Reference already says “All traits define an implicit type parameter Self that refers to ʻthe type that is implementing this interface,ʻ" which supports the conclusion above that this is fixing a bug. |
Yay! |
Rendered