Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lukaskollmer opened this issue Jan 12, 2025 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels

Comments

@lukaskollmer
Copy link

lukaskollmer commented Jan 12, 2025

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
@lukaskollmer lukaskollmer added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant