Skip to content

[Typed throws] Make try? and try! into catch nodes #70283

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 9 commits into from
Dec 7, 2023

Conversation

DougGregor
Copy link
Member

Both try? and try! are catch nodes, because they catch an error
thrown in their subexpression and handle it. Introduce an ASTScope for
all try/try?/try1 expressions so we can find them, and model them as
catch nodes.

While here, remove a premature SILGen optimization that ends up
emitting bad SIL.

Fixes rdar://119216455 & rdar://119214492.

The ASTScope class hierarchy rooted at ASTScopeImpl is relying entirely
on overridden virtual functions and, in some cases, string comparisons
on the class name, to provide customization for the various classes.
Introduce proper LLVM dynamic casting support to this class hierarchy,
eliminating the string comparisons and the manually-specified
implementations of `getClassName` (which is now used only for printing).

We've been meaning to do this for years, but today I got angry enough.
Replace a number of virtual query functions like `getDeclIfAny`, which had
to be overridden at various points in the hierarchy, with simpler
implementations based on the scope kind and `dyn_cast`.
Macro-metaprogram whether each scope node is associated with a
particular declaration/statement/expression/attribute so these
implementations can be efficiently macro-metaprogrammed.
The subclasses of `GenericTypeOrExtensionScope` are customized by a `Portion`
mix-in class that effectively makes each into several different scope
nodes: one for the "whole decl", one for the where clause, one for the
body. Re-introduce the customized `getClassName()` operation so we get
the nicer debugging dumps.
The implementation of "lookup the catch node" is scattered throughout
various `ASTScopeImpl` subclasses. Remove that, and instead centralize
the code to find the innermost catch scope.
... it's just a `dyn_cast` now.
We only used this in one place, and then immediately filtered down
to a `CustomAttr`. Just use `dyn_cast` in the obvious way and remove
this function.
Both `try?` and `try!` are catch nodes, because they catch an error
thrown in their subexpression and handle it. Introduce an ASTScope for
all `try/try?/try1` expressions so we can find them, and model them as
catch nodes.

Fixes rdar://119216455.
An attempted SILGen optimization caused assertions and SIL verification
errors. Always convert the error appropriately.

Fixes rdar://119214492.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test macOS

@DougGregor DougGregor merged commit 12cb8be into swiftlang:main Dec 7, 2023
@DougGregor DougGregor deleted the typed-try-force-and-opt branch December 7, 2023 05:26
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