The following simple function will cause Xcode 12E262 to have "Abort: trap 6"

The following simple function will cause Xcode 12E262 to have "Abort: trap 6" during compilation.

import UIKit
import CoreData


class ViewController: UIViewController {

    func xyz() {
        let container = NSPersistentContainer(name: "xyz")

        let batchUpdateRequest = NSBatchUpdateRequest(entityName: "xyz")

        let batchUpdateResult = try! container.viewContext.execute(batchUpdateRequest) as? NSBatchUpdateResult

        guard let batchUpdateResult = batchUpdateResult else { return }

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

We will not observe "Abort: trap 6", if under Build Settings, we are using "Optimize for Speed" in Debug, instead of "No Optimization"

We can also avoid "Abort: trap 6", if we change the following code

guard let batchUpdateResult = batchUpdateResult else { return }

to

guard let batchUpdateResult2 = batchUpdateResult else { return }

May I know, why is it so?

A simpler code example to reproduce problem, without CoreData would be

import UIKit

class ViewController: UIViewController {

    func getAny() throws -> Any? {
        return nil
    }

    func xyz() {
        let name = try! getAny() as? UIViewController
        guard let name = name else { return }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}
Accepted Answer

Thanks for sharing the info. I can reproduce the same issue with your simpler code example.

May I know, why is it so?

In older versions of Swift compilers, declaring exactly the same identifier in the same code block caused an error, even when one is declared with guard-let. I'm not sure why this is happening, but compilers should not stop with Abort Trap and you can send a bug report.

(Showed Definition conflicts with previous value in Xcode 10.1. It is different than shadowing identifiers declared in outer scopes.)

And in Xcode 13 beta, your simpler code compiles without errors. I have not tried with Xcode 12.5.1, have you tried?

It seems that this issue is resolved in Xcode 13 beta - https://bugs.swift.org/browse/SR-14715

The following simple function will cause Xcode 12E262 to have "Abort: trap 6"
 
 
Q