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

Type checker fails when calling a function with an Optional<() -> Void> ternary expression as a parameter #78113

Open
TheHarcker opened this issue Dec 11, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. diagnostics QoI Bug: Diagnostics Quality of Implementation expressions Feature: expressions failed to produce diagnostic Bug → internal error: Failed to produce diagnostic for expression type checker Area → compiler: Semantic analysis

Comments

@TheHarcker
Copy link
Contributor

TheHarcker commented Dec 11, 2024

Description

No response

Reproduction

When compiling the following, the compiler fails with the error Type of expression is ambiguous without a type annotation

func takingAnOptionalClosure(closure: (() -> Void)?) {}
func someFunc() {}
func someOtherFunc() {
    let a = false ? someFunc : nil  // Fine
    takingAnOptionalClosure(closure: a)   // Fine
    takingAnOptionalClosure(closure: false ? someFunc : nil)  // Error
}

The above issue also leads to another diagnostic when the function call is within a SwiftUI ViewBuilder.

import SwiftUI
extension View {
    public func takingAnOptionalClosure(closure: (() -> Void)?) -> some View {
        self
    }
}
struct SwiftUIExample: View {
    func aFunc() {}

    var body: some View {
        Text("Hi")
        .takingAnOptionalClosure(closure: false ? aFunc : nil)
    }
}

Here the compiler returns the error Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) on the line of var body

POC swift package:
POC.zip

Expected behavior

I expected the code to successfully compile without any warnings or errors

Environment

The issues mentioned above happens when compiling the attached POC in Xcode 16.1 (16B40), Xcode 16.2 (16C5031c), and with swift build from the terminal. The problem does not seem to occur when compiling with swiftc

Output of swiftc -version:

swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.9 clang-1600.0.30.1)
Target: x86_64-apple-macosx15.0

Additional information

The problem first occurred in a file similar to the SwiftUI example above, after a class containing the equivalent of someFunc had been annotated with @ MainActor

@TheHarcker TheHarcker added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Dec 11, 2024
@xedin xedin added diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis failed to produce diagnostic Bug → internal error: Failed to produce diagnostic for expression expressions Feature: expressions and removed triage needed This issue needs more specific labels labels Dec 11, 2024
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. diagnostics QoI Bug: Diagnostics Quality of Implementation expressions Feature: expressions failed to produce diagnostic Bug → internal error: Failed to produce diagnostic for expression type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants