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-893] Compiler fails to infer return type of the transform closure in a map call #43505

Closed
swift-ci opened this issue Mar 7, 2016 · 2 comments
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

@swift-ci
Copy link
Collaborator

swift-ci commented Mar 7, 2016

Previous ID SR-893
Radar None
Original Reporter Neil Faiman (JIRA User)
Type Bug
Environment

Observed with Xcode 7.2.1 on OS X 10.11.3

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

md5: c0ac1ad743999c1756dbbe9d58a76fbd

is duplicated by:

  • SR-1664 Improve diagnostic for inability to infer return types from multi line closures
  • SR-1773 ..< is ambiguous together with map for certain closures

relates to:

  • SR-1570 'Generic parameter 'Result' could not be inferred' compiler error for wrapper functions like autoreleasepool that return the result of their closure argument.

Issue Description:

When the map method is called with closure for its transform argument, and the closure return type specification is omitted, everything works fine if the type of the returned expression is implicit – for example,

let x1 = [1,2].map { _ in
    return "abc"
}

But if the same expression is assigned to a variable, and the variable is returned:

let x2 = [1,2].map { _ in
    let x = "abc"
    return x
}

then the compiler gives an error. In Xcode 7.2.1, the error is

Cannot invoke 'map' with an argument list of type '(@noescape (Int) throws -> _)'
Expected an argument list of type '(@noescape (Self.Generator.Element) throws -> T)'

It seems that in both calls, the transform closure is returning a String, and the compiler knows it is a String, so the error in the second call looks like a compiler bug.

(Making the return type explicit – map { _ -> String in ... } – works around the bug.)

@belkadan
Copy link
Contributor

belkadan commented Mar 8, 2016

Discussed this a bit on the list; while this is deliberately not supported, ChrisL pointed out that we could do a much better job explaining the problem to the user.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Jul 6, 2022

Fixed with SE-0326

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

No branches or pull requests

3 participants