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

Passing a binding as a parameter without $ leads to SwiftCompile failed with nonzero exit code (Swift 5.10) #75416

Open
Loradim opened this issue Jul 23, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. diagnostics QoI Bug: Diagnostics Quality of Implementation expressions Feature: expressions property wrappers Feature: property wrappers SwiftUI Flag: Involves SwiftUI type checker Area → compiler: Semantic analysis type inference Feature: type inference

Comments

@Loradim
Copy link

Loradim commented Jul 23, 2024

Description

I created a simple custom (bottom) sheet as an extension to View to show (and close) a bottom sheet based on a @State variable (did this as the Botton sheet should be used at multiple Views, possibly trigged by a @State var show: Bool or a view model @Published var isShown: Bool)

extension View {
    func customBottomSheet(@Binding isPresented: Bool) -> some View {
        self.sheet(isPresented: $isPresented,
                   content: {
            Button("Dismiss") {
                isPresented.toggle()
            }
            .buttonStyle(BorderedProminentButtonStyle())
        })
    }
}

I thought I had to call this custom bottom sheet like this:

struct ContentView: View {
    @State var show = false

    var body: some View {
        VStack {
            Button("Tap me") {
                show.toggle()
            }
            .buttonStyle(BorderedProminentButtonStyle())
        }
        .padding()
        .customBottomSheet(isPresented: $show)
    }
}

but it results in this error message:
Error Message: Cannot convert value '$show' of type 'Binding<Bool>' to expected type 'Bool', use wrapped value instead

Suggested Fix (by Xcode - will create a separate ticket via Feedback Assistant for this) is: Remove the $

After applying this "fix", the compiler has a nonzero exit code

.customBottomSheet(isPresented: show) // Command SwiftCompile failed with a nonzero exit code

The correct, working code would be (not intuitive for me):

.customBottomSheet($isPresented: $show) // working

I'm not sure if the working code is the correct one by Swift language definition, but I wouldn't expect the compiler to exit without a meaningful error message.

If I can help with any additional information, please let me know!
~Heiko

Reproduction

import SwiftUI

extension View {
    func customBottomSheet(@Binding isPresented: Bool) -> some View {
        self.sheet(isPresented: $isPresented,
                   content: {
            Button("Dismiss") {
                isPresented.toggle()
            }
            .buttonStyle(BorderedProminentButtonStyle())
        })
    }
}

struct ContentView: View {
    @State var show = false

    var body: some View {
        VStack {
            Button("Tap me") {
                show.toggle()
            }
            .buttonStyle(BorderedProminentButtonStyle())
        }
        .padding()
//        .customBottomSheet(isPresented: show) // Command SwiftCompile failed with a nonzero exit code
//        .customBottomSheet(isPresented: $show) // Error Message: Cannot convert value '$show' of type 'Binding<Bool>' to expected type 'Bool', use wrapped value instead
                                                 // Fix: does remove the $, making this the first non-zero exit code error
        .customBottomSheet($isPresented: $show) // working
    }
}

#Preview {
    ContentView()
}

Expected behavior

I can't tell if the "working code" is the correct one by Swift language standards, but I would expect a meaningful error message from the compiler instead of existing with a nonzero code and no information.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Additional information

No response

@Loradim Loradim added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jul 23, 2024
@xedin xedin added diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis property wrappers Feature: property wrappers type inference Feature: type inference expressions Feature: expressions SwiftUI Flag: Involves SwiftUI and removed triage needed This issue needs more specific labels labels Jul 24, 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. diagnostics QoI Bug: Diagnostics Quality of Implementation expressions Feature: expressions property wrappers Feature: property wrappers SwiftUI Flag: Involves SwiftUI type checker Area → compiler: Semantic analysis type inference Feature: type inference
Projects
None yet
Development

No branches or pull requests

2 participants