Skip to content

Sema: Three fixes for the new @escaping attribute (3.0) #4388

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

Conversation

slavapestov
Copy link
Contributor

  • Explanation: This patch fixes some issues that were reported with the change to make non-escaping closures the default and add an @escaping attribute. In particular, there was no way to have a non-escaping parameter of function type if the function type was an alias, or parenthesized. Furthermore, @escaping could not be applied to these types, and @escaping in invalid locations was not diagnosed and had no effect.
  • Scope: Affects anyone writing code that operates on closures. This is a source-breaking change, and the current behavior is counter-intuititive, so this needs to be fixed.
  • Risk: Medium.
  • Testing: New tests added, existing tests updated for the new, more correct semantics of @escaping.
  • Bugs: https://bugs.swift.org/browse/SR-2053, https://bugs.swift.org/browse/SR-2397

- If a parameter type is a sugared function type, mark the type
  as non-escaping by default. Previously, we were only doing this
  if the parameter type was written as a function type, with no
  additional sugar.

  This means in the following cases, the function parameter type
  is now non-escaping:

  func foo(f: ((Int) -> Void))

  typealias Fn = (Int) -> Void
  func foo(f: Fn)

- Also, allow @escaping to be used in the above cases:

  func foo(f: @escaping ((Int) -> Void))

  typealias Fn = (Int) -> Void
  func foo(f: @escaping Fn)

- Diagnose usages of @escaping in inappropriate locations, instead
  of just ignoring them.

It is unfortunate that sometimes we end up desugaring the typealias,
but currently there are other cases where this occurs too, such as
qualified lookup of protocol typealiases with a concrete base
type, and generic type aliases. A more general representation for
sugared types (such as an AttributedType sugared type) would allow
us to solve this in a more satisfactory manner in the future.

However at the very least this patch factors out the common code
paths and adds comments, so it shouldn't be too bad going forward.

Note that this is a source-breaking change, both because @escaping
might need to be added to parameters with a sugared function type,
and @escaping might be removed if it appears somewhere where we
do not mark function types as non-escaping by default.

Fixes <https://bugs.swift.org/browse/SR-2053> and
<https://bugs.swift.org/browse/SR-2397>.
@slavapestov slavapestov added this to the Swift 3.0 milestone Aug 18, 2016
@slavapestov
Copy link
Contributor Author

@swift-ci Please test os x

@slavapestov
Copy link
Contributor Author

@milseman or @jrose-apple, can you review this for 3.0?

@slavapestov slavapestov changed the title Sema: Three fixes for the new @escaping attribute Sema: Three fixes for the new @escaping attribute (3.0) Aug 18, 2016
@swift-ci
Copy link
Contributor

Build failed
Jenkins build - Swift Test OS X Platform
Git Commit - c1214fe
Test requested by - @slavapestov

@slavapestov
Copy link
Contributor Author

@tkremenek Do you know if anyone is looking at this?

/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-swift-3.0-branch/swiftpm/Sources/Basic/Thread.swift:50:33: error: extraneous argument label 'block:' in call
16:18:03         self.thread = ThreadImpl(block: theTask)
16:18:03                                 ^~~~~~~~
16:18:03                                  
16:18:03 <unknown>:0: error: build had 1 command failures

@ematejska
Copy link
Contributor

@slavapestov That failure is supposed to be fixed with swiftlang/swift-package-manager#612.

@ematejska
Copy link
Contributor

@swift-ci Please test os x

}

// Hack to apply context-specific @escaping to an AST function type.
static Type adjustFunctionExtInfo(DeclContext *DC,
Copy link
Member

Choose a reason for hiding this comment

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

More specific name here, that is, adjust it to do what?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed to applyNonEscapingFromContext().

@milseman
Copy link
Member

The code LGTM, could you otherwise elaborate on what the risk is from this change?

if (!ty) ty = resolveType(repr, options);
if (!ty || ty->is<ErrorType>()) return ty;
} else if (hasFunctionAttr && fnRepr) {
for (auto i : FunctionAttrs)
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this part doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was there before, I think it's just clearing the attributes so that we don't diagnose about them later.

@slavapestov
Copy link
Contributor Author

Working on a new patch now that adds comments to clarify things.

@slavapestov
Copy link
Contributor Author

@swift-ci Please test os x

@slavapestov
Copy link
Contributor Author

For posterity: rdar://27918871

@slavapestov slavapestov deleted the escaping-fixes-3.0 branch August 19, 2016 05:20
@jrose-apple
Copy link
Contributor

What's the plan for optional function types, too? NTBF for Swift 3?

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.

6 participants