Skip to content

Sema: Allow protocols with 'Self' constraints again #19844

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 6 commits into from
Oct 12, 2018

Conversation

slavapestov
Copy link
Contributor

This sort of worked in 4.2, then I banned it but it broke source comaptibility.

Now that protocol superclass constraints are fully supported, let's allow this again, with some minor refactoring to the request evaluator.

So both of these are equivalent and should work correctly:

protocol P : SomeClass {}
protocol P where Self : SomeClass {}

Fixes rdar://problem/43028442.

@slavapestov
Copy link
Contributor Author

@swift-ci Please test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 495873fb83ea21f3344794f466cb08d66264d388

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 495873fb83ea21f3344794f466cb08d66264d388

An extension might have an extended nominal, even if the extended type
is an error type. This can happen if, for example, the type lookup was
ambiguous, because the declaration lookup more eagerly disambiguates.

This is fine, because even if we bind the extension to a nominal it's
okay to diagnose an error later. However, we do have to deal with this
case here by checking for an error type.

A better solution would be to ensure the extended type is always set
to something, even if we diagnosed an error while resolving it, because
we can always use the declared type of the extended nominal instead.

However when I tried that I ran into more problems, and I don't feel
like shaving this yak right now.
Previously you could pass in a vector of TypeDecls and it handled
module and AnyObject lookup for you. The AnyObject case was never
used and the module was was only needed in one place, so clean
things up to make them more direct here.
These two declarations are now equivalent:

  protocol P : SomeClass { ... }
  protocol P where Self : SomeClass { ... }

There's a long, complicated story here:

- Swift 4.2 rejected classes in the inheritance clause of a
  protocol, but it accepted the 'where' clause form, even
  though it didn't always work and would sometimes crash

- Recently we got the inheritance clause form working, and
  added a diagnostic to ban the 'where' clause form, because
  we thought it would simplify name lookup to not have to
  consider the 'where' clause

- However, we already had to support looking at the 'where'
  clause from name lookup anyway, because you could write

  extension P where Self : SomeClass { ... }

- It turns out that despite the crashes, protocols with
  'Self' constraints were already common enough that it was
  worth supporting the existing behavior, instead of banning
  it

Fixes <rdar://problem/43028442>.
@slavapestov slavapestov force-pushed the protocol-where-self-is-crap branch from 495873f to 64f12ff Compare October 12, 2018 10:08
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov slavapestov merged commit 5cebbb2 into swiftlang:master Oct 12, 2018
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