Skip to content

Runtime crash in AnyKeyPath.debugDescription when using @dynamicMemberLookup #64865

Closed
@mbrandonw

Description

@mbrandonw

Description

The following code defines a wrapper struct around a value and uses @dynamicMemberLookup to expose access to the underlying value:

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

In Swift 5.8, if we try to access a key path's debugDescription on Wrapper that goes through the dynamic member, we get a runtime crash:

let kp = \Wrapper<String>.count
print(kp.debugDescription)  // 🛑 Bad access

Steps to reproduce

Run the above code in any Swift environment.

Expected behavior

It should print out something like "\Wrapper<String>.count".

Environment

  • swift-driver version: 1.75.2 Apple Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100)
  • Xcode 14.3, Build version 14E222b
  • Deployment target: iOS 16.4

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 itselfcrashBug: A crash, i.e., an abnormal termination of softwaredeclarationsFeature: 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.9

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions