Skip to content

[SE-0276] Support multiple patterns in catch clauses #27776

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 1 commit into from
Apr 7, 2020

Conversation

owenv
Copy link
Contributor

@owenv owenv commented Oct 18, 2019

e.g. do { ... } catch MyErr.a(let code), MyErr.b(let code) { ... }

The approach I took here was to get rid of CatchStmt entirely and reuse CaseStmt in DoCatchStmt. If anyone has time to take a look, I'd appreciate feedback on whether this seems like a good approach before I pitch this on the forums. Initially I tried keeping CatchStmt around and just modifying it to hold CaseLabelItems, but it resulted in a lot of code duplication in Sema and SILGen. This approach is cleaner overall, but there are still some rough edges (in the profiling code, for instance).

Requires the following PRs for testing:

swiftlang/llvm-project#538
swiftlang/swift-syntax#190

@owenv
Copy link
Contributor Author

owenv commented Oct 21, 2019

Updated to fix code completion tests, this now passes the test suite

@rintaro
Copy link
Member

rintaro commented Oct 24, 2019

I'm a little uncomfortable with making CaseItemSyntax have is_optional Pattern.
@nkcsgexi What do you think?

Also, instead of using CaseStmt for catch, how about making a base class (e.g. AbstractMatchStmt) from which CaseStmt and CatchStmt inherit?

@jrose-apple
Copy link
Contributor

jrose-apple commented Oct 24, 2019

I'm not sure what benefit we'd get from having CatchStmt and CaseStmt continue to be different classes. They do exactly the same thing, just with different expectations about the incoming type.

@owenv
Copy link
Contributor Author

owenv commented Oct 24, 2019

@jrose-apple There are a few cases where we need to check the CaseParentKind to handle cases and catches differently which might cause some concern. Specifically, the Profiler and Formatting both need to special-case their CaseStmt handling in a few different places. I think some of the complexity can be cleaned up though, so the tradeoffs are probably worth it unless I discover other problems.

@rintaro I agree that the CaseItemSyntax change is not great. I'll look into ways of cleaning that up a bit when I have some more time to work on this. It might be best to not try to reuse the case constructs at the Syntax level.

@owenv owenv force-pushed the catch_revamp_take_4 branch from 7d0940a to 6a6bc71 Compare October 27, 2019 03:03
@owenv owenv marked this pull request as ready for review October 27, 2019 19:49
@owenv owenv force-pushed the catch_revamp_take_4 branch 2 times, most recently from ad9a6d3 to 29a40d5 Compare November 6, 2019 21:43
@owenv
Copy link
Contributor Author

owenv commented Nov 6, 2019

@swift-ci please test

@owenv owenv added the swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review label Nov 6, 2019
@owenv owenv requested review from gottesmm and rintaro November 6, 2019 22:02
@owenv owenv force-pushed the catch_revamp_take_4 branch 2 times, most recently from 4267fb3 to da8e73c Compare November 12, 2019 21:56
@owenv owenv force-pushed the catch_revamp_take_4 branch 2 times, most recently from 18449b8 to b1aa6b8 Compare January 7, 2020 23:20
@DougGregor
Copy link
Member

@swift-ci please smoke test

@DougGregor
Copy link
Member

@swift-ci please test source compatibility

@DougGregor
Copy link
Member

@swift-ci build toolchain

@owenv
Copy link
Contributor Author

owenv commented Jan 15, 2020

@DougGregor I guess I forgot to link them on this PR, swiftlang/llvm-project#538 and swiftlang/swift-syntax#190 are also required to build this. Sorry about that!

@theblixguy
Copy link
Collaborator

theblixguy commented Jan 15, 2020

swiftlang/llvm-project#538
swiftlang/swift-syntax#190

@swift-ci please build toolchain

@theblixguy
Copy link
Collaborator

@theblixguy
Copy link
Collaborator

swiftlang/llvm-project#538
swiftlang/swift-syntax#190

@swift-ci please test source compatibility

@owenv
Copy link
Contributor Author

owenv commented Jan 15, 2020

Thanks @theblixguy !

@swift-ci
Copy link
Contributor

Linux Toolchain (Ubuntu 16.04)
Download Toolchain
Git Sha - b1aa6b8

Install command
tar zxf swift-PR-27776-341-ubuntu16.04.tar.gz
More info

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - b1aa6b8

@owenv
Copy link
Contributor Author

owenv commented Mar 23, 2020

