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

[SR-15665] Failed to produce diagnostic for expression, when using Builder pattern with Generics #393

Open
swift-ci opened this issue Dec 29, 2021 · 0 comments
Labels
bug Compiler swift

Comments

@swift-ci
Copy link
Contributor

@swift-ci swift-ci commented Dec 29, 2021

Previous ID SR-15665
Radar None
Original Reporter Urtaq (JIRA User)
Type Bug

Attachment: Download

Environment

Xcode 13.2(included Swift 5.5)

OS: MacOS 12.1 Monterey(plz, refer the details in the 1st screen shot)

Testing Device on running the codes: iPad Air 9gen., iOS simulators

Additional Detail from JIRA
Votes 0
Component/s Compiler, swift, SwiftSyntax
Labels Bug
Assignee None
Priority Medium

md5: 08d1c24459a3c9359296d685692609e4

Issue Description:

I've tried to use the Builder pattern in the swift, especially with the Generics.

But the swift complier said "Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)" and include the project".

Originally, I've tried to configure the similar with the SwiftUI struct's styles for the passing values.

My codes are like below.

I defined the custom button styles in the separated target as framework. And I'd like to set the Button background color(or using Gradient), using the Builder pattern.

// Button Style class in the CustomStyles target as framework
import Foundation
import SwiftUI
import SwiftExtension

public enum ButtonState {
    case normal
    case highlighted
}

public class RoundedButtonStyleBuilder<Background: View & ShapeStyle> {
    var buttonProperties = [ButtonState: Background]()

    public init() {}

    public func color(
        _ color: Background,
        for buttonState: ButtonState
    ) -> RoundedButtonStyleBuilder {
        self.buttonProperties[buttonState] = color

        return self
    }

    public func build() -> RoundedButtonStyle<Background> {
        var roundedButtonStyle = RoundedButtonStyle<Background>()
        roundedButtonStyle.buttonProperties = buttonProperties
        return roundedButtonStyle
    }
}

public struct RoundedButtonStyle<Background: View & ShapeStyle>: ButtonStyle {

    var buttonProperties = [ButtonState: Background]()
    var propertyBuilder = RoundedButtonStyleBuilder<Background>()

    public init() {}

    public func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .font(.system(size: 32.0, weight: .medium))
            .foregroundColor(.white)
            .background(configuration.isPressed ? buttonProperties[.highlighted] : buttonProperties[.normal])
            .cornerRadius(16.0)
    }
}

The structs & classes related to the Builder patterns are complied well.

But, the problem is occurred on the codes to use the Builder.

Specially when I use the different struct types as a property of the custom Button styles, it's occurred the compile error. Please refer the below codes.

// Custom View using the custom button style with the Builder
import Foundation
import SwiftUI
import SwiftExtension
import CustomStyles


public struct ReceiptView: View {
    public var body: some View {
        VStack(alignment: .center) {
            Spacer().frame(minHeight: 10.0)
            VStack(alignment: .center, spacing: 16.0) {
                Image("clinicLogo")
                Text("Clinic Name")
                    .font(.system(size: 40.0, weight: .medium))
            }
            Spacer().frame(maxHeight: 112.0)
            Button(
                action: {
                    debugPrint(#function)
                },
                label: {
                    VStack(alignment: .center, spacing: 0.0) {
                        Spacer().frame(height: 64.0)
                        Image("icon")
                        Spacer().frame(height: 16.0)
                        Text("Receipt")
                        Spacer().frame(height: 72.0)
                    }
            })
            .frame(width: 440.0, height: 440.0)
            .buttonStyle(
                RoundedButtonStyleBuilder()
                    .color(
                        LinearGradient(
                            colors: [.lightTheme, .defaultTheme],
                            startPoint: .top,
                            endPoint: .bottom
                        ),
                        for: .normal
                    )
                    .color(Color.highlighted, for: .highlighted) // This case causes the compile error!!!!!
//                    .color(
//                        LinearGradient(
//                            colors: [.defaultTheme],
//                            startPoint: .top,
//                            endPoint: .bottom
//                        ),
//                        for: .highlighted
//                    ) // If the same type is passed, it's not occurred the compile error!!
                    .build()
            )
            Spacer().frame(height: 24.0)
        }
        .navigationBarTitleDisplayMode(.inline)
        .navigationBarHidden(true)
    }
}

So, I followed the compile error message and I reported the bug.

If I'm using the wrong way to use the Generics in Swift, plz, let me know & guide me.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Compiler swift
Projects
None yet
Development

No branches or pull requests

2 participants