Skip to content

Commit 7e3f56d

Browse files
committed
[Runtime] Fix generic existentials in layout strings
1 parent 56048ac commit 7e3f56d

File tree

4 files changed

+79
-9
lines changed

4 files changed

+79
-9
lines changed

stdlib/public/runtime/Metadata.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2895,7 +2895,7 @@ void swift::_swift_addRefCountStringForMetatype(LayoutStringWriter &writer,
28952895
auto tag = existential->isClassBounded() ? RefCountingKind::Unknown
28962896
: RefCountingKind::Existential;
28972897
writer.writeBytes(((uint64_t)tag << 56) | offset);
2898-
previousFieldOffset = existential->isClassBounded() ? fieldType->vw_size() - sizeof(uintptr_t) : 0;
2898+
previousFieldOffset = fieldType->vw_size() - (existential->isClassBounded() ? sizeof(uintptr_t) : (NumWords_ValueBuffer * sizeof(uintptr_t)));
28992899
fullOffset += fieldType->vw_size();
29002900
} else {
29012901
metadata:

test/Interpreter/Inputs/layout_string_witnesses_types.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,17 @@ public struct MultiProtocolExistentialWrapper {
279279
}
280280
}
281281

282+
public struct AnyWrapper {
283+
let x: Int = 0
284+
let y: Any
285+
let z: AnyObject
286+
287+
public init(y: Any, z: AnyObject) {
288+
self.y = y
289+
self.z = z
290+
}
291+
}
292+
282293
#if os(macOS)
283294
import Foundation
284295

@@ -343,8 +354,8 @@ public struct NestedWrapper<T> {
343354
}
344355

345356
struct InternalGeneric<T> {
346-
let x: T
347357
let y: Int
358+
let x: T
348359
}
349360

