20

Given the following Swift code, saved in bug.swift, and using Xcode 8.2.1 or Xcode 8.3 beta 2:

import Foundation

protocol MyProtocol {
    func foo() -> String
}

extension MyProtocol {
    func foo() -> String {
        return "\(self)"
    }
}

extension String: MyProtocol {}
extension NSAttributedString: MyProtocol {}

let normal = "normal".foo()
let attributed = NSAttributedString(string: "attributed", attributes: [:]).foo()

Run the following commands:

swiftc -g bug.swift
lldb bug

LLDB launches. Now, run these commands, and observe the output. Where I pass 9, pass the line in your bug.swift that contains return "\(self)":

(lldb) target create "bug"
Current executable set to 'bug' (x86_64).
(lldb) b 9
Breakpoint 1: where = bug`(extension in bug):bug.MyProtocol.foo () -> Swift.String + 19 at bug.swift:9, address = 0x0000000100001e53
(lldb) run
Process 16370 launched: '/Users/zev/Desktop/bug' (x86_64)
Process 16370 stopped
* thread #1: tid = 0x31730e, 0x0000000100001e53 bug`MyProtocol.foo(self="normal") -> String + 19 at bug.swift:9, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100001e53 bug`MyProtocol.foo(self="normal") -> String + 19 at bug.swift:9
   6    
   7    extension MyProtocol {
   8        func foo() -> String {
-> 9            return "\(self)"
   10       }
   11   }
   12   
(lldb) po self
"normal"


(lldb) c
Process 16370 resuming
Process 16370 stopped
* thread #1: tid = 0x31730e, 0x0000000100001e53 bug`MyProtocol.foo(self=0x00007fff5fbff480) -> String + 19 at bug.swift:9, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100001e53 bug`MyProtocol.foo(self=0x00007fff5fbff480) -> String + 19 at bug.swift:9
   6    
   7    extension MyProtocol {
   8        func foo() -> String {
-> 9            return "\(self)"
   10       }
   11   }
   12   
(lldb) po self
error: <EXPR>:1:11: error: use of undeclared type '$__lldb_context'
extension $__lldb_context {                            
          ^~~~~~~~~~~~~~~

error: <EXPR>:18:5: error: use of unresolved identifier '$__lldb_injected_self'
    $__lldb_injected_self.$__lldb_wrapped_expr_2(     
    ^~~~~~~~~~~~~~~~~~~~~

The first time we hit the breakpoint, we're in String's conformance to MyProtocol, and we can successfully po self.

However, the second time we hit the breakpoint, we're in NSAttributedString's conformance to MyProtocol, and LLDB prints gibberish instead of the expected output from po self.

Why does LLDB sometimes fail to print anything useful? This is a contrived example, but I run into this all the time in my day-to-day use of LLDB.

Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
  • 2
    Filed a [Swift bug](https://bugs.swift.org/browse/SR-3886) at [Joe Groff's suggestion](https://twitter.com/jckarter/status/828979787150422018). – Zev Eisenberg Feb 07 '17 at 15:28
  • 1
    Clearly a bug. This is tricky because the self in a protocol has no static type. So the debugger can only print self if it can resolve the runtime type of the object. With objects crossing from ObjC->Swift that's not entirely trivial, as your example shows... – Jim Ingham Feb 07 '17 at 17:54
  • This bug has been resolved in some recent version of Swift – Zev Eisenberg Apr 28 '20 at 02:06

3 Answers3

1

After reopening Xcode and clearing the project the error disappeared.

Nike Kov
  • 12,630
  • 8
  • 75
  • 122
0

According to this answer on the Swift bug reporter, the bug was fixed at some point. I tested in Swift 5.2, and confirmed that it's fixed

Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
  • I was not able to reproduce the bug, following my exact steps from the original question. If you are, maybe you've found a different but related bug? If you can find good repro steps, you could post it to bugs.swift.org :) – Zev Eisenberg Dec 23 '20 at 02:28
  • It happened while debugging a watchOS app, the debugger is quite useless in that case lol – Alexandru Motoc Dec 23 '20 at 17:28
0

I have run into this issue:

error: <EXPR>:1:11: error: use of undeclared type '$__lldb_context'
extension $__lldb_context {

even without any compiler bugs.

Ensure you set your Xcode Command Line Tools to match the currently running version of Xcode, in your Xcode -> Preferences (CMD + ,).

Xcode CLI Tools

Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71