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-3632] Swift is unable to properly infer the type of closures with inout parameters without specific type information. #46217

Closed
swift-ci opened this issue Jan 13, 2017 · 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

@swift-ci
Copy link
Collaborator

Previous ID SR-3632
Radar None
Original Reporter calebd (JIRA User)
Type Bug
Status Closed
Resolution Done
Environment

Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9

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

md5: 756f65893b77be61ba04a965d87e427b

duplicates:

  • SR-1976 Closure signature in Swift 3 required for inout params
  • SR-3520 Generic function taking closure with inout parameter can result in a variety of compiler errors or EXC_BAD_ACCESS

Issue Description:

A function that takes a closure that has an inout parameter requires explicit type info before it can be used. The lack of any explicit type info causes the compiler to fail in two distinct ways.

// A function that demonstrates this issue.
func apply<Value>(_ value: Value, _ transform: (inout Value) -> Void) -> Value {
    var value = value
    transform(&value)
    return value
}

// This code works as expected.
func testOne() -> [Int] {
    return apply([1, 2, 3], {
        $0.append(4)
    })
}

// This code works as expected.
let testTwo = apply([1, 2, 3], { (array: inout [Int]) -> Void in
    array.append(4)
})

// This code fails to compile because the "type of expression is ambiguous
// without more context". After further investigation the type appears to be
// ambiguous because the closure provided does not necessarily have an inout
// parameter (even though that is specified in the signature of apply).
let testThree = apply([1, 2, 3], {
    $0.append(4)
})

// This code also fails but for different reasons. I expected it to work because
// testOne compiles without issue. It does not result in an error with a
// diagnostic and source location but instead says "parameters may not have
// the 'var' specifier".
let testFour: [Int] = apply([1, 2, 3], {
    $0.append(4)
})
@swift-ci
Copy link
Collaborator Author

Comment by Caleb Davenport (JIRA)

Thanks to @JaviSoto for pointing out that this is already fixed in master. Tested and confirmed.

@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 Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

1 participant