Skip to content

[6.0][Runtime] Fix CVW for genreic single payload enums with no extra inha… #73187

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
Apr 23, 2024
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
8 changes: 8 additions & 0 deletions stdlib/public/runtime/BytecodeLayouts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,10 @@ static void singlePayloadEnumGeneric(const Metadata *metadata,

if (tagBytes) {
xiType = nullptr;
} else if (!xiType) {
// If there are no inhabitants and the extra tag bits are not set,
// we have a payload.
return;
}
}

Expand Down Expand Up @@ -633,6 +637,10 @@ static void singlePayloadEnumGeneric(const Metadata *metadata,

if (tagBytes) {
xiType = nullptr;
} else if (!xiType) {
// If there are no inhabitants and the extra tag bits are not set,
// we have a payload.
return;
}
}

Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/runtime/Enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,10 @@ void swift::swift_initEnumMetadataSinglePayloadWithLayoutString(
_swift_addRefCountStringForMetatype(writer, flags, payloadType, fullOffset,
previousFieldOffset);

writer.writeBytes((uint64_t)previousFieldOffset);
writer.writeBytes((uint64_t)previousFieldOffset + extraTagBytes);

writer.offset = skipBytesOffset;
writer.writeBytes(size - previousFieldOffset);
writer.writeBytes(payloadSize - previousFieldOffset);

// we mask out HasRelativePointers, because at this point they have all been
// resolved to metadata pointers
Expand Down
5 changes: 5 additions & 0 deletions test/Interpreter/Inputs/layout_string_witnesses_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ public struct ContainsSinglePayloadSimpleClassEnum {
}
}

public enum TestOptional<T> {
case empty
case nonEmpty(T)
}

public enum SinglePayloadEnum<T> {
case empty
case nonEmpty(Int, T?)
Expand Down
67 changes: 59 additions & 8 deletions test/Interpreter/layout_string_witnesses_dynamic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1181,27 +1181,48 @@ func testTupleAlignment() {

testTupleAlignment()

#if os(macOS)
func testWeakRefOptionalNative() {
let ptr = allocateInternalGenericPtr(of: TestOptional<WeakNativeWrapper>.self)
let ptr2 = allocateInternalGenericPtr(of: TestOptional<WeakNativeWrapper>.self)

import Foundation
do {
let classInstance = SimpleClass(x: 23)

@objc
final class ObjcClass: NSObject {
deinit {
print("ObjcClass deinitialized!")
do {
let x = TestOptional.nonEmpty(WeakNativeWrapper(x: classInstance))
let y = TestOptional.nonEmpty(WeakNativeWrapper(x: classInstance))
testGenericInit(ptr, to: x)
testGenericInit(ptr2, to: y)
}

testGenericDestroy(ptr, of: TestOptional<WeakNativeWrapper>.self)
testGenericDestroy(ptr2, of: TestOptional<WeakNativeWrapper>.self)

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

// CHECK-NEXT: SimpleClass deinitialized!
}

ptr.deallocate()
}

testWeakRefOptionalNative()

#if os(macOS)

import Foundation

func testGenericObjc() {
let ptr = allocateInternalGenericPtr(of: ObjcClass.self)

do {
let x = ObjcClass()
let x = ObjcClass(x: 23)
testGenericInit(ptr, to: x)
}

do {
let y = ObjcClass()
let y = ObjcClass(x: 32)
// CHECK-macosx: Before deinit
print("Before deinit")

Expand All @@ -1220,4 +1241,34 @@ func testGenericObjc() {

testGenericObjc()

import Darwin

func testWeakRefOptionalObjc() {
let ptr = allocateInternalGenericPtr(of: TestOptional<WeakObjcWrapper>.self)
let ptr2 = allocateInternalGenericPtr(of: TestOptional<WeakObjcWrapper>.self)

do {
let classInstance = ObjcClass(x: 23)

do {
let x = TestOptional.nonEmpty(WeakObjcWrapper(x: classInstance))
let y = TestOptional.nonEmpty(WeakObjcWrapper(x: classInstance))
testGenericInit(ptr, to: x)
testGenericInit(ptr2, to: y)
}

testGenericDestroy(ptr, of: TestOptional<WeakObjcWrapper>.self)
testGenericDestroy(ptr2, of: TestOptional<WeakObjcWrapper>.self)

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

// CHECK-macosx-NEXT: ObjcClass deinitialized!
}

ptr.deallocate()
}

testWeakRefOptionalObjc()

#endif