Skip to content

[Sema] Make typealiases in protocols available for nested type lookup. #2885

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 1 commit into from
Jun 7, 2016

Conversation

gregomni
Copy link
Contributor

@gregomni gregomni commented Jun 4, 2016

What's in this pull request?

protocol TestProto {
  typealias X = Int
}

struct Foo : TestProto {
  let a : X
}

This involved a couple complications, mostly making the distinction between typealiases whose definitions contain associated types and those which don't:

  • Only check final component of compound idents for existential-typeness, so that it is legal to refer to a nested typealias inside the existential.
  • New diagnosis for trying to use typealias which is an alias for an associated type outside of the defining protocol.
  • Add check on member lookup to omit typealiases to assoc types, similar to existing treatment of assoc types.
  • Added test.

This PR arises out of the conversation in this pull swiftlang/swift-evolution#321 where @lattner was surprised that this didn't already work (I was surprised too - oops.).

Also note that all of this is currently disabled by default and enabled by the -enable-protocol-typealiases flag (done in #2803).

Resolved bug number: (SR-)


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
All supported platforms @swift-ci Please smoke test and merge
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
All supported platforms @swift-ci Please test and merge
OS X platform @swift-ci Please test OS X platform
OS X platform @swift-ci Please benchmark
Linux platform @swift-ci Please test Linux platform

Lint Testing

Language Comment
Python @swift-ci Please Python lint

Note: Only members of the Apple organization can trigger swift-ci.

This involved several complications, mostly making the distinction between typealiases whose definitions contain associated types and those which don't:
- Only check final component of compound idents for existential-typeness, so that it is legal to refer to a nested typealias inside the existential.
- New diagnosis for trying to use typealias which is an alias for an associated type outside of the defining protocol.
- Add check on member lookup to omit typealiases to assoc types, similar to existing treatment of assoc types.
- Added test.
@gregomni
Copy link
Contributor Author

gregomni commented Jun 4, 2016

@swift-ci Please test

@gregomni
Copy link
Contributor Author

gregomni commented Jun 4, 2016

@DougGregor Would you mind taking a quick look at this? Thanks!

@slavapestov
Copy link
Contributor

@gregomni I have a set of changes I'm working on here -- take a look at slavapestov@358ff8f

@slavapestov slavapestov self-assigned this Jun 6, 2016
@gregomni
Copy link
Contributor Author

gregomni commented Jun 7, 2016

@slavapestov That rearrangement looks great! I don't think it conflicts with / directly intersects the changes here.

@slavapestov
Copy link
Contributor

Sounds good! One thing I noticed is that the current code breaks when the typealias names an associated type, but as an argument of a bound generic. Eg,

protocol P { associatedtype A; typealias T = [A] }

I'll have an updated version of my patch ready tomorrow.

// Substitute the base into the member's type.
if (Type memberType = substMemberTypeWithBase(dc->getParentModule(),
typeDecl, type,
/*isTypeReference=*/true)) {

// Similar to the associated type case, ignore typealiases containing
// associated types when looking into a non-protocol.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be clearer to factor out the entire predicate into its own method, because then in addition to comments you can rely on the method name to understand what's going on

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