diff --git a/stdlib/public/core/DebuggerSupport.swift b/stdlib/public/core/DebuggerSupport.swift index 93efe80075c8b..64a4618e1d5e8 100644 --- a/stdlib/public/core/DebuggerSupport.swift +++ b/stdlib/public/core/DebuggerSupport.swift @@ -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) ? "" diff --git a/test/stdlib/DebuggerSupport.swift b/test/stdlib/DebuggerSupport.swift index 8747680505ced..337bd27e1649b 100644 --- a/test/stdlib/DebuggerSupport.swift +++ b/test/stdlib/DebuggerSupport.swift @@ -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" @@ -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())