Skip to content

Failed to produce diagnostic when unable to choose correct initializer inside of result builder #65467

Open
@j-f1

Description

@j-f1

Description
When you have (1) a key path to a value of type A, passed to (2) a struct initializer with two overloads which expect the key path to be either of type B or C, (3) inside of a result builder, Swift reports “failed to produce diagnostic for expression; please submit a bug report” instead of a useful error.

Steps to reproduce

struct Model {
    let member = ""
}

struct Wrapper {
    // produces correct error message if one of these is removed
    init(keyPath: KeyPath<Model, Int32>) {}
    init(keyPath: KeyPath<Model, Float>) {}
}

// can be any result builder
@resultBuilder struct MyResultBuilder {
    static func buildBlock(_ components: Void...) -> Void {}
}

// 🛑 Type of expression is ambiguous without more context
let baz = Wrapper(keyPath: \.member)

@MyResultBuilder
// 🛑 Failed to produce diagnostic for expression
var built: Void {
    let baz = Wrapper(keyPath: \.member)
}

Expected behavior

error: TypeConfusion.playground:18:15: error: no exact matches in call to initializer 
    let baz = Wrapper(keyPath: \.member)
              ^

TypeConfusion.playground:7:5: note: candidate expects value of type 'KeyPath<Model, Int32>' for parameter #1 (got 'KeyPath<Model, String>')
    init(keyPath: KeyPath<Model, Int32>) {}
    ^

TypeConfusion.playground:8:5: note: candidate expects value of type 'KeyPath<Model, Float>' for parameter #1 (got 'KeyPath<Model, String>')
    init(keyPath: KeyPath<Model, Float>) {}

Environment

  • Both the integrated Swift toolchain and Swift Development Snapshot 2023-04-25 (a)
  • Xcode 14.3, Build version 14E222b
  • Deployment target: macOS 13.3

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.closuresFeature: closurescompilerThe Swift compiler itselfdeclarationsFeature: declarationsdiagnostics QoIBug: Diagnostics Quality of ImplementationexpressionsFeature: expressionsfailed to produce diagnosticBug → internal error: Failed to produce diagnostic for expressionfuncFeature → declarations: Functionskey pathsFeature: key paths (both native and Objective-C)overloadingFeature: Overloading symbol namesresult buildersFeature: Result buildersswift 5.9type checkerArea → compiler: Semantic analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions