Skip to content

Instantly share code, notes, and snippets.

@Verdagon
Created December 15, 2025 17:39
Show Gist options
  • Select an option

  • Save Verdagon/5772534141c20ac36c67704b7de115b0 to your computer and use it in GitHub Desktop.

Select an option

Save Verdagon/5772534141c20ac36c67704b7de115b0 to your computer and use it in GitHub Desktop.
struct Navigation: ~Copyable {
var antenna: Bool
}
struct Engine: ~Copyable {
var warp_coils: [Int]
}
enum Variant: ~Copyable {
case navigation(Navigation)
case engine(Engine)
}
class Spaceship {
var v: Variant
init(_ v: consuming Variant) {
self.v = v
}
}
func maintenance(_ ship: Spaceship) {
ship.v = Variant.navigation(Navigation(antenna: true))
}
func foo(_ ship: Spaceship) {
switch ship.v { // Borrows ship.v since it's a noncopyable type
case .navigation(let navigation):
break
case .engine(let engine): // engine is a borrow reference
maintenance(ship)
// Theoretically we'd get the same error if we write to fuel here,
// though Swift doesn't have mutable references.
let warp_coils = engine.warp_coils
print("Warp coils:", warp_coils)
}
}
func main() {
let ship = Spaceship(Variant.engine(Engine(warp_coils: [42, 73])))
foo(ship)
}
main()
// verdagon@Evans-MBP swiftub % swift run -c release
// Building for production...
// [1/1] Write swift-version-24CDE65C6C26C763.txt
// Build of product 'SwiftUB' complete! (0.08s)
// Simultaneous accesses to 0x16f563110, but modification requires exclusive access.
// Previous access (a read) started at SwiftUB`foo(_:) + 44 (0x10089f458).
// Current access (a modification) started at:
// 0 libswiftCore.dylib 0x00000001aab0cdf0 swift::runtime::AccessSet::insert(swift::runtime::Access*, void*, void*, swift::ExclusivityFlags) + 432
// 1 libswiftCore.dylib 0x00000001aab0d008 swift_beginAccess + 84
// 2 SwiftUB 0x000000010089f42c foo(_:) + 84
// 3 SwiftUB 0x000000010089f30c SwiftUB_main + 104
// 4 dyld 0x000000019a7217a8 start + 2360
// Fatal access conflict detected.
// Previous version, which also crashes:
//
// 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)
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment