Skip to content

[IRGen] Don't set HasLayoutString flag on non-copyable generic types #81530

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 1 commit into from
May 15, 2025
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
28 changes: 16 additions & 12 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6014,12 +6014,14 @@ namespace {
if (!layoutStringsEnabled(IGM)) {
return false;
}
return !!getLayoutString() ||
(IGM.Context.LangOpts.hasFeature(
Feature::LayoutStringValueWitnessesInstantiation) &&
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation &&
(HasDependentVWT || HasDependentMetadata) &&
!isa<FixedTypeInfo>(IGM.getTypeInfo(getLoweredType())));
const auto &TI = IGM.getTypeInfo(getLoweredType());
return (!!getLayoutString() ||
(IGM.Context.LangOpts.hasFeature(
Feature::LayoutStringValueWitnessesInstantiation) &&
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation &&
(HasDependentVWT || HasDependentMetadata) &&
!isa<FixedTypeInfo>(TI))) &&
TI.isCopyable(ResilienceExpansion::Maximal);
}

llvm::Constant *emitNominalTypeDescriptor() {
Expand Down Expand Up @@ -6547,13 +6549,15 @@ namespace {
if (!layoutStringsEnabled(IGM)) {
return false;
}
auto &TI = IGM.getTypeInfo(getLoweredType());

return !!getLayoutString() ||
(IGM.Context.LangOpts.hasFeature(
Feature::LayoutStringValueWitnessesInstantiation) &&
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation &&
(HasDependentVWT || HasDependentMetadata) &&
!isa<FixedTypeInfo>(IGM.getTypeInfo(getLoweredType())));
return (!!getLayoutString() ||
(IGM.Context.LangOpts.hasFeature(
Feature::LayoutStringValueWitnessesInstantiation) &&
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation &&
(HasDependentVWT || HasDependentMetadata) &&
!isa<FixedTypeInfo>(TI))) &&
TI.isCopyable(ResilienceExpansion::Maximal);
}

llvm::Constant *emitNominalTypeDescriptor() {
Expand Down
20 changes: 20 additions & 0 deletions test/Interpreter/Inputs/layout_string_witnesses_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,21 @@ public enum MultiPayloadAnyObject {
case z(AnyObject)
}

public struct NonCopyableGenericStruct<T>: ~Copyable {
let x: Int
let y: T

public init(x: Int, y: T) {
self.x = x
self.y = y
}
}

public enum NonCopyableGenericEnum<T>: ~Copyable {
case x(Int, T?)
case y(Int)
}

@inline(never)
public func consume<T>(_ x: T.Type) {
withExtendedLifetime(x) {}
Expand Down Expand Up @@ -751,6 +766,11 @@ public func testGenericArrayDestroy<T>(_ buffer: UnsafeMutableBufferPointer<T>)
buffer.deinitialize()
}

@inline(never)
public func testGenericArrayDestroy<T: ~Copyable>(_ buffer: UnsafeMutableBufferPointer<T>) {
buffer.deinitialize()
}

@inline(never)
public func testGenericArrayInitWithCopy<T>(dest: UnsafeMutableBufferPointer<T>, src: UnsafeMutableBufferPointer<T>) {
_ = dest.initialize(fromContentsOf: src)
Expand Down
36 changes: 36 additions & 0 deletions test/Interpreter/layout_string_witnesses_dynamic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,42 @@ func testGenericResilientWithUnmanagedAndWeak() {

testGenericResilientWithUnmanagedAndWeak()

func testNonCopyableGenericStructSimpleClass() {
let ptr = UnsafeMutableBufferPointer<NonCopyableGenericStruct<SimpleClass>>.allocate(capacity: 1)

let x = NonCopyableGenericStruct(x: 23, y: SimpleClass(x: 23))
ptr[0] = x

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


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

ptr.deallocate()
}

testNonCopyableGenericStructSimpleClass()

func testNonCopyableGenericEnumSimpleClass() {
let ptr = UnsafeMutableBufferPointer<NonCopyableGenericEnum<SimpleClass>>.allocate(capacity: 1)

let x = NonCopyableGenericEnum.x(23, SimpleClass(x: 23))
ptr[0] = x

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


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

ptr.deallocate()
}

testNonCopyableGenericEnumSimpleClass()

#if os(macOS)

import Foundation
Expand Down