Skip to content

Runtime crash (BAD_ACCESS) when iterating over a generic pack (tuple) and trying to access a property #78598

@lukaskollmer

Description

@lukaskollmer

Description

Some remarks on the program below:

  • this compiles without issues for both debug and release builds
  • it crashes at runtime, with a BAD_ACCESS in the isEmpty variable access in hmmm, for both debug and release builds
  • changing isEmpty from a computed property into e.g. let isEmpty = true will also result in a runtime crash, albeit with a different stack trace (see below)
  • removing the break statement from the for _ in repeat each loop in the initialiser makes everything work fine
  • removing the break statement from the for loop in hmmm still causes it to crash, but at a different place and with a different backtrace
  • turning Entry into a struct makes everything work fine.

Reproduction

final class Entry<Results> {
    var isEmpty: Bool { true }
}


struct Foo<each Results> {
    private let entry: (repeat Entry<each Results>)
    
    init(_ entry: (repeat Entry<each Results>)) {
        self.entry = entry
        for entry in repeat each entry {
            if entry.isEmpty {
                break
            }
        }
    }
    
    var hmmm: Void {
        for entry in repeat each entry {
            if !entry.isEmpty { // <-- EXC_BAD_ACCESS
                break
            }
        }
    }
}


_ = Foo((
    Entry<Any>()
)).hmmm

Stack dump

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xbca019aec150)
    frame #0: 0x00000001000017cc test_2025-01-12`Entry.isEmpty.getter() at <stdin>:0
  * frame #1: 0x0000000100001704 test_2025-01-12`Foo.hmmm.getter() at main.swift:54:23
    frame #2: 0x0000000100000ce8 test_2025-01-12`main at main.swift:65:4
    frame #3: 0x00000001913df154 dyld`start + 2476

Expected behavior

I see no reason why this should not work.

Environment

swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx14.0

Additional information

This is the backtrace of the crash of the alternative version (when isEmpty is let isEmpty = true):

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x44b5779b4ff0)
    frame #0: 0x00000001a17ea188 libswiftCore.dylib`_swift_release_dealloc + 32
    frame #1: 0x00000001a17eacf8 libswiftCore.dylib`bool swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>>::doDecrementSlow<(swift::PerformDeinit)1>(swift::RefCountBitsT<(swift::RefCountInlinedness)1>, unsigned int) + 136
    frame #2: 0x0000000100002c78 test_2025-01-12`outlined destroy of Foo<Pack{Any}> at <compiler-generated>:0
  * frame #3: 0x0000000100002384 test_2025-01-12`main at main.swift:39:4
    frame #4: 0x00000001913df154 dyld`start + 2476

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.crashBug: A crash, i.e., an abnormal termination of softwareparameter packsFeature → generics: Parameter packs

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions