From 5182948ae88402f5d21616545c9ed367e32d8469 Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Fri, 3 Oct 2025 14:21:57 -0700 Subject: [PATCH 1/2] [Debug] Print only description of value types that conform to CustomStringConvertible --- stdlib/public/core/DebuggerSupport.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/stdlib/public/core/DebuggerSupport.swift b/stdlib/public/core/DebuggerSupport.swift index 93efe80075c8b..d6e5088f790da 100644 --- a/stdlib/public/core/DebuggerSupport.swift +++ b/stdlib/public/core/DebuggerSupport.swift @@ -214,18 +214,18 @@ 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... - let isNonClass = mirror.displayStyle != .`class` + // Do not expand types that conform to Custom(Debug)StringConvertible, + // unless the type also conform to CustomReflectable. + let isCustomStringConvertible = false let isCustomReflectable: Bool if let value = value { isCustomReflectable = value is CustomReflectable + isCustomStringConvertible = + value is CustomStringConvertible || value is CustomDebugStringConvertible } else { isCustomReflectable = true } - let willExpand = isNonClass || isCustomReflectable + let willExpand = isCustomReflectable || !isCustomStringConvertible let count = mirror.children.count let bullet = isRoot && (count == 0 || !willExpand) ? "" From 1077d42f620cf4d51314be447fd5864ad6d5d3aa Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Sat, 4 Oct 2025 13:48:39 -0700 Subject: [PATCH 2/2] Restore logic for classes, add tests --- stdlib/public/core/DebuggerSupport.swift | 14 +++++++++----- test/stdlib/DebuggerSupport.swift | 10 ++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/stdlib/public/core/DebuggerSupport.swift b/stdlib/public/core/DebuggerSupport.swift index d6e5088f790da..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 types that conform to Custom(Debug)StringConvertible, - // unless the type also conform to CustomReflectable. - let isCustomStringConvertible = false + // 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 - isCustomStringConvertible = + isStringConvertible = value is CustomStringConvertible || value is CustomDebugStringConvertible } else { isCustomReflectable = true + isStringConvertible = false } - let willExpand = isCustomReflectable || !isCustomStringConvertible + 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())