For reference, the update above picks up changes from recent updates to function builders and the indentation implementation

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - f1f15ea5d57af74be4ec145d3d4ae8e99e8bd370

@owenv
Copy link
Contributor Author

owenv commented Mar 23, 2020

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - f1f15ea5d57af74be4ec145d3d4ae8e99e8bd370

@owenv
Copy link
Contributor Author

owenv commented Mar 24, 2020

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - f1f15ea5d57af74be4ec145d3d4ae8e99e8bd370

@owenv
Copy link
Contributor Author

owenv commented Mar 24, 2020

Looks like something in swift-syntax has fallen out of date. I’ll take a closer look tommorrow

@owenv
Copy link
Contributor Author

owenv commented Mar 26, 2020

testing w/ updated swift-syntax PR

swiftlang/llvm-project#538
swiftlang/swift-syntax#190

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - f1f15ea5d57af74be4ec145d3d4ae8e99e8bd370

@owenv
Copy link
Contributor Author

owenv commented Mar 26, 2020

@owenv
Copy link
Contributor Author

owenv commented Mar 26, 2020

@rjmccall do you mind helping review the type checker and SILGen changes here as the original author of do-catch?

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - f1f15ea5d57af74be4ec145d3d4ae8e99e8bd370

@owenv
Copy link
Contributor Author

owenv commented Mar 26, 2020

My lldb patch seems to be out of date again, looking now

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - f1f15ea5d57af74be4ec145d3d4ae8e99e8bd370

@owenv
Copy link
Contributor Author

owenv commented Mar 27, 2020

On further investigation, the failures above may have just been infra issues. trying again:

swiftlang/llvm-project#538
swiftlang/swift-syntax#190

@swift-ci please test

Like switch cases, a catch clause may now include a comma-
separated list of patterns. The body will be executed if any
one of those patterns is matched.

This patch replaces `CatchStmt` with `CaseStmt` as the children
of `DoCatchStmt` in the AST. This necessitates a number of changes
throughout the compiler, including:
- Parser & libsyntax support for the new syntax and AST structure
- Typechecking of multi-pattern catches, including those which
  contain bindings.
- SILGen support
- Code completion updates
- Profiler updates
- Name lookup changes
@owenv owenv force-pushed the catch_revamp_take_4 branch from f1f15ea to 43e2d10 Compare April 4, 2020 16:29
@owenv
Copy link
Contributor Author

owenv commented Apr 4, 2020

fixing profiler merge conflicts

swiftlang/llvm-project#538
swiftlang/swift-syntax#190

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 4, 2020

Build failed
Swift Test Linux Platform
Git Sha - f1f15ea5d57af74be4ec145d3d4ae8e99e8bd370

@swift-ci
Copy link
Contributor

swift-ci commented Apr 4, 2020

Build failed
Swift Test OS X Platform
Git Sha - f1f15ea5d57af74be4ec145d3d4ae8e99e8bd370

@owenv
Copy link
Contributor Author

owenv commented Apr 4, 2020

macOS failure looks like flakiness:

09:52:38 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.4.sdk/usr/include/objc/objc-api.h:29:10: fatal error: cannot open file '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.4.sdk/usr/include/AvailabilityMacros.h': Operation not permitted
09:52:38 #include <AvailabilityMacros.h>
09:52:38          ^
09:52:38 1 error generated.

Windows failure is expected since it doesn't support cross-repo PR testing

swiftlang/llvm-project#538
swiftlang/swift-syntax#190

@swift-ci please test macOS platform

Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good! I have one comment where it looks like we have a lot of code duplication in the type checker between switch and do-catch, but we can clean that up in a follow-up PR.


auto clauses = S->getCatches();
CaseStmt *previousBlock = nullptr;
for (auto i = clauses.begin(), e = clauses.end(); i != e; ++i) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code looks nearly identical to what's in visitSwitchStmt. Can they be unified?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they can be if I refactor fallthrough handling a little. I'll look into that asap

@DougGregor
Copy link
Member

@swift-ci please test source compatibility

@DougGregor
Copy link
Member

swiftlang/llvm-project#538
swiftlang/swift-syntax#190

@swift-ci please test source compatibility

// Since we know that we only have one case label item, grab its pattern
// vars and use that to update expected with the right SILValue.
//
// TODO: Do we need a copy here?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gottesmm Any thoughts on this TODO?

@DougGregor DougGregor merged commit 78880ff into swiftlang:master Apr 7, 2020
@DougGregor
Copy link
Member

@owenv Also, can you update the ChangeLog in a follow-up PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants