"Couldn't realize type of self" error when debugging methods defined in protocol extension

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.

1 Like

It's embrassing...I find a clue shortly after I posted the question. I always use po command in debugging. It doesn't work in this case. But the p command works fine.

(lldb) po type(of: self)
test1.Struct

(lldb) po self
expression produced error: error: Couldn't realize type of self. Try evaluating the expression with -d run-target

(lldb) po self as! Struct
expression produced error: error: Couldn't realize type of self. Try evaluating the expression with -d run-target

(lldb) p self
(test1.Struct) $R15 = (x = 1)

(lldb) p self.x
(Int) $R16 = 1
1 Like

Any idea why this might be the case (I’m an lldb noob)

I don't know much about lldb either. The issue I described seems to have been fixed. You may find this link helpful with regarding to po vs p vs v commands.

I think the issue is back in Xcode 13.3 (all betas & RC1 & Final, Xcode 13.2, 13.2.1 works properly).

I'll be trying to isolate the project but it seems to be quite complicated.

You may find @eskimo's explanation in this thread helpful.

Having the same issue on Xcode 13.2, the only way it worked for me was "Print description of self" in gui