Swift: Enums with stored properties not thread safe even when mutated using a serial queue

Originator:parthv21
Number:rdar://FB9083377 Date Originated:04/19/2021
Status:Open Resolved:
Product:Swift Playgrounds Product Version:
Classification: Reproducible:Yes
 
This issue relates to another issue reported on the Apple Developer forum titled [How to avoid Data Races in deinit](https://developer.apple.com/forums/thread/49930) from 4 years ago. I am seeing crashes in production related to associated data and `outlined consume` code added by the compiler to deinitialize enums with associated data that contain reference types.

When trying to find if someone else has faced the same issue, I stumbled across the above post. I reduced the example from that post to a simpler example below and reliably get a crash due to bad access.


Run in playground:

```
import Foundation
class Racer {
    let queue = DispatchQueue(label: "Racer-\(UUID())")
    var value: String? = "" // Desugars to Optional<String>, an enum
    func race() {
        queue.async {[weak self] in
            self?.value = UUID().uuidString
        }
    }
}
let racer = Racer()
while true {
    racer.race()
}
```

> error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x357d20cc94c0).

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!