Impossible unit test crash with generic class in Swift package

Is something wrong with this code? I do not understand why Xcode is reporting the unit test crash.

class ArrayQueueTests : XCTestCase {
    func testSanity() {
        let q = ArrQue<Int>(elts: [1,2,3])
        print(q.count)  // crashes here
    }
}

The crash message is: Thread 1: EXC_BAD_ACCESS (code=2, address=0x7fff89cff44a)

In the Swift package:

public class ArrQue<T> {
    private var elts: [T]
    public init(elts: [T]) {
        self.elts = elts
    }
    public var count: Int {
        return elts.count
    }
}

I notice that the stack trace is also completely insane. It refers to another method in another class. Maybe the call to count was compiled to method call to another class.

I have the Swift Package open in Xcode 11.2.1. I'm running the test by clicking the arrow in the left margin next to the test method.

Xcode 11.2 has introduced deadly bugs in its management of Swift packages. I know two workarounds:

  • Switch to Xcode 11.3 beta
  • Run your tests from an Xcode project, and set DEAD_CODE_STRIPPING to NO in the build settings.

Related issues: https://bugs.swift.org/browse/SR-11564, https://bugs.swift.org/browse/SR-11643

3 Likes

Thanks! I'll 11.3 beta.

Terms of Service

Privacy Policy

Cookie Policy