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-10013] Compiler does not infer the correct method #52416

Open
sethfri opened this issue Feb 28, 2019 · 2 comments
Open

[SR-10013] Compiler does not infer the correct method #52416

sethfri opened this issue Feb 28, 2019 · 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

@sethfri
Copy link

sethfri commented Feb 28, 2019

Previous ID SR-10013
Radar rdar://48463040
Original Reporter @sethfri
Type Bug

Attachment: Download

Environment

Xcode 10.1

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

md5: 4434578cd35fc77d0bb9598645f44df4

Issue Description:

Hi there!

I have a function with the following signature:

func handle(handler: ((_ foo: String) -> Void))

I have an extension function on the class with the following signature:

func handle<T>(handler: ((_ foo: String) throws -> T)) throws -> T

When I try to write code like the following, Swift infers the handle function that doesn't throw or return a value instead of the one that does, resulting in a compiler error:

let testInt = try bugTest.handle { string in

{{ print(string)}}

{{ }}

{{ if (string == "baz") {}}

{{ throw TestError.baz}}

    }

{{ }}

{{ return 42}}

{{}}}

I've attached a very detailed playground demonstrating exactly what I'm trying to do and why, with the reproducible test case included.

Thanks!

@belkadan
Copy link
Contributor

I think it's just because the compiler has no way of inferring what T is (since the bodies of multi-statement closures are not used when inferring the type of the closure except for whether or not it throws), so that makes this a diagnostics issue.

@belkadan
Copy link
Contributor

You'll have to provide a return type for the closure or an explicit type on testInt to get it to work.

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

No branches or pull requests

2 participants