Skip to content

Improve debug description of key paths with generic subscripts #65996

Open
@mbrandonw

Description

@mbrandonw

Motivation

In Swift 5.8 there was a runtime crash in AnyKeyPath.debugDescription that was fixed in #63305. However, the fix did the bare minimum to not crash, but the description of the key path is not great:

struct Foo {
  subscript<A>(_ a: A) -> A { a }
}
print(\Foo.[1])
// "\Foo.<computed 0x0000000105129928 (Int)>"

Ideally it would be great if it could be printed like so:

print(\Foo.[1])
// "\Foo.[1]"

Further, if possible, it would also be really great if dynamic members could be given special treatment to be printed nicely:

@dynamicMemberLookup
struct Wrapper<Value> {
  let value: Value
  subscript<OtherValue>(dynamicMember keyPath: KeyPath<Value, OtherValue>) -> OtherValue {
    self.value[keyPath: keyPath]
  }
}

print(\Wrapper<String>.count)
// Should print "\Wrapper<String>.count"
// instead of "\Wrapper<String>.[dynamicMember: \String.count]"

Metadata

Metadata

Assignees

No one assigned

    Labels

    @dynamicMemberLookupFeature → attributes: the @dynamicMemberLookup attributeAnyKeyPathArea → standard library: The `AnyKeyPath` typeCustomDebugStringConvertibleArea → standard library: The `CustomDebugStringConvertible` protocolattributesFeature: Declaration and type attributesbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdeclarationsFeature: declarationsdemanglingArea → compiler: The `libswiftDemangling` librarygenericsFeature: generic declarations and typeskey pathsFeature: key paths (both native and Objective-C)standard libraryArea: Standard library umbrellasubscriptFeature: Subscript declarationsswift 5.9unexpected behaviorBug: Unexpected behavior or incorrect output

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions