Skip to content
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

[SR-4188] withoutActuallyEscaping doesn't accept an @autoclosure argument #46771

Closed
ole opened this issue Mar 7, 2017 · 6 comments
Closed

[SR-4188] withoutActuallyEscaping doesn't accept an @autoclosure argument #46771

ole opened this issue Mar 7, 2017 · 6 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker

Comments

@ole
Copy link
Contributor

ole commented Mar 7, 2017

Previous ID SR-4188
Radar rdar://problem/30906031
Original Reporter @ole
Type Bug
Status Resolved
Resolution Done
Environment

macOS 10.12.3 (16D32), Xcode 8.2 (8C38) using snapshot 3.1-DEVELOPMENT-SNAPSHOT-2017-03-06-a in a macOS Command Line project (Xcode template)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee @ole
Priority Medium

md5: 760a30461350d82b8f45ad66661d395b

Issue Description:

This (contrived) example, adopted from the documentation for withoutActuallyEscaping, compiles and runs as expected:

func allValues(in array: [Int], smallerThan: () -> Int) -> Bool {
    return withoutActuallyEscaping(smallerThan) { escapableF in
        array.lazy.filter { $0 >= escapableF() }.isEmpty
    }
}

let result = allValues(in: [1,2,3], smallerThan: { 10 })
print(result) // true

When I make the function argument an @autoclosure like so:

func allValues(in array: [Int], smallerThan: @autoclosure () -> Int) -> Bool {
    return withoutActuallyEscaping(smallerThan) { escapableF in
        array.lazy.filter { $0 >= escapableF() }.isEmpty
    }
}

let result = allValues(in: [1,2,3], smallerThan: 10)
print(result)

compilation fails on line 2 with this error: "Expression type '(, () throws -> _) throws -> _' is ambiguous without more context".

I tried adding type annotations to get rid of the alleged ambiguity that seemed to confuse the type checker, but I haven't found a way to get rid of the error. After I added full type annotations like this:

    return withoutActuallyEscaping(smallerThan as (() -> Int)) {
        (escapableF: () -> Int) -> Bool in
        array.lazy.filter { $0 >= escapableF() }.isEmpty
    }

the ambiguity error remained and another error appeared on the array.lazy... line: "Closure use of non-escaping parameter 'escapableF' may allow it to escape".

Tested with 3.1-DEVELOPMENT-SNAPSHOT-2017-03-06-a.

Background: the above example is contrived. My actual use case is that I'd like to use withoutActuallyEscaping inside a custom assertion function that should take its arguments as autoclosures.

@jckarter
Copy link
Member

jckarter commented Mar 7, 2017

@swift-ci create

@ole
Copy link
Contributor Author

ole commented Mar 7, 2017

As suggested by @jckarter in this tweet, a workaround is to pass the @autoclosure as a regular closure to another function before invoking withoutActuallyEscaping. This works:

func allValues(in array: [Int], smallerThan: @autoclosure () -> Int) -> Bool {
    func allValuesImpl(in array: [Int], smallerThan: () -> Int) -> Bool {
        return withoutActuallyEscaping(smallerThan) { escapableF in
            array.lazy.filter { $0 >= escapableF() }.isEmpty
        }
    }
    return allValuesImpl(in: array, smallerThan: smallerThan)
}

let result = allValues(in: [1,2,3], smallerThan: 10)
print(result)

@ole
Copy link
Contributor Author

ole commented Apr 1, 2018

Just to give an update, this still doesn't compile in Swift 4.1 (tested on macOS 10.13.3 with Xcode 9.3 (9E145)).

@xedin
Copy link
Member

xedin commented Nov 11, 2018

This problem is resolved by #20152 I'm going to add a test-case in the separate PR as well.

@ole Please validate using next available snapshot of the master branch.

@ole
Copy link
Contributor Author

ole commented Nov 12, 2018

@xedin Will do. Thanks for working on this!

@ole
Copy link
Contributor Author

ole commented Nov 18, 2018

@xedin I confirm that it works. Thanks again!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker
Projects
None yet
Development

No branches or pull requests

3 participants