Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions stdlib/public/runtime/BytecodeLayouts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1435,14 +1435,32 @@ static void existentialAssignWithCopy(const Metadata *metadata,
uint8_t *dest,
uint8_t *src) {
uintptr_t _addrOffset = addrOffset;
auto *type = getExistentialTypeMetadata((OpaqueValue*)(src + _addrOffset));
auto *srcType = getExistentialTypeMetadata((OpaqueValue*)(src + _addrOffset));
auto *destType = getExistentialTypeMetadata((OpaqueValue*)(dest + _addrOffset));
auto *destObject = (OpaqueValue *)(dest + _addrOffset);
auto *srcObject = (OpaqueValue *)(src + _addrOffset);
addrOffset = _addrOffset + (sizeof(uintptr_t) * NumWords_ValueBuffer);
if (type->getValueWitnesses()->isValueInline()) {
type->vw_assignWithCopy(destObject, srcObject);

if (srcType == destType) {
if (srcType->getValueWitnesses()->isValueInline()) {
srcType->vw_assignWithCopy(destObject, srcObject);
} else {
swift_release(*(HeapObject**)destObject);
memcpy(destObject, srcObject, sizeof(uintptr_t));
swift_retain(*(HeapObject**)srcObject);
}
return;
}

if (destType->getValueWitnesses()->isValueInline()) {
destType->vw_destroy(destObject);
} else {
swift_release(*(HeapObject**)destObject);
}

if (srcType->getValueWitnesses()->isValueInline()) {
srcType->vw_initializeWithCopy(destObject, srcObject);
} else {
memcpy(destObject, srcObject, sizeof(uintptr_t));
swift_retain(*(HeapObject**)srcObject);
}
Expand Down
47 changes: 47 additions & 0 deletions test/Interpreter/layout_string_witnesses_static.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ struct StructWithSomeProtocolInline: SomeProtocol {
let x: SimpleClass
}

struct StructWithSomeProtocolInlineShifted: SomeProtocol {
let y: Int = 0
let y2: Int = 0
let x: SimpleClass
}

func testExistentialStructInline() {
let ptr = UnsafeMutablePointer<ExistentialWrapper>.allocate(capacity: 1)

Expand Down Expand Up @@ -396,6 +402,47 @@ func testExistentialStructBox() {

testExistentialStructBox()

func testExistentialStructTypeChange() {
let ptr = UnsafeMutablePointer<ExistentialWrapper>.allocate(capacity: 1)

do {
let x = StructWithSomeProtocolInline(x: SimpleClass(x: 23))
testInit(ptr, to: createExistentialWrapper(x))
}

do {
let y = StructWithSomeProtocolInlineShifted(x: SimpleClass(x: 32))

// CHECK: Before deinit
print("Before deinit")

// CHECK-NEXT: SimpleClass deinitialized!
var wrapper = createExistentialWrapper(y)
testAssignCopy(ptr, from: &wrapper)
}

do {
let z = StructWithSomeProtocolBox(x: SimpleClass(x: 32))

// CHECK-NEXT: Before deinit
print("Before deinit")

// CHECK-NEXT: SimpleClass deinitialized!
var wrapper = createExistentialWrapper(z)
testAssignCopy(ptr, from: &wrapper)
}

// CHECK-NEXT: Before deinit
print("Before deinit")

// CHECK-NEXT: SimpleClass deinitialized!
testDestroy(ptr)

ptr.deallocate()
}

testExistentialStructTypeChange()

func testAnyWrapper() {
let ptr = UnsafeMutablePointer<AnyWrapper>.allocate(capacity: 1)

Expand Down