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

Need diagnostic for mismatched Either branches in result builder #78655

Open
dabrahams opened this issue Jan 15, 2025 · 0 comments
Open

Need diagnostic for mismatched Either branches in result builder #78655

dabrahams opened this issue Jan 15, 2025 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@dabrahams
Copy link
Contributor

Description

Not getting this right yields: error: failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)

Reproduction

@resultBuilder
struct ASTBuilder {
  static func buildEither<T>(first x: T) -> (T.Type, T) { (T.self, x) }
  static func buildEither<T>(second x: T) -> (T, T.Type) { (x, T.self) }
  static func buildBlock(_ x: Any...) -> [Any] { x }
}

@ASTBuilder
var x: Any {
  if false { "3" }
  else { 30 }
}

Expected behavior

No request to file a bug; good diagnostic. An explanation of the rules in the docs.

Environment

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

Additional information

The exact rules are weird; there's probably a lot of type deduction going on.

For example, this works:

@resultBuilder
struct ASTBuilder {
  static func buildEither<T>(first x: T) -> T { x }
  static func buildEither<T>(second x: T) -> T { x }
  static func buildBlock(_ x: Any...) -> [Any] { x }
}

@ASTBuilder
var x: Any {
  if false { "3" }
  else { 30 }
}

This of course fails (understandable):

@resultBuilder
struct ASTBuilder {
  static func buildEither<T>(first x: T) -> T { x }
  static func buildEither<T>(second x: T) -> T { x }
  static func buildBlock<T>(_ x: T...) -> [T] { x }
}

var a = 1
@ASTBuilder
var x: Any {
  if a == 0 { "3" }
  else { 30 }
}

One might expect this to work but it fails the same way:

@resultBuilder
struct ASTBuilder {
  static func buildEither<T>(first x: T) -> T { x }
  static func buildEither<T>(second x: T) -> T { x }
  static func buildBlock(_ x: Any...) -> [Any] { x }
  static func buildPartialBlock<T>(first x: T) -> T { x }
  static func buildPartialBlock<T, U>(accumulated: T, next: U) -> (T, U) { (accumulated, next) }
}

var a = 1
@ASTBuilder
var x: Any {
  if a == 0 { "3" }
  else { 30 }
}
@dabrahams dabrahams added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jan 15, 2025
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. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant