Crash creating key path to variadic property

The following code consistently crashes for me when creating the key path on both 6.1 and the 6.2 betas:

struct Foo<each T> {
  
  struct Bar {
    let bar: (repeat each T)
  }
  
  func bar() {
    // Crashes here
    let keyPath = \Bar.bar
    print("Bar")
  }
}

Foo<Int, Int, Int>().bar()
print("Done")

Error is:

⭐️ swift run                           
Building for debugging...
/Users/george/code/Projects/Test/Sources/Test/Test.swift:12:9: warning: initialization of immutable value 'keyPath' was never used; consider replacing with assignment to '_' or removing it [#no-usage]
10 |   
11 |   func bar() {
12 |     let keyPath = \Bar.bar
   |         `- warning: initialization of immutable value 'keyPath' was never used; consider replacing with assignment to '_' or removing it [#no-usage]
13 |     print("Bar")
14 |   }
[8/8] Applying Test
Build of product 'Test' complete! (1.04s)

💣 Program crashed: Bad pointer dereference at 0xfffffffffffffffa

Thread 0 crashed:

 0 0x00000001982584b4 (anonymous namespace)::DecodedMetadataBuilder::beginPackExpansion(swift::MetadataPackOrValue) + 212 in libswiftCore.dylib
 1 0x000000019824d9e8 swift::Demangle::__runtime::TypeDecoder<(anonymous namespace)::DecodedMetadataBuilder>::decodeMangledType(swift::Demangle::__runtime::Node*, unsigned int, bool) + 5092 in libswiftCore.dylib
 2 0x00000001982522c8 swift::Demangle::__runtime::TypeDecoder<(anonymous namespace)::DecodedMetadataBuilder>::decodeGenericArgs(swift::Demangle::__runtime::Node*, unsigned int, __swift::__runtime::llvm::SmallVectorImpl<swift::MetadataPackOrValue>&) + 340 in libswiftCore.dylib
 3 0x000000019824c964 swift::Demangle::__runtime::TypeDecoder<(anonymous namespace)::DecodedMetadataBuilder>::decodeMangledType(swift::Demangle::__runtime::Node*, unsigned int, bool) + 864 in libswiftCore.dylib
 4 0x0000000198249a28 swift_getTypeByMangledNodeImpl(swift::MetadataRequest, swift::Demangle::__runtime::Demangler&, swift::Demangle::__runtime::Node*, void const* const*, std::__1::function<void const* (unsigned int, unsigned int)>, std::__1::function<swift::TargetWitnessTable<swift::InProcess> const* (swift::TargetMetadata<swift::InProcess> const*, unsigned int)>) + 864 in libswiftCore.dylib
 5 0x0000000198249f14 swift_getTypeByMangledNodeSlow(swift::MetadataRequest, swift::Demangle::__runtime::Demangler&, swift::Demangle::__runtime::Node*, void const* const*, std::__1::function<void const* (unsigned int, unsigned int)>, std::__1::function<swift::TargetWitnessTable<swift::InProcess> const* (swift::TargetMetadata<swift::InProcess> const*, unsigned int)>, std::__1::atomic<unsigned long>&, unsigned long, swift::TypeLookupErrorOr<swift::TypeInfo> (swift::MetadataRequest, swift::Demangle::__runtime::Demangler&, swift::Demangle::__runtime::Node*, void const* const*, std::__1::function<void const* (unsigned int, unsigned int)>, std::__1::function<swift::TargetWitnessTable<swift::InProcess> const* (swift::TargetMetadata<swift::InProcess> const*, unsigned int)>) swiftcall*) + 820 in libswiftCore.dylib
 6 0x00000001982458b4 swift_getTypeByMangledNode + 720 in libswiftCore.dylib
 7 0x000000019824a464 swift_getTypeByMangledNameImpl(swift::MetadataRequest, __swift::__runtime::llvm::StringRef, void const* const*, std::__1::function<void const* (unsigned int, unsigned int)>, std::__1::function<swift::TargetWitnessTable<swift::InProcess> const* (swift::TargetMetadata<swift::InProcess> const*, unsigned int)>) + 1188 in libswiftCore.dylib
 8 0x000000019824a928 swift_getTypeByMangledNameSlow(swift::MetadataRequest, __swift::__runtime::llvm::StringRef, void const* const*, std::__1::function<void const* (unsigned int, unsigned int)>, std::__1::function<swift::TargetWitnessTable<swift::InProcess> const* (swift::TargetMetadata<swift::InProcess> const*, unsigned int)>, std::__1::atomic<unsigned long>&, unsigned long, swift::TypeLookupErrorOr<swift::TypeInfo> (swift::MetadataRequest, __swift::__runtime::llvm::StringRef, void const* const*, std::__1::function<void const* (unsigned int, unsigned int)>, std::__1::function<swift::TargetWitnessTable<swift::InProcess> const* (swift::TargetMetadata<swift::InProcess> const*, unsigned int)>) swiftcall*) + 820 in libswiftCore.dylib
 9 0x00000001982435ac swift_getTypeByMangledName + 720 in libswiftCore.dylib
10 0x000000019824311c swift_getTypeByMangledNameInEnvironment + 180 in libswiftCore.dylib
11 0x0000000197fd831c _resolveKeyPathGenericArgReference(_:genericEnvironment:arguments:) + 272 in libswiftCore.dylib
12 0x0000000197fd89cc specialized _walkKeyPathPattern<A>(_:walker:) + 124 in libswiftCore.dylib
13 0x0000000197fd7e30 _getKeyPathClassAndInstanceSizeFromPattern(_:_:) + 72 in libswiftCore.dylib
14 0x0000000197fd7c04 _swift_getKeyPath(pattern:arguments:) + 128 in libswiftCore.dylib
15 Foo.bar() + 60 in Test at /Users/george/code/Projects/Test/Sources/Test/Test.swift:12:19

    10│   
    11│   func bar() {
    12│     let keyPath = \Bar.bar                                            
      │                   ▲
    13│     print("Bar")
    14│   }

16 static Test.main() + 76 in Test at /Users/george/code/Projects/Test/Sources/Test/Test.swift:22:28

    20│       
    21│       
    22│       Foo<Int, Int, Int>().bar()                                      
      │                            ▲
    23│       print("Done")
    24│     }

Backtrace took 0.11s

zsh: segmentation fault  swift run
🔸 139                                                                          

Has anyone run into this type of thing, have any workarounds to suggest? Right now I'm just planning to use a closure instead of a key path.

1 Like

AFAIK key paths have never worked on variadic generics.

4 Likes