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-11155] Severely misleading type inference of closure argument #53552

Closed
atrick opened this issue Jul 18, 2019 · 3 comments
Closed

[SR-11155] Severely misleading type inference of closure argument #53552

atrick opened this issue Jul 18, 2019 · 3 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

@atrick
Copy link
Member

atrick commented Jul 18, 2019

Previous ID SR-11155
Radar rdar://problem/53234368
Original Reporter @atrick
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee @atrick
Priority Medium

md5: 39662feb84c3367214be73ab8dc0749e

Issue Description:

import Foundation
func testTypeInferClosureArg(data: Data, array: inout [UInt8]) {
  data.withUnsafeBytes { (rawBuffer: UnsafeRawBufferPointer) in
    // I intentionally forgot the "contentsOf:" label here...
    array.append(rawBuffer)
  }
}

I intentionally forgot the "contentsOf:" label when passing the closure argument, rawBuffer, into array append. This version of array append takes a single Element, UInt8, rather than a sequence of those elements.

I expect to get an error at the call to array.append, stating that UnsafeRawBufferPointer cannot be converted to UInt8.

Instead I get this error:

 error: 'UnsafeRawBufferPointer' is not convertible to 'UnsafePointer<_>'
  data.withUnsafeBytes { (rawBuffer: UnsafeRawBufferPointer) in
                         ^

The type checker appears to be picking the wrong overload of withUnsafeBytes ❓ even though the argument type is explicitly provided. This is a problem because the diagnostic instructs the programmer to use the old deprecated API, potentially exposing undefined behavior.

Note that when I remove the closure argument type, I get this error:
"type of expression is ambiguous without more context"

Which prompts me to add the closure argument type. Never do I get an error pointing to the incorrect array.append call.

This problem isn't specific to array.append--that's just an easy way to demonstrate it. It seems to occur whenever the expression type checker fails to resolve something inside the closure...

@atrick
Copy link
Member Author

atrick commented Jul 18, 2019

@swift-cicreate

@xedin
Copy link
Member

xedin commented Jul 18, 2019

This is actually a problem related to name lookup filtering results based on labels. I think we should remove UR_LabelMismatch unviability reason and let solver handle label matching.

@xedin
Copy link
Member

xedin commented Jul 29, 2019

Fixed by #26302 Please verify using next nightly build.

@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

2 participants