Skip to content

[5.1] Opaque types prototype #22965

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 16 commits into from
Feb 28, 2019

Conversation

jckarter
Copy link
Contributor

WIP towards a prototype of an MVP subset of https://forums.swift.org/t/opaque-result-types/15645 . For an initial implementation, this won't include the resilience, second-order where constraint, or multiple conditional conformance features proposed in the thread.

  • Parsing
  • Decl representation for opaque types
  • Type representation for opaque types (probably as some new kind of ArchetypeType?)
  • Type checking to resolve the underlying type of opaque types
  • Non-resilient SILGen (lowering the opaque type directly to its underlying type)
  • Serialization support

Limitations include:

  • The opaque type is spelled __opaque Protocol or __opaque ProtocolA & ProtocolB [& ProtocolC...] as placeholder spelling.
  • Opaque types can only appear as the return type of func declarations, not properties or closures.
  • where clause constraints on the opaque type are not yet supported, nor are conditional conformances on the opaque type. However, the protocols in the __opaque type can be arbitrary protocols, with any number of associated type or Self type requirements. __opaque types can in turn be inferred as associated types of protocol conformances.
  • The runtime and resilience aspects are not yet implemented. Changing the underlying type of a public opaque type will break ABI.

Some known issues:

  • Because of the missing runtime support, I substitute out opaque types during SILGen in a rather hacky unprincipled way. It's likely that the optimizer, particularly generic specialization, may still cause crashes.
  • The type checker currently crashes if there's a type error in the underlying type, such as if it does not conform to the required protocols.
  • Diagnostics involving opaque types will print the type with a compiler-internal representation involving the original decl name and generic arguments, instead of anything human-understandable.

@jckarter jckarter force-pushed the opaque-type-ast-5.1 branch from ecccf77 to b3642ae Compare February 28, 2019 00:52
And maybe allow nested types to live on them.
Explicitly serialize the parent archetype so that it can be any kind of root archetype.
To represent the abstracted interface of an opaque type, we need a generic signature that refines
the outer context generic signature with an additional generic parameter representing the underlying
type and its exposed constraints. Opaque types also need to be keyed by their originating decl, so
that we can treat values of the same opaque type as the same. When we check a FuncDecl with an
opaque type specified as its return type, create an OpaqueTypeDecl and associate it with the
originating decl. (A representation for *types* derived from the opaque decl will come next.)
It's currently meaningless, and it'll require thought to pass the correct value when it becomes
meaningful.
…lt types.

This prevents opaque result types from propagating nontrivially into other declarations' types,
which may be confusing and create implementation complexities.
@jckarter jckarter force-pushed the opaque-type-ast-5.1 branch from b3642ae to 32f75fd Compare February 28, 2019 00:55
@jckarter
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 32f75fd

@jckarter
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 32f75fd

@jckarter
Copy link
Contributor Author

@swift-ci Please test Linux

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 32f75fd

@jckarter
Copy link
Contributor Author

@swift-ci Please clean test Linux

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 32f75fd

@jckarter
Copy link
Contributor Author

@swift-ci Please clean test Linux

@jckarter jckarter merged commit 234e525 into swiftlang:swift-5.1-branch Feb 28, 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