Files
swift-mirror/test/IRGen/unconditional_checked_cast_addr.sil
Saleem Abdulrasool 08a3d1862e SIL: add copying unconditional address casts
Add a [copy] attribute to unconditional_checked_cast_addr to preserve its
source and initialize an independently owned destination. Failure still
terminates execution through the runtime's cast diagnostic. This expresses
forced casts from borrowed storage without first copying the source or
introducing a conditional cast and a separate failure path.

Store the flag in SILNode's shared byte and preserve it through
serialization, cloning, and differentiation. Casts without [copy] retain
the existing consuming behavior.
2026-09-21 09:54:05 -07:00

30 lines
1.0 KiB
Plaintext

// RUN: %target-swift-frontend -emit-ir %s | %FileCheck %s
// RUN: %target-swift-frontend -O -emit-ir %s | %FileCheck %s
sil_stage canonical
import Swift
// Unconditional alone: preserve the source, and diagnose failure in the runtime.
// CHECK-LABEL: define {{.*}}@copy_source(
// CHECK: call {{.*}}@swift_dynamicCast({{.*}}i{{32|64}} 1)
// CHECK-NOT: @swift_dynamicCast
// CHECK: ret void
sil @copy_source : $@convention(thin) <T, U> (@in_guaranteed T) -> @out U {
bb0(%0 : $*U, %1 : $*T):
unconditional_checked_cast_addr [copy] T in %1 : $*T to U in %0 : $*U
%2 = tuple ()
return %2 : $()
}
// Unconditional | TakeOnSuccess | DestroyOnFailure: existing consuming behavior.
// CHECK-LABEL: define {{.*}}@take_source(
// CHECK: call {{.*}}@swift_dynamicCast({{.*}}i{{32|64}} 7)
// CHECK-NOT: @swift_dynamicCast
// CHECK: ret void
sil @take_source : $@convention(thin) <T, U> (@in T) -> @out U {
bb0(%0 : $*U, %1 : $*T):
unconditional_checked_cast_addr T in %1 : $*T to U in %0 : $*U
%2 = tuple ()
return %2 : $()
}