Capturing self instead of using self. in switch case in DispatchQueue causes compiler error

I have an @objC used for notification.

kTag is an Int constant, fieldBeingEdited is an Int variable.

The following code fails at compilation with error: Command CompileSwift failed with a nonzero exit code if I capture self (I edited code, to have minimal case)

    @objc func keyboardDone(_ sender : UIButton) {
        
        DispatchQueue.main.async { [self] () -> Void in
            switch fieldBeingEdited {
                case kTag : break
                default : break
            }
        }
    }

If I explicitly use self, it compiles, even with self captured:

    @objc func keyboardDone(_ sender : UIButton) {
        
        DispatchQueue.main.async { [self] () -> Void in
            switch fieldBeingEdited {  // <<-- no need for self here
                case self.kTag : break  // <<-- self here
                default : break
            }
        }
    }

This compiles as well:

    @objc func keyboardDone(_ sender : UIButton) {
        
        DispatchQueue.main.async { () -> Void in
            switch self.fieldBeingEdited {  // <<-- no need for self here
                case self.kTag : break  // <<-- self here
                default : break
            }
        }
    }

Is it a compiler bug or am I missing something ?

Definitely a bug, since your code actually crashes the Swift REPL:

Welcome to Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1).
Type :help for assistance.
  1> import Foundation 
  2. class Claude { 
  3.     var fieldBeingEdited = 0 
  4.     let kTag = 0 
  5.      
  6.     @objc func keyboardDone(_ sender : NSObject) { 
  7.         DispatchQueue.main.async { [self] () -> Void in 
  8.             switch fieldBeingEdited { 
  9.             case kTag : break 
 10.             default : break 
 11.             } 
 12.         } 
 13.     } 
 14. }
LLDB diagnostics will be written to /var/folders/z3/53rv5j6x0697yjhjh890hnnr0000gr/T/diagnostics-18fbd0
Please include the directory content when filing a bug report
PLEASE submit a bug report to https://developer.apple.com/bug-reporting/ and include the crash backtrace.
[ ... stack trace omitted ... ]

If I remove the case kTag line, then it has no error.

Thanks Scott.

I filed a bug report: Mar 13, 2025 at 3:24 PM – FB16853197

I'll leave the thread open for a while, in case some compiler engineer notices it.

Capturing self instead of using self. in switch case in DispatchQueue causes compiler error
 
 
Q