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-6725] @autoclosure causes ambiguity warnings that can be fixed by wrapping in identity function #49274

Open
weissi opened this issue Jan 9, 2018 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@weissi
Copy link
Member

weissi commented Jan 9, 2018

Previous ID SR-6725
Radar rdar://problem/36376946
Original Reporter @weissi
Type Bug

Attachment: Download

Environment
$ /Library/Developer/Toolchains/swift-4.1-DEVELOPMENT-SNAPSHOT-2017-12-30-a.xctoolchain/usr/bin/swiftc --version
Apple Swift version 4.1-dev (LLVM 0fcc19c0d8, Clang 1696f82ad2, Swift 691139445e)
Target: x86_64-apple-darwin17.4.0
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 7b0d8a53ec125efb1af7dad400438ba5

Issue Description:

The following code

public func pretendsEscaping(_ body: @escaping () -> Int) -> Int {
    return body()
}

func foo(_ value: @autoclosure () -> Int) -> Int {
    return withoutActuallyEscaping(value) { (value: @escaping () -> Int) -> Int in
        return pretendsEscaping(value)
    }
}

print(foo(42))

should just print 42 but unfortunately it doesn't compile:

$ /Library/Developer/Toolchains/swift-4.1-DEVELOPMENT-SNAPSHOT-2017-12-30-a.xctoolchain/usr/bin/swiftc test.swift 
test.swift:6:12: error: expression type '(_, (_) throws -> _) throws -> _' is ambiguous without more context
    return withoutActuallyEscaping(value) { (value: @escaping () -> Int) -> Int in
           ^~~~~~~~~~~~~~~~~~~~~~~

Adding more type annotations also doesn't seem to help.

However, just wrapping the @autoclosure value value in the identity function id makes it compile. That's surprising 🙂

public func pretendsEscaping(_ body: @escaping () -> Int) -> Int {
    return body()
}

func foo(_ value: @autoclosure () -> Int) -> Int {
    func id<T>(_ value: T) -> T {
        return value
    }
    return withoutActuallyEscaping(id(value)) { (value: @escaping () -> Int) -> Int in
        return pretendsEscaping(value)
    }
}

print(foo(42))

compiler is the latest 4.1 snapshot but the same problem exists in 4.0 (and 3.1).

@belkadan
Copy link
Contributor

belkadan commented Jan 9, 2018

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants