Skip to content

Instantly share code, notes, and snippets.

@Verdagon
Created November 29, 2025 02:28
Show Gist options
  • Select an option

  • Save Verdagon/257dde6f6ddc12d23dd784e6f0fdf4c7 to your computer and use it in GitHub Desktop.

Select an option

Save Verdagon/257dde6f6ddc12d23dd784e6f0fdf4c7 to your computer and use it in GitHub Desktop.
struct Navigation: ~Copyable {
var antenna: Bool
}
struct Engine: ~Copyable {
var fuel: Int
}
enum Variant: ~Copyable {
case navigation(Navigation)
case engine(Engine)
}
class Spaceship {
var v: Variant
init(_ v: consuming Variant) {
self.v = v
}
}
func changeAndPrintShip(_ shipA: Spaceship, _ shipB: Spaceship) {
print("In changeAndPrintShip...")
switch shipA.v {
case .navigation(let data):
break
case .engine(let data):
print("In engine case, borrowed the inner Engine...")
shipB.v = Variant.navigation(Navigation(antenna: true))
print("Dereferencing the borrow...")
let fuel = data.fuel
print("ship.engine.fuel = \(fuel)")
}
}
func main() {
print("Starting up!")
let ship = Spaceship(Variant.engine(Engine(fuel: 42)))
changeAndPrintShip(ship, ship)
}
main()
// Output:
//
// Starting up!
// In changeAndPrintShip...
// In engine case, borrowed the inner Engine...
// Simultaneous accesses to 0x16b2d3120, but modification requires exclusive access.
// Previous access (a read) started at SwiftUB`changeAndPrintShip(_:_:) + 172 (0x104b2f3f0).
// Current access (a modification) started at:
// 0 libswiftCore.dylib 0x00000001a562cdf0 swift::runtime::AccessSet::insert(swift::runtime::Access*, void*, void*, swift::ExclusivityFlags) + 432
// 1 libswiftCore.dylib 0x00000001a562d008 swift_beginAccess + 84
// 2 SwiftUB 0x0000000104b2f344 changeAndPrintShip(_:_:) + 296
// 3 SwiftUB 0x0000000104b2f1f4 SwiftUB_main + 192
// 4 dyld 0x00000001952417a8 start + 2360
// Fatal access conflict detected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment