-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[WIP] Forward matching of trailing closure arguments. #32644
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
[WIP] Forward matching of trailing closure arguments. #32644
Conversation
@swift-ci please test source compatibility |
If we're matching the trailing closure at the end but haven't seen any mismatches yet, consider the trailing closure to be a mismatch. This function probably needs to be rewritten in terms of matchCallArguments(), because it is performing an incorrect approximation of label matching that doesn't work for multiple trailing closures.
Introduce an experimental attribute and command-line flag to replace the "backward" matching algorithm for unlabeled trailing closure arguments with a simpler "forward" algorithm. In this new algorithm, unlabeled trailing closure arguments match the next parameter in the parameter list that can accept an unlabeled trailing closure. The "can accept an unlabeled trailing closure" criteria looks at the parameter itself. The parameter accepts an unlabeled trailing closure if all of the following are true: * The parameter is not variadic * The parameter is not 'inout' * The adjusted type of the parameter (defined below) is a function type The adjusted type of the parameter is the parameter's type as declared, after performing two adjustments: * If the parameter is an @autoclosure, use the result type of the parameter's declared (function) type, before performing the second adjustment. * Remove all outer "optional" types. For example, the following function illustrates both adjustments to determine that the parameter "body" accepts an unlabeled trailing closure: func doSomething(body: @autoclosure () -> (((Int) -> String)?)) This source-breaking change is accessible in one of two ways. The new attribute @_trailingClosureMatching(.forward) can be applied to a function, initializer, or subscript to enable the new forward matching algorithm for direct calls to that function. This allows specific APIs to opt in to forward matching while leaving all other calls unaffected. The command-line parameter `-enable-experimental-trailing-closure-matching` enables forward matching as the default algorithm. It is most useful for experimenting with the source-compatibility effects of forward matching. Specific APIs can also explicitly request to use the current, backward matching algorithm to maintain their existing behavior even under the new command-line parameter, by using the other form of the new trailing closure matching attribute: @_trailingClosureMatching(.backward)
b689423
to
32ae69f
Compare
@swift-ci test source compatibility |
1 similar comment
@swift-ci test source compatibility |
We're missing a lot of data here because of the current SwiftPM issue. However, there are only 3 failures that are clearly attributable to this new rule:
|
@swift-ci build toolchain |
@swift-ci please build toolchain |
32ae69f
to
f0b8eb1
Compare
@swift-ci please build toolchain |
@swift-ci please test |
@swift-ci please test source compatibility |
@swift-ci please build toolchain |
@swift-ci please test source compatibility |
@swift-ci please test |
Build failed |
@swift-ci please test platform macOS |
1 similar comment
@swift-ci please test platform macOS |
Linux Toolchain (Ubuntu 16.04) Install command |
macOS Toolchain Install command |
@swift-ci please smoke test platform macOS |
Closing out this dead-end approach. See #32891 for the proposed way forward |
Introduce an experimental attribute and command-line flag to replace
the "backward" matching algorithm for unlabeled trailing closure
arguments with a simpler "forward" algorithm. In this new algorithm,
unlabeled trailing closure arguments match the next parameter in the
parameter list that can accept an unlabeled trailing closure.
The "can accept an unlabeled trailing closure" criteria looks at the
parameter itself. The parameter accepts an unlabeled trailing closure
if all of the following are true:
inout
The adjusted type of the parameter is the parameter's type as
declared, after performing two adjustments:
@autoclosure
, use the result type of theparameter's declared (function) type, before performing the second
adjustment.
For example, the following function illustrates both adjustments to
determine that the parameter "body" accepts an unlabeled trailing
closure:
This source-breaking change is accessible in one of two ways. The new attribute
can be applied to a function, initializer, or subscript to enable the
new forward matching algorithm for direct calls to that function. This
allows specific APIs to opt in to forward matching while leaving all
other calls unaffected.
The command-line parameter
-enable-experimental-trailing-closure-matching
enables forward matching as the default algorithm. It is most useful
for experimenting with the source-compatibility effects of forward
matching.
Specific APIs can also explicitly request to use the current, backward
matching algorithm to maintain their existing behavior even under the
new command-line parameter, by using the other form of the new
trailing closure matching attribute: