Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions stdlib/public/core/DebuggerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,22 @@ public enum _DebuggerSupport {

print(String(repeating: " ", count: indent), terminator: "", to: &target)

// do not expand classes with no custom Mirror
// yes, a type can lie and say it's a class when it's not since we only
// check the displayStyle - but then the type would have a custom Mirror
// anyway, so there's that...
// 1. Do not expand classes, unless they conform to CustomReflectable.
// 2. Do not expand value types that conform to CustomStringConvertible
// or CustomDebugStringConvertible, unless the type also conform to
// CustomReflectable.
let isNonClass = mirror.displayStyle != .`class`
let isStringConvertible: Bool
let isCustomReflectable: Bool
if let value = value {
isCustomReflectable = value is CustomReflectable
isStringConvertible =
value is CustomStringConvertible || value is CustomDebugStringConvertible
} else {
isCustomReflectable = true
isStringConvertible = false
}
let willExpand = isNonClass || isCustomReflectable
let willExpand = (isNonClass && !isStringConvertible) || isCustomReflectable

let count = mirror.children.count
let bullet = isRoot && (count == 0 || !willExpand) ? ""
Expand Down
10 changes: 10 additions & 0 deletions test/stdlib/DebuggerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ struct StructWithMembers {
var b = "Hello World"
}

struct StructWithMembersAndDescription: CustomStringConvertible {
var a = 1
var description: String { "Hello World" }
}

class ClassWithMembers {
var a = 1
var b = "Hello World"
Expand Down Expand Up @@ -61,6 +66,11 @@ StringForPrintObjectTests.test("StructWithMembers") {
expectEqual(printed, "▿ StructWithMembers\n - a : 1\n - b : \"Hello World\"\n")
}

StringForPrintObjectTests.test("StructWithMembersAndDescription") {
let printed = _stringForPrintObject(StructWithMembersAndDescription())
expectEqual(printed, "Hello World\n")
}

#if _runtime(_ObjC)
StringForPrintObjectTests.test("ClassWithMembers") {
let printed = _stringForPrintObject(ClassWithMembers())
Expand Down