Skip to content

Commit 8299d7b

Browse files
authored
Merge pull request #81531 from drexin/wip-151176697-6.2
[6.2][IRGen] Don't set HasLayoutString flag on non-copyable generic types
2 parents 21f3bc1 + 75c581d commit 8299d7b

File tree

3 files changed

+72
-12
lines changed

3 files changed

+72
-12
lines changed

lib/IRGen/GenMeta.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6014,12 +6014,14 @@ namespace {
60146014
if (!layoutStringsEnabled(IGM)) {
60156015
return false;
60166016
}
6017-
return !!getLayoutString() ||
6018-
(IGM.Context.LangOpts.hasFeature(
6019-
Feature::LayoutStringValueWitnessesInstantiation) &&
6020-
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation &&
6021-
(HasDependentVWT || HasDependentMetadata) &&
6022-
!isa<FixedTypeInfo>(IGM.getTypeInfo(getLoweredType())));
6017+
const auto &TI = IGM.getTypeInfo(getLoweredType());
6018+
return (!!getLayoutString() ||
6019+
(IGM.Context.LangOpts.hasFeature(
6020+
Feature::LayoutStringValueWitnessesInstantiation) &&
6021+
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation &&
6022+
(HasDependentVWT || HasDependentMetadata) &&
6023+
!isa<FixedTypeInfo>(TI))) &&
6024+
TI.isCopyable(ResilienceExpansion::Maximal);
60236025
}
60246026

60256027
llvm::Constant *emitNominalTypeDescriptor() {
@@ -6547,13 +6549,15 @@ namespace {
65476549
if (!layoutStringsEnabled(IGM)) {
65486550
return false;
65496551
}
6552+
auto &TI = IGM.getTypeInfo(getLoweredType());
65506553

6551-
return !!getLayoutString() ||
6552-
(IGM.Context.LangOpts.hasFeature(
6553-
Feature::LayoutStringValueWitnessesInstantiation) &&
6554-
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation &&
6555-
(HasDependentVWT || HasDependentMetadata) &&
6556-
!isa<FixedTypeInfo>(IGM.getTypeInfo(getLoweredType())));
6554+
return (!!getLayoutString() ||
6555+
(IGM.Context.LangOpts.hasFeature(
6556+
Feature::LayoutStringValueWitnessesInstantiation) &&
6557+
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation &&
6558+
(HasDependentVWT || HasDependentMetadata) &&
6559+
!isa<FixedTypeInfo>(TI))) &&
6560+
TI.isCopyable(ResilienceExpansion::Maximal);
65576561
}
65586562

65596563
llvm::Constant *emitNominalTypeDescriptor() {

test/Interpreter/Inputs/layout_string_witnesses_types.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,21 @@ public enum MultiPayloadAnyObject {
673673
case z(AnyObject)
674674
}
675675

676+
public struct NonCopyableGenericStruct<T>: ~Copyable {
677+
let x: Int
678+
let y: T
679+
680+
public init(x: Int, y: T) {
681+
self.x = x
682+
self.y = y
683+
}
684+
}
685+
686+
public enum NonCopyableGenericEnum<T>: ~Copyable {
687+
case x(Int, T?)
688+
case y(Int)
689+
}
690+
676691
@inline(never)
677692
public func consume<T>(_ x: T.Type) {
678693
withExtendedLifetime(x) {}
@@ -751,6 +766,11 @@ public func testGenericArrayDestroy<T>(_ buffer: UnsafeMutableBufferPointer<T>)
751766
buffer.deinitialize()
752767
}
753768

769+
@inline(never)
770+
public func testGenericArrayDestroy<T: ~Copyable>(_ buffer: UnsafeMutableBufferPointer<T>) {
771+
buffer.deinitialize()
772+
}
773+
754774
@inline(never)
755775
public func testGenericArrayInitWithCopy<T>(dest: UnsafeMutableBufferPointer<T>, src: UnsafeMutableBufferPointer<T>) {
756776
_ = dest.initialize(fromContentsOf: src)

test/Interpreter/layout_string_witnesses_dynamic.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,42 @@ func testGenericResilientWithUnmanagedAndWeak() {
12391239

12401240
testGenericResilientWithUnmanagedAndWeak()
12411241

1242+
func testNonCopyableGenericStructSimpleClass() {
1243+
let ptr = UnsafeMutableBufferPointer<NonCopyableGenericStruct<SimpleClass>>.allocate(capacity: 1)
1244+
1245+
let x = NonCopyableGenericStruct(x: 23, y: SimpleClass(x: 23))
1246+
ptr[0] = x
1247+
1248+
// CHECK-NEXT: Before deinit
1249+
print("Before deinit")
1250+
1251+
1252+
// CHECK-NEXT: SimpleClass deinitialized!
1253+
testGenericArrayDestroy(ptr)
1254+
1255+
ptr.deallocate()
1256+
}
1257+
1258+
testNonCopyableGenericStructSimpleClass()
1259+
1260+
func testNonCopyableGenericEnumSimpleClass() {
1261+
let ptr = UnsafeMutableBufferPointer<NonCopyableGenericEnum<SimpleClass>>.allocate(capacity: 1)
1262+
1263+
let x = NonCopyableGenericEnum.x(23, SimpleClass(x: 23))
1264+
ptr[0] = x
1265+
1266+
// CHECK-NEXT: Before deinit
1267+
print("Before deinit")
1268+
1269+
1270+
// CHECK-NEXT: SimpleClass deinitialized!
1271+
testGenericArrayDestroy(ptr)
1272+
1273+
ptr.deallocate()
1274+
}
1275+
1276+
testNonCopyableGenericEnumSimpleClass()
1277+
12421278
#if os(macOS)
12431279

12441280
import Foundation

0 commit comments

Comments
 (0)