Skip to content

Commit 96faeb5

Browse files
authored
[Debug] Accept _debugDescription in DebugDescriptionMacro (#70728)
Some data types cannot modify their `description` or `debugDescription` properties, as modification to those properties could result in breakage to their users. To support these conditions, `DebugDescriptionMacro` should support and prioritize an independent property (one that that is not being reused). This change adds support for `_debugDescription`. The macro will now prioritize the description properties in this order: 1. `_debugDescription` 2. `debugDescription` 3. `debugDescription` rdar://120498021
1 parent dfe1e69 commit 96faeb5

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

lib/Macros/Sources/SwiftMacros/DebugDescriptionMacro.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension DebugDescriptionMacro: MemberAttributeMacro {
4747
return []
4848
}
4949

50-
guard propertyName == "debugDescription" || propertyName == "description" else {
50+
guard DESCRIPTION_PROPERTIES.contains(propertyName) else {
5151
return []
5252
}
5353

@@ -72,8 +72,8 @@ extension DebugDescriptionMacro: MemberAttributeMacro {
7272
}
7373
}
7474

75-
// `debugDescription` takes priority: skip `description` if `debugDescription` also exists.
76-
if propertyName == "description" && properties["debugDescription"] != nil {
75+
// Skip if this description property is not prioritized.
76+
guard propertyName == designatedProperty(properties) else {
7777
return []
7878
}
7979

@@ -239,6 +239,23 @@ extension _DebugDescriptionPropertyMacro: PeerMacro {
239239
}
240240
}
241241

242+
/// The names of properties that can be converted to LLDB type summaries, in priority order.
243+
fileprivate let DESCRIPTION_PROPERTIES = [
244+
"_debugDescription",
245+
"debugDescription",
246+
"description",
247+
]
248+
249+
/// Identifies the prioritized description property, of available properties.
250+
fileprivate func designatedProperty(_ properties: [String: PatternBindingSyntax]) -> String? {
251+
for name in DESCRIPTION_PROPERTIES {
252+
if properties[name] != nil {
253+
return name
254+
}
255+
}
256+
return nil
257+
}
258+
242259
// MARK: - Encoding
243260

244261
fileprivate let ENCODING_VERSION: UInt = 1

test/Macros/DebugDescription/either_description.swift renamed to test/Macros/DebugDescription/supported_description.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,24 @@ struct MyStruct1: CustomStringConvertible {
1717

1818
@_DebugDescription
1919
struct MyStruct2: CustomDebugStringConvertible {
20-
var debugDescription: String { "thirty" }
20+
var description: String { "thirty" }
21+
var debugDescription: String { "eleven" }
2122
}
2223
// CHECK: static let _lldb_summary = (
2324
// CHECK: /* version */ 1 as UInt8,
2425
// CHECK: /* record size */ 24 as UInt8,
2526
// CHECK: /* "main.MyStruct2" */ 15 as UInt8, 109 as UInt8, 97 as UInt8, 105 as UInt8, 110 as UInt8, 46 as UInt8, 77 as UInt8, 121 as UInt8, 83 as UInt8, 116 as UInt8, 114 as UInt8, 117 as UInt8, 99 as UInt8, 116 as UInt8, 50 as UInt8, 0 as UInt8,
26-
// CHECK: /* "thirty" */ 7 as UInt8, 116 as UInt8, 104 as UInt8, 105 as UInt8, 114 as UInt8, 116 as UInt8, 121 as UInt8, 0 as UInt8
27+
// CHECK: /* "eleven" */ 7 as UInt8, 101 as UInt8, 108 as UInt8, 101 as UInt8, 118 as UInt8, 101 as UInt8, 110 as UInt8, 0 as UInt8
2728
// CHECK: )
2829

30+
@_DebugDescription
31+
struct MyStruct3: CustomDebugStringConvertible {
32+
var description: String { "thirty" }
33+
var debugDescription: String { "eleven" }
34+
var _debugDescription: String { "two" }
35+
}
36+
// CHECK: static let _lldb_summary = (
37+
// CHECK: /* version */ 1 as UInt8,
38+
// CHECK: /* record size */ 21 as UInt8,
39+
// CHECK: /* "main.MyStruct3" */ 15 as UInt8, 109 as UInt8, 97 as UInt8, 105 as UInt8, 110 as UInt8, 46 as UInt8, 77 as UInt8, 121 as UInt8, 83 as UInt8, 116 as UInt8, 114 as UInt8, 117 as UInt8, 99 as UInt8, 116 as UInt8, 51 as UInt8, 0 as UInt8,
40+
// CHECK: /* "two" */ 4 as UInt8, 116 as UInt8, 119 as UInt8, 111 as UInt8, 0 as UInt8

0 commit comments

Comments
 (0)