Skip to content

[Runtime] Fix issues with dynamic layout string instantiation #67485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 24, 2023
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
2 changes: 1 addition & 1 deletion stdlib/public/runtime/BytecodeLayouts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ void swift::swift_resolve_resilientAccessors(uint8_t *layoutStr,
reader.skip(sizeof(uintptr_t));
break;
case RefCountingKind::SinglePayloadEnumSimple:
reader.skip((3 * sizeof(uint64_t)) + (4 * sizeof(size_t)));
reader.skip((2 * sizeof(uint64_t)) + (4 * sizeof(size_t)));
break;

case RefCountingKind::SinglePayloadEnumFN: {
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/runtime/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2827,9 +2827,9 @@ void swift::_swift_addRefCountStringForMetatype(LayoutStringWriter &writer,
}

if (offset) {
LayoutStringReader tagReader {writer.layoutStr, writer.offset};
auto writerOffsetCopy = writer.offset;
reader.offset = layoutStringHeaderSize;
auto firstTagAndOffset = reader.readBytes<uint64_t>();
auto firstTagAndOffset = tagReader.readBytes<uint64_t>();
firstTagAndOffset += offset;
writer.writeBytes(firstTagAndOffset);
writer.offset = writerOffsetCopy;
Expand Down
70 changes: 70 additions & 0 deletions test/Interpreter/layout_string_witnesses_dynamic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,41 @@ func testGenericSinglePayloadEnumManyXI() {

testGenericSinglePayloadEnumManyXI()

struct RefPlusEnumResolve {
let x: SimpleClass
let y: ResilientSinglePayloadEnumComplex
}

func testRefPlusEnumResolve() {
let ptr = allocateInternalGenericPtr(of: RefPlusEnumResolve.self)

do {
let x = RefPlusEnumResolve(x: SimpleClass(x: 23), y: .nonEmpty(.nonEmpty1(SimpleClass(x: 23))))
testGenericInit(ptr, to: x)
}

do {
let y = RefPlusEnumResolve(x: SimpleClass(x: 23), y: .nonEmpty(.nonEmpty1(SimpleClass(x: 23))))
// CHECK: Before deinit
print("Before deinit")

// CHECK-NEXT: SimpleClass deinitialized!
// CHECK-NEXT: SimpleClass deinitialized!
testGenericAssign(ptr, from: y)
}

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

// CHECK-NEXT: SimpleClass deinitialized!
// CHECK-NEXT: SimpleClass deinitialized!
testGenericDestroy(ptr, of: RefPlusEnumResolve.self)

ptr.deallocate()
}

testRefPlusEnumResolve()

func testResilientSingletonEnumTag() {
let x = switch getResilientSingletonEnumNonEmpty(SimpleClass(x: 23)) {
case .nonEmpty: 0
Expand Down Expand Up @@ -939,6 +974,41 @@ func testResilientPayloadSinglePayloadEnum() {

testResilientPayloadSinglePayloadEnum()

struct SinglePayloadSimpleResolve {
let x: ResilientSinglePayloadEnumSimple
let y: ResilientSinglePayloadEnumComplex
}

func testSinglePayloadSimpleResolve() {
let ptr = allocateInternalGenericPtr(of: SinglePayloadSimpleResolve.self)

do {
let x = SinglePayloadSimpleResolve(x: .nonEmpty(SimpleClass(x: 23)), y: .nonEmpty(.nonEmpty1(SimpleClass(x: 23))))
testGenericInit(ptr, to: x)
}

do {
let y = SinglePayloadSimpleResolve(x: .nonEmpty(SimpleClass(x: 32)), y: .nonEmpty(.nonEmpty1(SimpleClass(x: 32))))
// CHECK: Before deinit
print("Before deinit")

// CHECK-NEXT: SimpleClass deinitialized!
// CHECK-NEXT: SimpleClass deinitialized!
testGenericAssign(ptr, from: y)
}

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

// CHECK-NEXT: SimpleClass deinitialized!
// CHECK-NEXT: SimpleClass deinitialized!
testGenericDestroy(ptr, of: SinglePayloadSimpleResolve.self)

ptr.deallocate()
}

testSinglePayloadSimpleResolve()

#if os(macOS)

import Foundation
Expand Down