Skip to content

Add an educational note explaining SE-0286 changes #33119

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
Aug 2, 2020

Conversation

owenv
Copy link
Contributor

@owenv owenv commented Jul 25, 2020

Some of this is copied verbatim from the proposal, but IMO it's important to have some documentation of this change beyond the SE proposal to communicate the expected impact.

If and when SE-0286 is accepted, I'll associate this note with whatever the final set of diagnostics is and hopefully cherrypick to the 5.3 branch.

@owenv owenv requested review from xwu, xedin and DougGregor July 25, 2020 17:55

In Swift, calling a function with one or more trailing closure arguments requires the label of the first trailing closure argument to be omitted. As a result, the compiler must consider additional context when determining which function parameter the trailing closure should satisfy.

Before Swift 5.3, the compiler used a backward scanning rule to match a trailing closure to a function parameter. Starting from the end of the parameter list, it moved backwards until finding a parameter compatible with trailing closures. This could sometimes lead to unexpected behavior. Consider the following example:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This explanation is a little vague, but I think that's OK since it describes the old behavior

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe replace compatible with trailing closures with which could accept a trailing closure argument i.e. function type, generic parameter, 'Any' type?

@owenv
Copy link
Contributor Author

owenv commented Jul 25, 2020

Thanks @benrimmington!

@owenv owenv force-pushed the closure-matching-note branch from 5d79f83 to cee71ae Compare July 27, 2020 01:26
@benrimmington

This comment has been minimized.


In Swift, calling a function with one or more trailing closure arguments requires the label of the first trailing closure argument to be omitted. As a result, the compiler must consider additional context when determining which function parameter the trailing closure should satisfy.

Before Swift 5.3, the compiler used a backward scanning rule to match a trailing closure to a function parameter. Starting from the end of the parameter list, it moved backwards until finding a parameter compatible with trailing closures. This could sometimes lead to unexpected behavior. Consider the following example:
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe replace compatible with trailing closures with which could accept a trailing closure argument i.e. function type, generic parameter, 'Any' type?

Apply suggestions from code review

Co-authored-by: Ben Rimmington <[email protected]>

Reword explanation of pre-5.3 behavior

Associate SE-0286 note with warning diagnostic
@owenv owenv force-pushed the closure-matching-note branch from a017a58 to bacc96b Compare July 30, 2020 04:14
@owenv
Copy link
Contributor Author

owenv commented Jul 30, 2020

@swift-ci smoke test

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.

4 participants