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-10195] Swift 5 has trouble inferring return type of closure for Data.withUnsafeMutableBytes #52595

Closed
bjhomer opened this issue Mar 27, 2019 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis

Comments

@bjhomer
Copy link
Contributor

bjhomer commented Mar 27, 2019

Previous ID SR-10195
Radar rdar://problem/49339659
Original Reporter @bjhomer
Type Bug
Status Resolved
Resolution Done
Environment

macOS 10.14.4
Xcode 10.2

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

md5: 97d0c7477c9997a2f87954db79cf6866

Issue Description:

Swift 5.0 has trouble inferring the return type of a fairly simple closure.

var data = Data()

let result1 = data.withUnsafeMutableBytes { (bufferPointer: UnsafeMutableRawBufferPointer) in
    // This works
    return 4
}

let result = data.withUnsafeMutableBytes { (bufferPointer: UnsafeMutableRawBufferPointer) in
    // This does not work
    // error: 'UnsafeMutableRawBufferPointer' is not convertible to 'UnsafeMutablePointer<_>'
    // It's like it's trying to call the deprecated version, but failing.
    guard let p = bufferPointer.baseAddress else { return 3 }
    return 4
}

Adding an explicit return type to the closure resolves the error.

@belkadan
Copy link
Contributor

Types are never inferred from the bodies of multi-statement closures, so this is not a new problem…but it is a new diagnostic fail, in an area that a lot of people have been confused. I'm not sure why it would prefer the old one, since it's more generic than this one.

@xedin
Copy link
Member

xedin commented Feb 17, 2021

Main branch compiler produces a correct diagnostic for this:

error: unable to infer complex closure return type; add explicit type to disambiguate
let result = data.withUnsafeMutableBytes { (bufferPointer: UnsafeMutableRawBufferPointer) in
                                         ^
                                                                                          -> <#Result#>

@bjhomer Please use the latest snapshot of main branch from swift.org to verify and close.

@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 diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants