Skip to content

Failed to produce diagnostic: resultBuilder containing switch containing invalid case with an associated value #82520

Open
@k-natt

Description

@k-natt

Description

Per the title, see example. When a resultBuilder contains an if case … = … or a switch where the case is both invalid and has an associated object, the compiler displays the "Failed to produce diagnostic for expression; please submit a bug report" message. Removing the resultBuilder, correcting the case, or removing the associated value (whether or not empty parentheses remain) each fix the issue and behave as expected.

I assume it applies to all resultBuilders, though I've only actually tested @ViewBuilder.

Reproduction

if case:

import SwiftUI
enum E {
    case e
}
@ViewBuilder
func foo(_ e: E) { // error: failed to produce diagnostic for expression; please submit a bug report
    if case .a(.x) = e {
        EmptyView()
    }
}

switch:

import SwiftUI
enum E {
    case e
}
@ViewBuilder
func foo(_ e: E) { // error: failed to produce diagnostic for expression; please submit a bug report
    switch e {
    case .a(.x):
        EmptyView()
    }
}

Complete command line example:

----------% cat -n test.swift
     1	import SwiftUI
     2	enum E {
     3	    case e
     4	}
     5	@ViewBuilder
     6	func foo(_ e: E) {
     7	    if case .a(.x) = e {
     8	        EmptyView()
     9	    }
    10	}
----------% xcrun swiftc --version
swift-driver version: 1.127.5.3 Apple Swift version 6.2 (swiftlang-6.2.0.10.950 clang-1700.3.10.950)
Target: arm64-apple-macosx15.0
----------% xcrun -l swiftc test.swift
env SDKROOT=/Applications/Xcode-26b2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX26.0.sdk /Applications/Xcode-26b2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc test.swift
test.swift:6:6: error: failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)
 4 | }
 5 | @ViewBuilder
 6 | func foo(_ e: E) {
   |      `- error: failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)
 7 |     if case .a(.x) = e {
 8 |         EmptyView()
----------%                                                                                                                                                1

Expected behavior

Compiler should print a useful diagnostic, likely the same as outside the resultBuilder context.

For if case…:

  • Type of expression is ambiguous without a type annotation

For switch:

  • Cannot infer contextual base in reference to member 'x'
  • Type 'E' has no member 'a'

Environment

swift-driver version: 1.127.5.3 Apple Swift version 6.2 (swiftlang-6.2.0.10.950 clang-1700.3.10.950)
Target: arm64-apple-macosx15.0

Occurs at least as far back as I have Xcode installed:
swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx15.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions