Skip to content

Cleanup inheritance clause parsing #11781

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

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Sep 5, 2017

In order to fully implement SE-0156, we need to implement two additional language changes:

  • In Swift 5 mode, warn or error on ': class' in a protocol inheritance clause, offering a fixit to replace 'class' with 'AnyObject' instead.
  • Allow subclass existentials and class types in protocol inheritance clauses, to define a protocol whose conforming type must be a subclass of a given class.

Also, we have some code duplication and recursive validation bugs in Sema's inheritance clause checking logic. Cleaning this up will make the code clearer and fix various crashes.

This PR doesn't fully address any of the above, but it cleans up the code in preparation for making those changes. I don't intend on working on this any time soon, but I wanted to finish this cleanup as a quick follow-up to #11762.

We had two slightly different codepaths to diagnose ': class'
in an inheritance clause where it is not supported.

For generic parameters, we would fix the 'class' to 'AnyObject',
but for associated types we didn't do this. Perform the fix in
all cases where it makes sense and remove one of the two
diagnostics.
We allowed them for generic parameter inheritance clauses but
not anywhere else. While arguably this has stylistic benefits,
the restriction was not enforced consistently and was mostly a
result of implementation limitations.

Lift the restriction and fix things up where needed to make them
work. This brings us closer to allowing protocols to constrain
the 'Self' type to a subclass of a class by listing the class in
the protocol's inheritance clause, which was a feature from SE-0156,
but this doesn't quite work.

Fixes <https://bugs.swift.org/browse/SR-4678> and
<rdar://problem/31785092>.
We can just use parseType() everywhere instead. We already check
for non-identifier types in inheritance clauses elsewhere, and indeed
we have to anyway because an identifier type might resolve to a
type alias whose underlying type is a non-nominal type.

It doesn't look like this change made any diagnostics worse, but if
we find a case where it did, we could revert it.
@slavapestov slavapestov force-pushed the cleanup-inheritance-clause-parsing branch from 0151e3b to 2c7246e Compare September 6, 2017 04:54
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

1 similar comment
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

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.

1 participant