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

I cannot find a way to fix this error. I have seen posts about a similar problem but I cannot make any correlations.

I am trying to take the input from a textfield, putting it in an array and displaying it in a list.


struct GameView: View {
  @State var score = 0
  @State private var playerWord = ""
  @State private var usedwords = [String]()
   
   
  var body: some View {
     
    ZStack{
      LinearGradient(gradient: Gradient(colors: [Color.red, Color.orange]), startPoint: .top, endPoint: .bottom)
            .edgesIgnoringSafeArea(.vertical)
       
      VStack{
        HStack{
          Image("chain").scaleEffect(1.3)
          Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
          Image("chain").scaleEffect(1.3)
          Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
          Image("chain").scaleEffect(1.3)
        }
        HStack{
          Text("Score:")
          Text(String(score))
        }.padding()
         
         
        Spacer()
        Spacer()
         
        VStack {
 
          TextField("Enter a Word", text: $playerWord, onCommit: addNewWord()->Void)
            .padding()
            .border(Color.black, width: 2)
            .scaleEffect(0.8)
    
          List(usedwords, id: \.self){
            Text($0)
          }
           
        }
         
        Spacer()
         
        HStack{
          Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
          Image("chain").scaleEffect(1.3)
          Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
          Image("chain").scaleEffect(1.3)
          Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
        }
      }
    }
  }
  func addNewWord(){
    let answer =
      playerWord.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
     
    guard answer.count > 0 else {
      return
    }
     
    usedwords.insert(answer, at: 0)
    playerWord = ""
  }
}

struct SwiftUIView_Previews: PreviewProvider {
  static var previews: some View {
    GameView()
  }
}
Answered by OOPer in 681378022

The error Failed to produce diagnostic for expression is one of the big flaws in the current implementation of SwiftUI and Swift compiler. It just is indicating that there is some sort of errors in the block which is a very little clue for us developers.

You may need find where is the part causing the issue by splitting your code by commenting out some parts one by one. For example, the following compiles successfully:

struct GameView: View {
    @State var score = 0
    @State private var playerWord = ""
    @State private var usedwords = [String]()
    
    
    var body: some View {
        
        ZStack{
            LinearGradient(gradient: Gradient(colors: [Color.red, Color.orange]), startPoint: .top, endPoint: .bottom)
                .edgesIgnoringSafeArea(.vertical)
            
            VStack{
                HStack{
                    Image("chain").scaleEffect(1.3)
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                    Image("chain").scaleEffect(1.3)
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                    Image("chain").scaleEffect(1.3)
                }
                HStack{
                    Text("Score:")
                    Text(String(score))
                }.padding()
                
                Spacer()
                Spacer()
                
                VStack {
                    /*
                    TextField("Enter a Word", text: $playerWord, onCommit: addNewWord()->Void)
                        .padding()
                        .border(Color.black, width: 2)
                        .scaleEffect(0.8)
 */
                    List(usedwords, id: \.self){
                        Text($0)
                    }
                    
                }
                
                Spacer()
                
                HStack{
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                    Image("chain").scaleEffect(1.3)
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                    Image("chain").scaleEffect(1.3)
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                }
            }
        }
    }
    func addNewWord(){
        let answer =
            playerWord.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
        
        guard answer.count > 0 else {
            return
        }
        
        usedwords.insert(answer, at: 0)
        playerWord = ""
    }
}

With examining the code commented out carefully, it seems the parameter passed to onCommit: is invalid in Swift. It should be:

                    TextField("Enter a Word", text: $playerWord, onCommit: addNewWord) //<- No `()->Void` here
                        .padding()
                        .border(Color.black, width: 2)
                        .scaleEffect(0.8)
Accepted Answer

The error Failed to produce diagnostic for expression is one of the big flaws in the current implementation of SwiftUI and Swift compiler. It just is indicating that there is some sort of errors in the block which is a very little clue for us developers.

You may need find where is the part causing the issue by splitting your code by commenting out some parts one by one. For example, the following compiles successfully:

struct GameView: View {
    @State var score = 0
    @State private var playerWord = ""
    @State private var usedwords = [String]()
    
    
    var body: some View {
        
        ZStack{
            LinearGradient(gradient: Gradient(colors: [Color.red, Color.orange]), startPoint: .top, endPoint: .bottom)
                .edgesIgnoringSafeArea(.vertical)
            
            VStack{
                HStack{
                    Image("chain").scaleEffect(1.3)
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                    Image("chain").scaleEffect(1.3)
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                    Image("chain").scaleEffect(1.3)
                }
                HStack{
                    Text("Score:")
                    Text(String(score))
                }.padding()
                
                Spacer()
                Spacer()
                
                VStack {
                    /*
                    TextField("Enter a Word", text: $playerWord, onCommit: addNewWord()->Void)
                        .padding()
                        .border(Color.black, width: 2)
                        .scaleEffect(0.8)
 */
                    List(usedwords, id: \.self){
                        Text($0)
                    }
                    
                }
                
                Spacer()
                
                HStack{
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                    Image("chain").scaleEffect(1.3)
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                    Image("chain").scaleEffect(1.3)
                    Image("chain").scaleEffect(1.3).rotationEffect(Angle(degrees: 90))
                }
            }
        }
    }
    func addNewWord(){
        let answer =
            playerWord.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
        
        guard answer.count > 0 else {
            return
        }
        
        usedwords.insert(answer, at: 0)
        playerWord = ""
    }
}

With examining the code commented out carefully, it seems the parameter passed to onCommit: is invalid in Swift. It should be:

                    TextField("Enter a Word", text: $playerWord, onCommit: addNewWord) //<- No `()->Void` here
                        .padding()
                        .border(Color.black, width: 2)
                        .scaleEffect(0.8)
Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
 
 
Q