Skip to content

Commit e54634c

Browse files
committed
Apply @DougGregor's feedback
1 parent 0f51f30 commit e54634c

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

lib/Macros/Sources/SwiftMacros/DebugDescriptionMacro.swift

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ extension _DebugDescriptionPropertyMacro: PeerMacro {
161161
// 2. The single item must be a return of a string literal
162162
// 3. Later on, the interpolation in the string literal will be validated.
163163
guard let codeBlock = onlyBinding.accessorBlock?.accessors.as(CodeBlockItemListSyntax.self),
164-
let descriptionString = codeBlock.onlyReturnExpr?.as(StringLiteralExprSyntax.self) else {
164+
let descriptionString = codeBlock.asSingleReturnExpr?.as(StringLiteralExprSyntax.self) else {
165165
let message: ErrorMessage = "body must consist of a single string literal"
166166
context.diagnose(node: declaration, error: message)
167167
return []
@@ -184,25 +184,12 @@ extension _DebugDescriptionPropertyMacro: PeerMacro {
184184
let expr = onlyLabeledExpr.expression
185185

186186
// "Parse" the expression into a flattened chain of property accesses.
187-
var propertyChain: [DeclReferenceExprSyntax] = []
188-
var invalidExpr: ExprSyntax? = nil
189-
if let declRef = expr.as(DeclReferenceExprSyntax.self) {
190-
// A reference to a single property on self.
191-
propertyChain.append(declRef)
192-
} else if let memberAccess = expr.as(MemberAccessExprSyntax.self) {
193-
do {
194-
propertyChain = try memberAccess.propertyChain()
195-
} catch let error as UnexpectedExpr {
196-
invalidExpr = error.expr
197-
}
198-
} else {
199-
// The expression was neither a DeclReference nor a MemberAccess.
200-
invalidExpr = expr
201-
}
202-
203-
if let invalidExpr {
187+
var propertyChain: [DeclReferenceExprSyntax]
188+
do {
189+
propertyChain = try expr.propertyChain()
190+
} catch let error as UnexpectedExpr {
204191
let message: ErrorMessage = "only references to stored properties are allowed"
205-
context.diagnose(node: invalidExpr, error: message)
192+
context.diagnose(node: error.expr, error: message)
206193
return []
207194
}
208195

@@ -397,10 +384,10 @@ extension PatternBindingSyntax {
397384
case nil:
398385
// No accessor block, not computed.
399386
return false
400-
case .accessors(let accessors)?:
387+
case .accessors(let accessors):
401388
// A `get` accessor indicates a computed property.
402389
return accessors.contains { $0.accessorSpecifier.tokenKind == .keyword(.get) }
403-
case .getter?:
390+
case .getter:
404391
// A property with an implementation block is a computed property.
405392
return true
406393
@unknown default:
@@ -411,7 +398,7 @@ extension PatternBindingSyntax {
411398

412399
extension CodeBlockItemListSyntax {
413400
/// The return statement or expression for a code block consisting of only a single item.
414-
fileprivate var onlyReturnExpr: ExprSyntax? {
401+
fileprivate var asSingleReturnExpr: ExprSyntax? {
415402
guard let item = self.only?.item else {
416403
return nil
417404
}
@@ -423,35 +410,48 @@ fileprivate struct UnexpectedExpr: Error {
423410
let expr: ExprSyntax
424411
}
425412

413+
extension ExprSyntax {
414+
/// Parse an expression consisting only of property references. Any other syntax throws an error.
415+
fileprivate func propertyChain() throws -> [DeclReferenceExprSyntax] {
416+
if let declRef = self.as(DeclReferenceExprSyntax.self) {
417+
// A reference to a single property on self.
418+
return [declRef]
419+
} else if let memberAccess = self.as(MemberAccessExprSyntax.self) {
420+
return try memberAccess.propertyChain()
421+
} else {
422+
// This expression is neither a DeclReference nor a MemberAccess.
423+
throw UnexpectedExpr(expr: self)
424+
}
425+
}
426+
}
427+
426428
extension MemberAccessExprSyntax {
427-
/// Parse a member access consisting only of property references. Any other syntax throws an error.
428429
fileprivate func propertyChain() throws -> [DeclReferenceExprSyntax] {
429430
// MemberAccess is left associative: a.b.c is ((a.b).c).
430431
var propertyChain: [DeclReferenceExprSyntax] = []
431432
var current = self
432-
while let base = current.base {
433+
while true {
434+
guard let base = current.base else {
435+
throw UnexpectedExpr(expr: ExprSyntax(current))
436+
}
437+
433438
propertyChain.append(current.declName)
439+
434440
if let declRef = base.as(DeclReferenceExprSyntax.self) {
435-
propertyChain.append(declRef)
436441
// Terminal case.
437-
break
438-
}
439-
if let next = base.as(MemberAccessExprSyntax.self) {
440-
current = next
442+
// Top-down traversal produces references in reverse order.
443+
propertyChain.append(declRef)
444+
propertyChain.reverse()
445+
return propertyChain
446+
} else if let next = base.as(MemberAccessExprSyntax.self) {
441447
// Recursive case.
448+
current = next
442449
continue
450+
} else {
451+
// The expression was neither a DeclReference nor a MemberAccess.
452+
throw UnexpectedExpr(expr: base)
443453
}
444-
// The expression was neither a DeclReference nor a MemberAccess.
445-
throw UnexpectedExpr(expr: base)
446-
}
447-
448-
guard current.base != nil else {
449-
throw UnexpectedExpr(expr: ExprSyntax(current))
450454
}
451-
452-
// Top-down traversal produces references in reverse order.
453-
propertyChain.reverse()
454-
return propertyChain
455455
}
456456
}
457457

stdlib/public/core/DebuggerSupport.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
import SwiftShims
1414

15-
#if $Macros && hasAttribute(attached)
16-
1715
/// Converts description definitions to a debugger type summary.
1816
///
1917
/// This macro converts compatible `debugDescription` (or `description`)
@@ -44,8 +42,6 @@ public macro _DebugDescription() =
4442
public macro _DebugDescriptionProperty(_ debugIdentifier: String, _ computedProperties: [String]) =
4543
#externalMacro(module: "SwiftMacros", type: "_DebugDescriptionPropertyMacro")
4644

47-
#endif
48-
4945
#if SWIFT_ENABLE_REFLECTION
5046

5147
@frozen // namespace

0 commit comments

Comments
 (0)