swiftlang / swift

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

Failed to produce diagnostic for expression in SwiftUI code #73750

Closed moreindirection closed 4 months ago

moreindirection commented 6 months ago

Description

While writing a SwiftUI view, I got the following compiler error:

Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)

I isolated the cause to the following short snippet. I eventually figured out that the problem is on line 21. Where the example code has:

ForEach(categories, id: \.categoryName) { category in

it needs to handle the case where categories is nil. Replacing this line with the following fixes it:

ForEach(categories ?? [], id: \.categoryName) { category in

Note that if I remove the surrounding Group, I get the correct error:

Value of optional type '[CatalogCategory]?' must be unwrapped to a value of type '[CatalogCategory]'

so the Group is necessary to reproduce the bug.

Reproduction

import Foundation
import SwiftUI

struct CatalogCategory: Codable, Identifiable {
  let categoryName: String

  var id: String { return self.categoryName }
}

struct BasicsCatalogView: View {
  @State var categories: [CatalogCategory]? = nil

  var body: some View {
    Group {
      if self.categories == nil {
        Text("Loading...")
      }
      else {
        List {
          ForEach(categories, id: \.categoryName) { category in
            Text(category.categoryName.capitalized)
          }
        }
      }
    }
  }
}

Expected behavior

Instead of getting this message (which is raised at the surrounding var body: some View declaration), I should have received an error on the ForEach line, telling me that I was passing an optional to a method that expects a collection.

As described above, without the surrounding Group, I get the correct error on the correct line.

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

xedin commented 4 months ago

Looks like this has been fixed on main recently:

error: value of optional type '[CatalogCategory]?' must be unwrapped to a value of type '[CatalogCategory]'
18 |       else {
19 |         List {
20 |           ForEach(categories, id: \.categoryName) { category in
   |                   |- error: value of optional type '[CatalogCategory]?' must be unwrapped to a value of type '[CatalogCategory]'
   |                   |- note: coalesce using '??' to provide a default when the optional value contains 'nil'
   |                   `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
21 |             Text(category.categoryName.capitalized)
22 |           }

Please use a recent main branch snapshot from swift.org to verify and close.

AnthonyLatsis commented 4 months ago

Confirmed with a June 3 main as well. Let us close this on the spot.

moreindirection commented 4 months ago

As the OP, can confirm this is now fixed - thanks!