Skip to content

Typed throws #68629

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 8 commits into from
Sep 30, 2023
Merged

Typed throws #68629

merged 8 commits into from
Sep 30, 2023

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Sep 19, 2023

Start implementing typed throw specifiers with the syntax throws(X). There are a large number of tasks involved in such a feature; this pull request covers the parsing and type checking aspects.

  • Parsing throws(X) as an effect specifier everywhere it is needed (functions, initializers, accessors, closures, function types, expressions that can become function types)
  • AST representation for having a specified thrown type on functions, initializers, accessors, closures, and function types
  • Function type substitution and canonicalization
  • AST serialization and printing support
  • Type checking of throws statements against the specified thrown type of functions/initializers/accessors
  • Type checking of closures with specified thrown type
  • Effects checking
  • Access control checking for thrown type
  • SIL type lowering and SIL generation
  • Subtyping relationships involving typed throws
  • Override relationships involving typed throws
  • Witness checking involving typed throws
  • Associated type inference from typed throws
  • IR generation
  • SIL generation for generic thrown errors
  • SIL generation for thunks that convert generic thrown error types
  • Name mangling for typed throws
  • Runtime type metadata and dynamic casting support for functions with typed throws
  • async let with typed throws
  • Opaque thrown error types
  • Closure thrown error type inference

@DougGregor DougGregor force-pushed the typed-throws branch 2 times, most recently from a7187cf to b352be8 Compare September 22, 2023 19:13
@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2219

@swift-ci please smoke test

Parse typed throw specifiers as `throws(X)` in every place where there
are effects specified, and record the resulting thrown error type in
the AST except the type system. This includes:
* `FunctionTypeRepr`, for the parsed representation of types
* `AbstractFunctionDecl`, for various function-like declarations
* `ClosureExpr`, for closures
* `ArrowExpr`, for parsing of types within expression context

This also introduces some serialization logic for the thrown error
type of function-like declarations, along with an API to extract the
thrown interface type from one of those declarations, although right
now it will either be `Error` or empty.
Add the thrown type into the AST representation of function types,
mapping from function type representations and declarations into the
appropriate thrown type. Add tests for serialization, printing, and
basic equivalence of function types that have thrown errors.
… type

Rather than always type-check the expression in a `throw` statement for
conformance to `Error`, check that it converts to the thrown error type.
Enable typed throws on explicit closures, either due to contextual
type information or due to explicit specification.
Lower the thrown error type into the SIL function type. This requires
very little code because the thrown error type was already modeled as
a SILResultInfo, which carries type information. Note that this
lowering does not yet account for error types that need to passed
indirectly, but we will need to do so for (e.g.) using resilient error
types.

Teach a few places in SIL generation not to assume that thrown types
are always the existential error type, which primarily comes down to
ensuring that rethrow epilogues have the thrown type of the
corresponding function or closure.

Teach throw emission to implicitly box concrete thrown errors in the
error existential when needed to satisfy the throw destination. This
is a temporary solution that helps translate typed throws into untyped
throws, but it should be replaced by a better modeling within the AST
of the points at which thrown errors are converted.
@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2219

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2219

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please build toolchain

@DougGregor
Copy link
Member Author

Note: I'm looking to get this building cleanly and then merging it, since everything is behind an experimental flag and the next part is a major refactoring of the effects checker.

@DougGregor
Copy link
Member Author

DougGregor commented Sep 30, 2023

(Note: toolchain builds aren't useful at this point, and were kicked off with the wrong settings because I meant to do that in a different PR. Those failures are meaningless)

@DougGregor DougGregor merged commit 581b9d8 into swiftlang:main Sep 30, 2023
@DougGregor DougGregor deleted the typed-throws branch September 30, 2023 18:18
@DougGregor
Copy link
Member Author

Effects checking mostly implemented in #68898 and #68976

@DougGregor
Copy link
Member Author

Witness checking and associated type inference was implemented in #69087

@DougGregor
Copy link
Member Author

Subtyping implemented via #68995

@DougGregor
Copy link
Member Author

Basic IR generation is in #68833

@DougGregor
Copy link
Member Author

Improving effects checking in #69198

@DougGregor
Copy link
Member Author

Infer Error conformance when needed in #69181

@DougGregor
Copy link
Member Author

Introduce upcoming feature FullTypedThrows for inferring thrown type from a throw statement in #69225

@DougGregor
Copy link
Member Author

Name mangling is coming in via #69471

@DougGregor
Copy link
Member Author

SIL generation for generic thrown errors is coming in via #69430

@DougGregor
Copy link
Member Author

Name mangling and runtime type metadata are in #69493

@DougGregor
Copy link
Member Author

SIL generation for generic typed throws is working now, with #69767 being the last of the series of PRs.

@DougGregor
Copy link
Member Author

Fun in the standard library: #69771

@DougGregor
Copy link
Member Author

Yet more SILGen thunking fun: #69791

@DougGregor
Copy link
Member Author

Overrides: #69839

@DougGregor
Copy link
Member Author

Silly me, overrides are going to land as #69922, but the code is done

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