Hi, sorry if this sounds like a beginner quesiton, I googled but didn't find any relevant discussion. I'm working on a swift package and implement most methods in protocol extension. I find that I'm unable to debug these methods in Xcode. The symptom is that Xcode reports "Couldn't realize type of self" error. Below is an simple example to reproduce it.
(While I run into the error when working on a swift package, I find I can reproduce it consistently in other ways. For example, create a macOS command line app in Xcode, then paste the code below and run it.)
import Foundation
protocol Proto {
var x: Int { get set }
mutating func modifyX(x: Int)
}
extension Proto {
mutating func modifyX(x: Int) {
self.x = x // Set break point here. Debug error: Couldn't realize type of self
}
}
struct Struct: Proto {
var x: Int = 1
}
var s = Struct()
s.modifyX(x: 10)
If I move the modifyX()
defintion from protocol extension to struct, however, the error is gone.
import Foundation
protocol Proto {
var x: Int { get set }
mutating func modifyX(x: Int)
}
struct Struct: Proto {
var x: Int = 1
mutating func modifyX(x: Int) {
self.x = x // Set break point here. It works.
}
}
var s = Struct()
s.modifyX(x: 10)
I find a dicussion about the error on SO. The top rated answers were about debug mode and optimization option setting. I don't think that's relevant in my case because 1) as explained above, it works fine if I moves the method defintion to struct, and 2) I check Xcode build log and see the swiftc command uses options like -Onone
and `-DDEBUG'.
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -incremental -module-name test1 -Onone ... -DDEBUG ... -DDEBUG\=1 ...
My environment: Xcode 12.5.1, Swift 5.4.2.
$ xcrun swift -version
Apple Swift version 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57)
Target: x86_64-apple-darwin20.5.0
Did anyone observe this issue too and how did you solve it? Thanks.