Skip to content

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

Merged
merged 7 commits into from
Aug 12, 2019
Merged

Associated types #1677

merged 7 commits into from
Aug 12, 2019

Conversation

flodiebold
Copy link
Member

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::Projections by type variables and add obligations to normalize the associated type

@flodiebold flodiebold requested a review from matklad August 11, 2019 14:07
Copy link
Member

@matklad matklad left a 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
Copy link
Member

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.

Copy link
Member Author

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> },
Copy link
Member

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.

Copy link
Member Author

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`.
@flodiebold
Copy link
Member Author

bors r+

bors bot added a commit that referenced this pull request Aug 12, 2019
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]>
@bors
Copy link
Contributor

bors bot commented Aug 12, 2019

Build succeeded

@bors bors bot merged commit 5af9691 into rust-lang:master Aug 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants