-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Associated types #1677
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 types #1677
Conversation
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.
LGMT!
ast::PathSegmentKind::Angle { type_ref, trait_ref } => { | ||
assert!(path.qualifier().is_none()); // this can only occur at the first segment | ||
|
||
// <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo |
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.
Let's add a TODO for <()>::default
syntax.
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.
I kind of forgot about that syntax!
@@ -91,6 +91,7 @@ impl ast::Attr { | |||
#[derive(Debug, Clone, PartialEq, Eq)] | |||
pub enum PathSegmentKind { | |||
Name(ast::NameRef), | |||
Angle { type_ref: Option<ast::TypeRef>, trait_ref: Option<ast::PathType> }, |
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.
Hm, should we perhaps introduce different variants for <Type>
and <Type as Trait>
flavors? I am also not too thrilled by Angle
name, but UFCS
is worse, and Qualified
(used by "official" grammar) is outright confusing.
Perhaps just call it Type
? That's what it is: <>
is a way to explicitly switch from expr grammar to types grammar.
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.
Hmm yeah I'm fine with Type
.
This adds three different representations, copied from the Chalk model: - `Ty::Projection` is an associated type projection written somewhere in the code, like `<Foo as Trait>::Bar`. - `Ty::UnselectedProjection` is similar, but we don't know the trait yet (`Foo::Bar`). - The above representations are normalized to their actual types during type inference. When that isn't possible, for example for `T::Item` inside an `fn foo<T: Iterator>`, the type is normalized to an application type with `TypeCtor::AssociatedType`.
I.e. `<T as Trait>::Foo`.
11d2d97
to
5af9691
Compare
bors r+ |
1677: Associated types r=flodiebold a=flodiebold This implements basic support for (fully qualified) associated type projections: - handle fully qualified paths like `<T as Trait>::AssocType` (basically desugaring to something like `Trait<Self=T>::AssocType`) - lower these to a new `Ty::Projection` enum variant - also introduce `Ty::UnselectedProjection` for cases like `T::AssocType` where the trait from which the type comes isn't specified, but these aren't handled further so far - in inference, normalize these projections using Chalk: basically, when encountering a type e.g. from a type annotation or signature, we replace these `Ty::Projection`s by type variables and add obligations to normalize the associated type Co-authored-by: Florian Diebold <[email protected]>
Build succeeded |
This implements basic support for (fully qualified) associated type projections:
<T as Trait>::AssocType
(basically desugaring to something likeTrait<Self=T>::AssocType
)Ty::Projection
enum variantTy::UnselectedProjection
for cases likeT::AssocType
where the trait from which the type comes isn't specified, but these aren't handled further so farTy::Projection
s by type variables and add obligations to normalize the associated type