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

**URL:** <https://forums.swift.org/t/couldnt-realize-type-of-self-error-when-debugging-methods-defined-in-protocol-extension/50268>\
**Category:** Using Swift\
**Tags:** xcode, protocols\
**Created:** [July 7, 2021, 12:16pm UTC](https://forums.swift.org/t/couldnt-realize-type-of-self-error-when-debugging-methods-defined-in-protocol-extension/50268 "2021-07-07T12:16:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rayx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rayx/32/12204_2.png) [@rayx](https://forums.swift.org/u/rayx)\
**Post date:** [July 7, 2021, 12:16pm UTC](https://forums.swift.org/t/couldnt-realize-type-of-self-error-when-debugging-methods-defined-in-protocol-extension/50268/1 "2021-07-07T12:16:46Z")

</div>

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](https://stackoverflow.com/questions/19870176/xcode-debugger-doesnt-print-objects-and-shows-nil-when-they-arent) 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.

---

<div class="post-metadata">

**Author:** ![rayx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rayx/32/12204_2.png) [@rayx](https://forums.swift.org/u/rayx)\
**Post date:** [July 7, 2021, 12:36pm UTC](https://forums.swift.org/t/couldnt-realize-type-of-self-error-when-debugging-methods-defined-in-protocol-extension/50268/2 "2021-07-07T12:36:41Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![Lontronix](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lontronix/32/21811_2.png) [@Lontronix](https://forums.swift.org/u/Lontronix)\
**Post date:** [February 15, 2022, 6:43pm UTC](https://forums.swift.org/t/couldnt-realize-type-of-self-error-when-debugging-methods-defined-in-protocol-extension/50268/3 "2022-02-15T18:43:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rayx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rayx/32/12204_2.png) [@rayx](https://forums.swift.org/u/rayx)\
**Post date:** [February 16, 2022, 8:47am UTC](https://forums.swift.org/t/couldnt-realize-type-of-self-error-when-debugging-methods-defined-in-protocol-extension/50268/4 "2022-02-16T08:47:55Z")

</div>

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.

> **[LLDB: Beyond](https://developer.apple.com/videos/play/wwdc2019/429/?time=166)**
>
> LLDB is a powerful tool for exploring and debugging your app at runtime. Discover the various ways to display values in your app, how to...

---

<div class="post-metadata">

**Author:** ![Lukasz2891](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lukasz2891/32/22537_2.png) [@Lukasz2891](https://forums.swift.org/u/Lukasz2891)\
**Post date:** [March 29, 2022, 12:48pm UTC](https://forums.swift.org/t/couldnt-realize-type-of-self-error-when-debugging-methods-defined-in-protocol-extension/50268/5 "2022-03-29T12:48:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rayx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rayx/32/12204_2.png) [@rayx](https://forums.swift.org/u/rayx)\
**Post date:** [March 30, 2022, 4:55am UTC](https://forums.swift.org/t/couldnt-realize-type-of-self-error-when-debugging-methods-defined-in-protocol-extension/50268/6 "2022-03-30T04:55:04Z")

</div>

You may find @eskimo's explanation in this [thread](https://developer.apple.com/forums/thread/679879) helpful.

---

<div class="post-metadata">

**Author:** ![Mike](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mike/32/15284_2.png) [@Mike](https://forums.swift.org/u/Mike)\
**Post date:** [April 10, 2022, 7:29pm UTC](https://forums.swift.org/t/couldnt-realize-type-of-self-error-when-debugging-methods-defined-in-protocol-extension/50268/7 "2022-04-10T19:29:49Z")

</div>

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