swiftlang / swift

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

Failed to produce diagnostic for invalid projected value in ForEach content #74752

Open mateusrodriguesxyz opened 4 months ago

mateusrodriguesxyz commented 4 months ago

Description

The compiler reports Failed to produce diagnostic for expression when trying to use projected value in closure but the parameter is not actually a property wrapper.

Reproduction

struct User: Identifiable {
    let id = UUID()
    let name: String
}

struct ContentView: View {

    let users: [User] = []

    var body: some View {
        ForEach(users) { $user in
            Text(user.name)
        }
    }
}

Expected behavior

It would be good a diagnostic saying that the parameter type doesn't support projected value and a suggestion to remove the $, something like:

ForEach(users) { $user in // ❌ Type 'User' doesn't support projected value; did you mean to use 'user' instead? Remove '$'
    Text(user.name)
}

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

mateusrodriguesxyz commented 4 months ago

I tried running this code with Swift 6.0 toolchain in Xcode 15.3 and it does report Inferred projection type 'User' is not a property wrapper, but only for a second and then goes back to Failed to produce diagnostic for expression.