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

Xcode prompts the error at var body: some View. I cannot understand what's wrong here because there was no changes after the last successful build.

To solve I have already done:

  • Clean Build Folder for several times
  • Restart Xcode

Nothing worked.

import SwiftUI

struct HomeView: View {

    @StateObject private var viewModel = HomeViewModel()
    @State var tabSelection: Int = 0

    var body: some View {
        TabView {
            NavigationStack {
                Group {
                    if viewModel.entries.isEmpty {
                        EmptyStateView()
                    } else {
                        EntriesGridView(entries: viewModel.entries, tags: viewModel.tags, selectedTag: $viewModel.selectedTags)
                            
                    }
                }
            }
            .tabItem { Label("Saved", systemImage: "bookmark") }
            
            SettingsView()
                .tabItem { Label("Settings", systemImage: "gear") }
        }
        .onAppear {
            viewModel.fetchTags()
            viewModel.fetchEntries()
        }
    }
}

#Preview {
    HomeView()
}

Other related parts without any errors:

HomeViewModel.swift

import SwiftUI
import Firebase
import FirebaseFirestore
import FirebaseFirestoreSwift
import os.log

@MainActor
class HomeViewModel: ObservableObject {
    @Published var entries: [DokuEntry] = []
    @Published var tags: [String] = []
    @Published var selectedTags: String?
    
    private var db = Firestore.firestore()
    private let logger = Logger(subsystem: "dev.egecam.Doku", category: "HomeViewModel")
    
    init() {
        logger.log("HomeViewModel initialized")
        fetchEntries()
        fetchTags()
    }

// rest of the code
}

EntrisGridView.swift

import SwiftUI
import LinkPresentation

struct EntriesGridView: View {
    @Environment(\.colorScheme) var colorScheme
    @StateObject private var viewModel = HomeViewModel()
    
    @State private var entryFavouriteStatus: Bool = false
    @Binding var selectedTag: String?
    
    let entries: [DokuEntry]
    let tags: [String]
    let columns = Array(repeating: GridItem(.flexible(), spacing: 18), count: 2)
    
    private let impactMed = UIImpactFeedbackGenerator(style: .medium)
    
    var filteredEntries: [DokuEntry] {
        if let selectedTag = selectedTag {
            return entries.filter({ $0.tags.contains(selectedTag) })
        } else {
            return entries
        }
    }
    // rest of the code

}
Answered by egecam in 801853022

Update: Solved. Turns out that the problem occurs because EntriesGridView initialized in the wrong order.

Wrong:

EntriesGridView(entries: viewModel.entries, tags: viewModel.tags, selectedTag: $viewModel.selectedTags)

Correct:

EntriesGridView(selectedTag: $viewModel.selectedTags, entries: viewModel.entries, tags: viewModel.tags)

Just a comment: You can remove .onAppear { because @StateObject inits the object at on appear anyway and you have the same actions inside your init.

Accepted Answer

Update: Solved. Turns out that the problem occurs because EntriesGridView initialized in the wrong order.

Wrong:

EntriesGridView(entries: viewModel.entries, tags: viewModel.tags, selectedTag: $viewModel.selectedTags)

Correct:

EntriesGridView(selectedTag: $viewModel.selectedTags, entries: viewModel.entries, tags: viewModel.tags)
Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)
 
 
Q