Compiler emits misleading 'Binding' error on `ForEach` when its content accesses an optional property inside a complex View

1. Description

The Swift compiler fails to type-check a SwiftUI view when a ForEach loop's content includes conditional logic to unwrap an optional property.

The code is incorrect, the Ingredient struct does not own a quantity property, but the compiler produces misleading errors related to Binding and generic type inference. The problem is isolated to the if let... block inside the ForEach loop.

2. Steps to Reproduce

  1. Set up a project with standard Identifiable data models
import Foundation
import SwiftData
import SwiftUI

@Model
class Ingredient: Identifiable {
    @Attribute(.unique) var id: UUID
    var name: String
    var image: Data?
    
    init(id: UUID = UUID(), name: String, image: Data? = nil) {
        self.id = id
        self.name = name
        self.image = image
    }
}

  1. Create a parent SwiftUI view like below

  2. Inside the parent container, instantiate the following IngredientsDashboardView:

    // This is the view that fails to compile.
    struct IngredientsDashboardView: View {
        let ingredients: [Ingredient]
    
        var body: some View {
            VStack(alignment: .leading, spacing: 12) {
                Label("Ingredients (\(ingredients.count))", systemImage: "carrot")
                    .font(.headline)
    
                Divider()
    
                ScrollView {
                    VStack(alignment: .leading, spacing: 10) {
                        ForEach(ingredients) { ingredient in
                            HStack(spacing: 12) {
                                Circle()
                                    .fill(Color.orange.opacity(0.3))
                                    .frame(width: 8, height: 8)
    
                                Text(ingredient.name)
                                    .font(.subheadline)
    
                                Spacer()
    
                                // THIS BLOCK IS THE TRIGGER.
                                // quantity is not a member of ingredient, but compiler give misleading error messages
                                if let qty = ingredient.quantity, !qty.isEmpty {
                                    Text(qty)
                                        .font(.caption)
                                        .foregroundColor(.secondary)
                                        .padding(.horizontal, 6)
                                        .padding(.vertical, 2)
                                        .background(Capsule().fill(Color(.systemGray5)))
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
  3. Attempt to compile the project.

3. Expected Behavior

The cmpiler should throw an unwrapping errors.

4. Actual Behavior

The build fails with misleading errors pointing to the ForEach declaration.

StepsDetailComponents/StepFullScreenView.swift:396:13 Generic parameter 'C' could not be inferred

StepsDetailComponents/StepFullScreenView.swift:396:21 Cannot convert value of type '[Ingredient]' to expected argument type 'Binding<C>'

StepFullScreenView.swift:402:21 Initializer 'init(_:)' requires that 'Binding<Subject>' conform to 'StringProtocol'

5. Workaround

None

6. Environment

  • Swift version: swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
  • Xcode version: Version 16.3 (16E140)
  • OS: 15.4.1 (24E263)