Skip to content

[SE-0279] Add support for an unbraced syntax for multiple trailing closures #31052

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 37 commits into from
May 6, 2020

Conversation

rjmccall
Copy link
Contributor

@rjmccall rjmccall commented Apr 15, 2020

This PR implements the syntax proposed in SE-0279, allowing an initial unlabeled trailing closure to be followed by an arbitrary number of labeled-closure postfixes.

swift-syntax: swiftlang/swift-syntax#212

@rjmccall
Copy link
Contributor Author

@swift-ci Please smoke test.

@rjmccall
Copy link
Contributor Author

@swift-ci Please test.

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 8e2ffa2

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 8e2ffa2

@rintaro
Copy link
Member

rintaro commented Apr 16, 2020

swiftlang/swift-syntax#212
@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 395ac06

@rintaro
Copy link
Member

rintaro commented Apr 16, 2020

swiftlang/swift-syntax#212
@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 395ac06

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 395ac06

@rintaro
Copy link
Member

rintaro commented Apr 17, 2020

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 9d90e14

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 9d90e14

@rintaro
Copy link
Member

rintaro commented Apr 17, 2020

swiftlang/swift-syntax#212
@swift-ci Please test

@theblixguy theblixguy added the swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review label Apr 17, 2020
@benlangmuir
Copy link
Contributor

swiftlang/swift-syntax#212
@swift-ci please test

1 similar comment
@benlangmuir
Copy link
Contributor

swiftlang/swift-syntax#212
@swift-ci please test

Comment on lines +1014 to +1015
if (!returnsVoid)
OS << " -> " << AFT->getResult()->getString(PO);
Copy link
Member

@rintaro rintaro May 1, 2020

Choose a reason for hiding this comment

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

@benlangmuir If the return type is unresolved type, could you stop emitting -> _?
In such cases, the return type probably inferred by the body, so we should not emit the return type signature.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have a suggestion for a test case? Everything I've tried either gives a name or it fails entirely to provide argument completions.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe a generic param type or a archetype type. Sorry I don't have this branch in my hand, but does this suggests y: { () -> A in <#code#> }?

func foo<A>(x: () -> Void, y: () -> A) {}
foo() {} #^COMPLETE^#

If so, I think we should not suggest the signature in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yep it gives y: { () -> A in <#code#> }. This is consistent with how completing the whole "foo" call behaves though (if you complete foo and then expand the placeholder the result is identical), so if we're going to change it I think it should be a separate PR.

In general, we might want to drop the return type in most cases even if it is resolved - it seems to be more common style to omit the types unless they are required. We could also drop the parens around the parameters in that case.

Copy link
Member

Choose a reason for hiding this comment

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

I think it should be a separate PR

Agreed!

@swift-ci
Copy link
Contributor

swift-ci commented May 1, 2020

Build failed
Swift Test Linux Platform
Git Sha - f1fff73

@swift-ci
Copy link
Contributor

swift-ci commented May 1, 2020

Build failed
Swift Test OS X Platform
Git Sha - f1fff73

@benlangmuir
Copy link
Contributor

It appears that CI ran the tests against this branch directly without first merging it into master. We probably need to rebase.

GitHub pull request #31052 of commit f1fff73, has merge conflicts.
swift : f1fff73

@benlangmuir benlangmuir force-pushed the unbraced-multiple-trailing-closures branch from f1fff73 to f884f33 Compare May 2, 2020 02:16
@benlangmuir
Copy link
Contributor

swiftlang/swift-syntax#212
@swift-ci please test

1 similar comment
@benlangmuir
Copy link
Contributor

swiftlang/swift-syntax#212
@swift-ci please test

@benlangmuir
Copy link
Contributor

swiftlang/swift-syntax#212
@swift-ci please test macOS

@swift-ci
Copy link
Contributor

swift-ci commented May 2, 2020

Build failed
Swift Test OS X Platform
Git Sha - f1fff73

@swift-ci
Copy link
Contributor

swift-ci commented May 2, 2020

Build failed
Swift Test Linux Platform
Git Sha - f884f33

@benlangmuir
Copy link
Contributor

swiftlang/swift-syntax#212
@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented May 2, 2020

Build failed
Swift Test OS X Platform
Git Sha - f884f33

rjmccall and others added 15 commits May 6, 2020 01:56
At an earlier point, we were doing this check after parsing
the labeled closures, but that never made much sense, since
it's a good diagnostic regardless.
TODO: Postfix completion after trailing closures
So that SourceKit can expand them to closure literals.
Instead of getting all edits up front using the same source code, apply
each replacement before calculating the next. Placeholder expansion is
sensitive the surrounding code, so expanding multiple closures
separately can give different results from doing so in order. To allow
testing that, add a magic placeholder identifier __skip__ to skip
expansion during testing.

This is also required for handling multiple trailing closures.
Since placeholder expansion works with a single placeholder, which is
somewhat at odds with multiple-trailing closures, we eagerly attempt to
expand all consecutive placeholders of closure type. That is, if the API
has multiple closure parameters at the end, expanding any one of them
will transform all of them to the new syntax.

Example

```
foo(a: <#T##()->()#>, b: <#T##()->()#>)
```

expanding *either* parameter will produce the following:

```
foo {
  <#code#>
} b: {
  <#code#>
}
```

(caveat: the indentation is not part of placeholder expansion, but it's
added here for clarity)

At least for now we do not attempt to corral an existing closure into
the new syntax, so for

```
foo(a: { bar() }, b: <#T##()->()#>)
```

The exansion will be

```
foo(a: { bar() }) {
  <#code#>
}
```

as it was before.

rdar://59688632
When completing a single argument for a trailing closure, pre-expand the
closure expression syntax instead of using a placeholder. It's not valid
to pass a non-closure anyway.

rdar://62189182
@rjmccall rjmccall force-pushed the unbraced-multiple-trailing-closures branch from f884f33 to 37b98af Compare May 6, 2020 16:23
@rjmccall
Copy link
Contributor Author

rjmccall commented May 6, 2020

swiftlang/swift-syntax#212
@swift-ci please test

@rjmccall rjmccall marked this pull request as ready for review May 6, 2020 16:25
@swift-ci
Copy link
Contributor

swift-ci commented May 6, 2020

Build failed
Swift Test Linux Platform
Git Sha - f884f33

@swift-ci
Copy link
Contributor

swift-ci commented May 6, 2020

Build failed
Swift Test OS X Platform
Git Sha - f884f33

@rintaro
Copy link
Member

rintaro commented May 6, 2020

@rjmccall I changed swift-syntax PR to swiftlang/swift-syntax#216 because the CI somehow didn't picked up the updated hash of it.

@rintaro
Copy link
Member

rintaro commented May 6, 2020

swiftlang/swift-syntax#216
@swift-ci Please test

@rjmccall rjmccall merged commit 6a0bd67 into master May 6, 2020
@rjmccall rjmccall deleted the unbraced-multiple-trailing-closures branch May 6, 2020 19:54
@xwu
Copy link
Collaborator

xwu commented May 9, 2020

@rjmccall I have updated the PR description to reflect that what's implemented is in fact the proposed syntax and not an alternative to it, for future readers who may be confused.

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