Skip to content

[WIP] Sema: Resolve type witnesses for conditional conformances early #30700

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

AnthonyLatsis
Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis commented Mar 28, 2020

Resolves SR-7516.

Allows for correctly diagnosing references to default type witnesses with
unsatisfied requirements once we start realizing types in members.

This change also establishes a definite type witness resolution order when associated type collisions occur in the presence of multiple conformances with differing conditional requirements. Previously, depending on validation order, a default type witness for a conditional conformance could satisfy a type requirement for a regular conformance, leading to miscompiles like the following:

 protocol A { associatedtype X }
 protocol B { associatedtype X = Bool }

 struct Foo<T>: A {} // no objections, A.X := Bool
 extension Foo: B where T == Int {}

Given the conditional requirements of a conformance are satisfied by those
of another conformance, default type witnesses for the former, more general
conformance, can now satisfy type requirements for the latter – but not vice versa – in
case of associated type collisions.
For example, both conformances in the snippets below will associate X with a single type witness
synthesized for the more general conformance to B.

 struct Foo<T>: B {
   internal typealias X = Bool // synthesized!
 }
 extension Foo: A where T == Int {}
 struct Foo<T> {}
 extension Foo: B where T: Equatable {
   internal typealias X = Bool // synthesized!
 }
 extension Foo: A where T == Int {}

@AnthonyLatsis AnthonyLatsis changed the title Sema: Resolve type witnesses for conditional conformances early [WIP] Sema: Resolve type witnesses for conditional conformances early Mar 28, 2020
This allows for correctly diagnosing references to default type witnesses
with unsatisfied requirements once we start realizing types in members.

This change also lays the groundwork for a special case of default type
witness resolution behavior when associated type collisions occur in the
presence of multiple regular and conditional conformances. Previously,
depending on evaluation order, a default type witness for a conditional
conformance could satisfy a type requirement for a regular conformance,
and the following snippet would have compiled:

 protocol A { associatedtype X }
 protocol B { associatedtype X = Bool }

 struct Foo<T>: A {} // error: type 'Foo<T>' does not conform to protocol 'A'
 extension Foo: B where T == Int {}

If the conditional requirements of a conformance are satisfied by those
of another conformance, default type witnesses for the former, more
general conformance, can now satisfy type requirements for the latter
conformance – but not vice versa – in case of associated type collisions.
For example, both conformances below will associate 'X' with a single type
witness synthesized for the less constrained conformance to B.

 struct Foo<T>: B {
   internal typealias X = Bool // synthesized!
 }
 extension Foo: A where T == Int {}
@shahmishal
Copy link
Member

Please update the base branch to main by Oct 5th otherwise the pull request will be closed automatically.

  • How to change the base branch: (Link)
  • More detail about the branch update: (Link)

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.

3 participants