350361
public enum SinglePayloadSimpleClassEnum {
@@ -615,19 +626,19 @@ public func allocateInternalGenericPtr<T>(of tpe: T.Type) -> UnsafeMutableRawPoi
615626
@inline(never)
616627
public func testGenericAssign<T>(_ ptr: __owned UnsafeMutableRawPointer, from x: T) {
617628
let ptr = ptr.assumingMemoryBound(to: InternalGeneric<T>.self)
618-
let x = InternalGeneric(x: x, y: 23)
629+
let x = InternalGeneric(y: 23, x: x)
619630
testAssign(ptr, from: x)
620631
}
621632

622633
@inline(never)
623634
public func testGenericInit<T>(_ ptr: __owned UnsafeMutableRawPointer, to x: T) {
624635
let ptr = ptr.assumingMemoryBound(to: InternalGeneric<T>.self)
625-
let x = InternalGeneric(x: x, y: 23)
636+
let x = InternalGeneric(y: 23, x: x)
626637
testInit(ptr, to: x)
627638
}
628639

629640
@inline(never)
630641
public func testGenericDestroy<T>(_ ptr: __owned UnsafeMutableRawPointer, of tpe: T.Type) {
631-
let ptr = ptr.assumingMemoryBound(to: tpe)
642+
let ptr = ptr.assumingMemoryBound(to: InternalGeneric<T>.self)
632643
testDestroy(ptr)
633644
}

test/Interpreter/layout_string_witnesses_dynamic.swift

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// RUN: %target-codesign %t/%target-library-name(layout_string_witnesses_types)
55
// RUN: %target-swift-frontend -enable-experimental-feature LayoutStringValueWitnesses -enable-experimental-feature LayoutStringValueWitnessesInstantiation -enable-layout-string-value-witnesses -enable-layout-string-value-witnesses-instantiation -enable-library-evolution -enable-autolinking-runtime-compatibility-bytecode-layouts -emit-module -emit-module-path=%t/layout_string_witnesses_types_resilient.swiftmodule %S/Inputs/layout_string_witnesses_types_resilient.swift
66
// RUN: %target-build-swift -g -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnessesInstantiation -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-layout-string-value-witnesses-instantiation -Xfrontend -enable-library-evolution -c -parse-as-library -o %t/layout_string_witnesses_types_resilient.o %S/Inputs/layout_string_witnesses_types_resilient.swift
7-
// RUN: %target-build-swift -g -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnesses -Xfrontend -enable-experimental-feature -Xfrontend LayoutStringValueWitnessesInstantiation -Xfrontend -enable-layout-string-value-witnesses -Xfrontend -enable-layout-string-value-witnesses-instantiation -Xfrontend -enable-type-layout -parse-stdlib -module-name layout_string_witnesses_dynamic -llayout_string_witnesses_types -L%t %t/layout_string_witnesses_types_resilient.o -I %t -o %t/main %s %target-rpath(%t)
7+
// RUN: %target-build-swift -g -parse-stdlib -module-name layout_string_witnesses_dynamic -llayout_string_witnesses_types -L%t %t/layout_string_witnesses_types_resilient.o -I %t -o %t/main %s %target-rpath(%t)
88
// RUN: %target-codesign %t/main
99
// RUN: %target-run %t/main %t/%target-library-name(layout_string_witnesses_types) | %FileCheck %s --check-prefix=CHECK -check-prefix=CHECK-%target-os
1010

@@ -92,7 +92,35 @@ func testGeneric() {
9292

9393
testGeneric()
9494

95-
func testPrespecializedStructAnyObject() {
95+
func testGenericAny() {
96+
let ptr = allocateInternalGenericPtr(of: Any.self)
97+
98+
do {
99+
let x: Any = TestClass()
100+
testGenericInit(ptr, to: x as Any)
101+
}
102+
103+
do {
104+
let y: Any = TestClass()
105+
// CHECK: Before deinit
106+
print("Before deinit")
107+
108+
// CHECK-NEXT: TestClass deinitialized!
109+
testGenericAssign(ptr, from: y as Any)
110+
}
111+
112+
// CHECK-NEXT: Before deinit
113+
print("Before deinit")
114+
115+
// CHECK-NEXT: TestClass deinitialized!
116+
testGenericDestroy(ptr, of: Any.self)
117+
118+
ptr.deallocate()
119+
}
120+
121+
testGenericAny()
122+
123+
func testPrespecializedAnyObject() {
96124
let ptr = UnsafeMutablePointer<PrespecializedStruct<AnyObject>>.allocate(capacity: 1)
97125

98126
do {
@@ -120,7 +148,7 @@ func testPrespecializedStructAnyObject() {
120148
ptr.deallocate()
121149
}
122150

123-
testPrespecializedStructAnyObject()
151+
testPrespecializedAnyObject()
124152

125153
func testPrespecializedStructSimpleClass() {
126154
let ptr = UnsafeMutablePointer<PrespecializedStruct<SimpleClass>>.allocate(capacity: 1)
@@ -866,7 +894,7 @@ func testResilientSinglePayloadEnumComplexTag() {
866894
testResilientSinglePayloadEnumComplexTag()
867895

868896
func testResilientMultiPayloadEnumTag() {
869-
let x = switch getResilientMultiPayloadEnumEmpty0() {
897+
let x = switch getResilientMultiPayloadEnumEmpty0(AnyObject.self) {
870898
case .nonEmpty0: 0
871899
case .nonEmpty1: 1
872900
case .empty0: 2

test/Interpreter/layout_string_witnesses_static.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,37 @@ func testExistentialStructBox() {
293293

294294
testExistentialStructBox()
295295

296+
func testAnyWrapper() {
297+
let ptr = UnsafeMutablePointer<AnyWrapper>.allocate(capacity: 1)
298+
299+
do {
300+
let x = TestClass()
301+
testInit(ptr, to: AnyWrapper(y: x, z: SimpleClass(x: 23)))
302+
}
303+
304+
do {
305+
let y = TestClass()
306+
307+
// CHECK: Before deinit
308+
print("Before deinit")
309+
310+
// CHECK-NEXT: TestClass deinitialized!
311+
// CHECK-NEXT: SimpleClass deinitialized!
312+
testAssign(ptr, from: AnyWrapper(y: y, z: SimpleClass(x: 32)))
313+
}
314+
315+
// CHECK-NEXT: Before deinit
316+
print("Before deinit")
317+
318+
// CHECK-NEXT: TestClass deinitialized!
319+
// CHECK-NEXT: SimpleClass deinitialized!
320+
testDestroy(ptr)
321+
322+
ptr.deallocate()
323+
}
324+
325+
testAnyWrapper()
326+
296327
class ClassWithABC: A, B, C {
297328
deinit {
298329
print("ClassWithABC deinitialized!")

0 commit comments

Comments
 (0)