swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.3k stars 10.34k forks source link

Another ‘Failed to produce diagnostic for expression’ error with basic switch mismatched type #61106

Open Veence opened 2 years ago

Veence commented 2 years ago

This code:

import SwiftUI

enum StatusBarState {
    case ok (message: String)
    case loading (message: String)
    case error (log: String)
    case warning (log: String)
}

class   StatusBarMessage: ObservableObject {
    var message: StatusBarState = .ok(message: "Starting")
}

struct StatusBarView: View {   
    @StateObject var status: StatusBarMessage

    var body: some View {
        switch status {
            case .ok (let message):
[…]

Where status should be status.message fails to produce a diagnostic (or rather ends up with the message quoted in subject).

LucianoPAlmeida commented 2 years ago

The fails to produce diagnostic should probably have been fixed by @xedin and @ahoppen recent work on pattern matching. On main we see

Exit Code: 1

Command Output (stderr):
--
.../file.swift:21:19: error: unexpected error produced: type '_ErrorCodeProtocol' has no member 'ok'
            case .ok (let message):
                  ^
.../file.swift:21:34: error: unexpected error produced: expression pattern of type '(_) -> StatusBarMessage' cannot match values of type 'StatusBarMessage'
            case .ok (let message):
                                 ^
.../file.swift:22:15: error: unexpected error produced: closure containing control flow statement cannot be used with result builder 'ViewBuilder'
              break
              ^
SwiftUI.ViewBuilder:2:30: note: diagnostic produced elsewhere: struct 'ViewBuilder' declared here
@resultBuilder public struct ViewBuilder {
                             ^

But that type '_ErrorCodeProtocol' has no member 'ok' looks a bit off

xedin commented 2 years ago

Probably at least partially by https://github.com/apple/swift/pull/59210 which changed how these patterns are handled. I think we can resolve this if failure to produce a diagnostic is fixed on main.

hfhbd commented 1 year ago

Similar error:

import SwiftUI

struct ContentView: View {    
    @State private var appState: AppState = .start

    var body: some View {
        switch appState {
        case .Start:
            Button("getText") {
                print("pressed")
            }

        case .Finished(let text):
            Text(text)
        case .Processing(let text):
                ProgressView()
        }
    }
}

enum AppState {
    case start, processing(text: String?), finished(text: String)
}