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

"failed to produce diagnostic for expression" when working with multiple overlays in SwiftUI #66454

Open
alexwlchan opened this issue Jun 8, 2023 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. expressions Feature: expressions failed to produce diagnostic Bug → internal error: Failed to produce diagnostic for expression

Comments

@alexwlchan
Copy link
Contributor

Description
I have a program that fails to compile with the error:

failed to produce diagnostic for expression; please submit a bug report

Steps to reproduce
The following program is a minimal reproduction of the error:

import SwiftUI

struct Asset {
    func state() -> ReviewState? {
        return .Approved
    }
}

enum ReviewState {
    case Approved
    
    func color() -> Color {
        return .green
    }
    
    func icon() -> Image {
        return Image(systemName: "checkmark.circle.fill")
    }
}

struct ThumbnailImage: View {
    var asset: Asset
    
    var body: some View {
        Image(systemName: "checkmark.circle.fill")
            .overlay(
                RoundedRectangle.stroke(asset.state?.color() ?? .gray)
            )
            .overlay(alignment: Alignment(horizontal: .leading, vertical: .top)) {
                if let thisState = asset.state {
                    thisState.icon()
                }
            }
    }
}

@main
struct BlinkReviewerApp: App {
    var body: some Scene {
        WindowGroup {
            ThumbnailImage(asset: Asset(id: "1"))
        }
    }
}

When I try to build this program as a macOS app in Xcode 14.3.1, I get the "failed to produce diagnostic for expression" error. I get a similar error if I save this code to a file and try to compile it on the command-line:

$ swift s.swift
s.swift:37:1: error: 'main' attribute cannot be used in a module that contains top-level code
@main
^
s.swift:1:1: note: top-level code defined in this source file
import SwiftUI
^
s.swift:1:1: note: pass '-parse-as-library' to compiler invocation if this is intentional
import SwiftUI
^
s.swift:24:25: error: failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)
    var body: some View {
                        ^

Expected behavior
Swift gives me a descriptive error of what I've done wrong.

Environment

  • Swift compiler version info

    $ swift -version
    swift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100)
    Target: arm64-apple-macosx13.0
  • Xcode version info

    $ xcodebuild -version
    Xcode 14.3.1
    Build version 14E300c
  • Deployment target: macOS 13.3

Additional context
There are lots of ways you can tweak the program and helpful diagnostics start falling out, including:

  • Replacing thisState.icon() with a hard-coded icon Image(systemName: "checkmark.circle.fill")
  • Replacing asset.state?.color() with a hard-coded color .green
  • Removing either of the .overlay(…) modifiers
  • Removing the alignment: parameter passed to the second overlay modifier
  • Removing the Asset struct and passing a ReviewState directly into ThumbnailImage
@alexwlchan alexwlchan added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jun 8, 2023
@xedin
Copy link
Member

xedin commented Jun 8, 2023

@alexwlchan looks like this has been fixed on main:

error: instance member 'stroke' cannot be used on type 'RoundedRectangle'; did you mean to use a value of this type instead?
                RoundedRectangle.stroke(asset.state?.color() ?? .gray)
                ^~~~~~~~~~~~~~~~
error: function value was used as a property; add () to call it
                RoundedRectangle.stroke(asset.state?.color() ?? .gray)
                                                   ^
                                                    ()
error: function 'thisState' was used as a property; add () to call it
                    thisState.icon()
                    ^
                             ()
error: value of optional type 'ReviewState?' must be unwrapped to refer to member 'icon' of wrapped base type 'ReviewState'
                    thisState.icon()
                    ^
note: chain the optional using '?' to access member 'icon' only for non-'nil' base values
                    thisState.icon()
                    ^
                             ?
note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
                    thisState.icon()
                    ^
                             !
<stdin>:41:45: error: argument passed to call that takes no arguments
            ThumbnailImage(asset: Asset(id: "1"))
                                       ~~~~~^~~~

Could you please verify using the latest main toolchain from swift.org?

@hborla hborla added 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 Apr 27, 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. expressions Feature: expressions failed to produce diagnostic Bug → internal error: Failed to produce diagnostic for expression
Projects
None yet
Development

No branches or pull requests

3 participants