Failed to produce diagnostic for expression; please file a bug report

Help me, I am writing a login function and putting login in button action but this error Failed to produce diagnostic for expression; please file a bug report.



import SwiftUI

import Alamofire



struct loginPage: View {

    @State private var email : String = ""

    @State private var isEmailValid : Bool   = false

    @State private var password : String = ""

    @State var hiddenpassword : Bool = false

    

    var body: some View {

        NavigationView {

            ZStack {

                VStack {

                    Spacer()

                    Spacer()

                    Text("Welcome").font(/*@START
MENUTOKEN@*/.title/*@ENDMENUTOKEN@*/)

                    HStack{

                        Image(systemName: "envelope.fill")

                        TextField("Email", text: self.$email , onEditingChanged:{ (isChanged) in

                            if !isChanged {

                                if self.textFieldValidatorEmail(self.email) {

                                    self.isEmailValid = true}

                            } else {

                                    self.isEmailValid = false

                                    self.email = ""

                            }

                            })//.foregroundColor(.yellowm)

                        if self.isEmailValid == true {

                            

                            Image(systemName: "checkmark")

                                        .font(.callout)

                                        .foregroundColor(Color.red)

                                }

                    }.padding()

                    HStack{

                        Image(systemName: "lock.fill")

                        if self.hidden
password{

                            TextField("Password", text: self.$password )

                        }else{

                            SecureField("Password",text: self.$password)

                        }

                        Button(action: {self.hiddenpassword.toggle()})

                        {

                            Image(systemName: self.hidden
password ? "eye.fill" : "eye.slash.fill").foregroundColor(.gray)

                        }



                    }.padding()

                    HStack() {

                        Spacer()

                        NavigationLink(destination: forgotPasswordPage() ) {

                            Text("Forgot Password?").font(.system(size: 15))

                                .foregroundColor(.yellow)

                                .padding()

                        }

                    }.padding(.bottom, 0)

                    

                    Button(action: self.login(email: $email, password: $password)) {

                        HStack{

                            Text("Log in")

                                .font(.headline)

                                .foregroundColor(.white)

                                .frame(width: 371,height: 40)

                                .background(Color.yellow)

                        }.cornerRadius(4.0)

                    }

                    Spacer()

                    

                }.background(Color.white)

                

            }.background(Color.black)

            .edgesIgnoringSafeArea(/*@STARTMENUTOKEN@*/.all/*@ENDMENUTOKEN@*/)

        }

        

    }

    

    func textFieldValidatorEmail( string: String) -> Bool {

        if string.count > 100 {

            return false

        }

       let emailFormat = "[A-Z0-9a-z.
%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z.]{2,64}"

       let emailPredicate = NSPredicate(format:"SELF MATCHES %@", emailFormat)

        return emailPredicate.evaluate(with: string)

    }

    

    

    func login (email : String , password : String) {

        let url = "https://test"

        AF.request(url,

                          method: .post,

                          parameters : [

                            "action" : "auth",

                            "email" : email,

                            "password" : password]).responseJSON { (response) in

                                switch response.result {

                                case .success(let value) :

                                    print(value)

                                case .failure(let error):

                                            print(error)

                                }

                            }

    }

    

    

}
When you ask something about errors, please specify the line the error occurs.

I guess the error is shown on this line: var body: some View {.
Unfortunately, the error Failed to produce diagnostic for expression is stating that Swift compiler found some errors somewhere but cannot report the right position and the right reason.
This seems to be a severe flaw as a practical compiler and you should better send a bug report as suggested.

You may need to comment out part by part and find which part of your code is causing errors.
As far as I tried, if I comment out this part, the code compiles:
Code Block
Button(action: self.login(email: $email, password: $password)) {
HStack {
Text("Log in")
.font(.headline)
.foregroundColor(.white)
.frame(width: 371,height: 40)
.background(Color.yellow)
}.cornerRadius(4.0)
}



The part shown above has several errors:
  • The action parameter of Button.init(action:label:) needs to be a closure

You are passing the result of login(email:password:), which is declared as not returning values. It is not a closure.
  • The parameters email and password needs to be String

You are passing $email and $password, the type of $ prefixed names are Binding<String> here. Not String.

You may need to fix two things as follows:
Code Block
Button(action: {self.login(email: email, password: password)}) {//<-
HStack {
Text("Log in")
.font(.headline)
.foregroundColor(.white)
.frame(width: 371,height: 40)
.background(Color.yellow)
}.cornerRadius(4.0)
}

(Please do not miss the enclosing braces passed to action, which creates a closure.)

I needed to guess many parts as may underscores (_) are missing and I have never used Alamofire things in my apps.
So, there may be more parts you need to fix. But I believe Swift compiler would generate better diagnostics once you fix the part shown above.


Some other things:
  • Please use Code block feature of this site. Shown with an icon like < >.

  • In Swift, type names should start with Capital letter, loginPage and forgotPasswordPage should be LoginPage and ForgotPasswordPage.

  • In Swift, you usually do not use snake-case identifiers like hidden_password, it should be hiddenPassword.

  • Using NSPredicate is not a good way just for validating a String. You can use regex based comparison or NSRegularExpression.

Failed to produce diagnostic for expression; please file a bug report
 
 
Q