Skip to content

[6.0] Fix incorrect BitwiseCopyable conformance lookups #73462

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
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 lib/IRGen/GenArchetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ const TypeInfo *TypeConverter::convertArchetypeType(ArchetypeType *archetype) {
IGM.getSwiftModule()->getASTContext().getProtocol(
KnownProtocolKind::BitwiseCopyable);
// The protocol won't be present in swiftinterfaces from older SDKs.
if (bitwiseCopyableProtocol && IGM.getSwiftModule()->lookupConformance(
if (bitwiseCopyableProtocol && IGM.getSwiftModule()->checkConformance(
archetype, bitwiseCopyableProtocol)) {
return BitwiseCopyableTypeInfo::create(storageType, abiAccessible);
}
Expand Down
8 changes: 5 additions & 3 deletions lib/IRGen/GenEnum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7296,17 +7296,19 @@ ResilientEnumImplStrategy::completeEnumTypeLayout(TypeConverter &TC,
SILType Type,
EnumDecl *theEnum,
llvm::StructType *enumTy) {
auto cp = !theEnum->canBeCopyable()
? IsNotCopyable : IsCopyable;
auto abiAccessible = IsABIAccessible_t(TC.IGM.isTypeABIAccessible(Type));
auto *bitwiseCopyableProtocol =
IGM.getSwiftModule()->getASTContext().getProtocol(
KnownProtocolKind::BitwiseCopyable);
if (bitwiseCopyableProtocol &&
IGM.getSwiftModule()->lookupConformance(
theEnum->getDeclaredInterfaceType(), bitwiseCopyableProtocol)) {
IGM.getSwiftModule()->checkConformance(Type.getASTType(),
bitwiseCopyableProtocol)) {
return BitwiseCopyableTypeInfo::create(enumTy, abiAccessible);
}
return registerEnumTypeInfo(
new ResilientEnumTypeInfo(*this, enumTy, Copyable,
new ResilientEnumTypeInfo(*this, enumTy, cp,
abiAccessible));
}

Expand Down
3 changes: 1 addition & 2 deletions lib/IRGen/GenStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1705,8 +1705,7 @@ const TypeInfo *TypeConverter::convertStructType(TypeBase *key, CanType type,
IGM.getSwiftModule()->getASTContext().getProtocol(
KnownProtocolKind::BitwiseCopyable);
if (bitwiseCopyableProtocol &&
IGM.getSwiftModule()->lookupConformance(D->getDeclaredInterfaceType(),
bitwiseCopyableProtocol)) {
IGM.getSwiftModule()->checkConformance(type, bitwiseCopyableProtocol)) {
return BitwiseCopyableTypeInfo::create(IGM.OpaqueTy, structAccessible);
}
return &getResilientStructTypeInfo(copyable, structAccessible);
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenTuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ const TypeInfo *TypeConverter::convertTupleType(TupleType *tuple) {
auto *bitwiseCopyableProtocol =
IGM.getSwiftModule()->getASTContext().getProtocol(
KnownProtocolKind::BitwiseCopyable);
if (bitwiseCopyableProtocol && IGM.getSwiftModule()->lookupConformance(
if (bitwiseCopyableProtocol && IGM.getSwiftModule()->checkConformance(
tuple, bitwiseCopyableProtocol)) {
return BitwiseCopyableTypeInfo::create(IGM.OpaqueTy, IsABIAccessible);
}
Expand Down
37 changes: 37 additions & 0 deletions test/IRGen/variadic_generic_tuples.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: %target-swift-frontend -emit-ir %s | %FileCheck %s

sil_stage canonical

import Builtin
import Swift
import SwiftShims

// CHECK-LABEL: define{{.*}} swiftcc void @func1

// CHECK: [[METADATA_PAIR:%.*]] = phi %swift.metadata_response
// CHECK: [[METADATA:%.*]] = extractvalue %swift.metadata_response [[METADATA_PAIR]], 0
// CHECK: [[VWT_PTR:%.*]] = getelementptr inbounds ptr, ptr [[METADATA]], {{i32|i64}} -1
// CHECK: [[VWT:%.*]] = load ptr, ptr [[VWT_PTR]]
// CHECK: [[DESTROY_PTR:%.*]] = getelementptr inbounds ptr, ptr [[VWT]], i32 1
// CHECK: [[DESTROY:%.*]] = load ptr, ptr [[DESTROY_PTR]]
// CHECK: call void [[DESTROY]]({{.*}})
// CHECK: ret void

sil @func1 : $@convention(thin) <each V> (@in (repeat each V)) -> () {
bb0(%0 : $*(repeat each V)):
destroy_addr %0 : $*(repeat each V)
%ret = tuple ()
return %ret : $()
}

// CHECK-LABEL: define{{.*}} swiftcc void @func2
// CHECK-NOT: call void %
// CHECK: ret void

sil @func2 : $@convention(thin) <each V where repeat each V : BitwiseCopyable> (@in (repeat each V)) -> () {
bb0(%0 : $*(repeat each V)):
destroy_addr %0 : $*(repeat each V)
%ret = tuple ()
return %ret : $()
}

13 changes: 13 additions & 0 deletions test/Interpreter/Inputs/bitwise_copyable_other.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public struct BitwiseStruct<T> {
var t: T

public init(t: T) { self.t = t }
}

extension BitwiseStruct: BitwiseCopyable where T: BitwiseCopyable {}

public enum BitwiseEnum<T> {
case t(T)
}

extension BitwiseEnum: BitwiseCopyable where T: BitwiseCopyable {}
34 changes: 34 additions & 0 deletions test/Interpreter/bitwise_copyable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %empty-directory(%t)

// RUN: %target-build-swift-dylib(%t/%target-library-name(bitwise_copyable_other)) -enable-library-evolution %S/Inputs/bitwise_copyable_other.swift -emit-module -emit-module-path %t/bitwise_copyable_other.swiftmodule -module-name bitwise_copyable_other
// RUN: %target-codesign %t/%target-library-name(bitwise_copyable_other)

// RUN: %target-build-swift %s -lbitwise_copyable_other -I %t -L %t -o %t/main %target-rpath(%t)
// RUN: %target-codesign %t/main

// RUN: %target-run %t/main %t/%target-library-name(bitwise_copyable_other)

// REQUIRES: executable_test

import StdlibUnittest
import bitwise_copyable_other

var bitwise = TestSuite("BitwiseCopyable")

bitwise.test("Conditional conformance with resilient struct") {
func callee(_: consuming BitwiseStruct<LifetimeTracked>) {}

for _ in 0..<10 {
callee(BitwiseStruct(t: LifetimeTracked(0)))
}
}

bitwise.test("Conditional conformance with resilient enum") {
func callee(_: consuming BitwiseEnum<LifetimeTracked>) {}

for _ in 0..<10 {
callee(BitwiseEnum.t(LifetimeTracked(0)))
}
}

runAllTests()
47 changes: 41 additions & 6 deletions test/Interpreter/variadic_generic_captures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

import StdlibUnittest

var types = TestSuite("VariadicGenericCaptures")
var captures = TestSuite("VariadicGenericCaptures")

func hasMetadataPack<each T>(_: repeat each T) -> () -> Any.Type {
return { return (repeat each T).self }
}

types.test("Metadata") {
captures.test("Metadata") {
expectEqual(Void.self, hasMetadataPack()())
expectEqual((Int, String, Bool).self, hasMetadataPack(1, "hi", false)())
}
Expand All @@ -20,7 +20,7 @@ func hasWitnessTablePack<each T: Sequence>(_: repeat each T) -> () -> Any.Type {
return { return (repeat (each T).Element).self }
}

types.test("WitnessTable") {
captures.test("WitnessTable") {
expectEqual(Void.self, hasWitnessTablePack()())
expectEqual((Int, String, Bool).self, hasWitnessTablePack([1], ["hi"], [false])())
}
Expand All @@ -29,7 +29,7 @@ func hasWitnessTablePack2<each T: Sequence>(_: repeat each T) -> () -> Any.Type
return { return (repeat (each T).Element.Element).self }
}

types.test("WitnessTable2") {
captures.test("WitnessTable2") {
expectEqual(Void.self, hasWitnessTablePack2()())
expectEqual((Int, String, Bool).self, hasWitnessTablePack2([[1]], [["hi"]], [[false]])())
}
Expand All @@ -43,7 +43,7 @@ func lifetimeTest2() -> () -> Any.Type {
return hasMetadataPack(3, 1.0)
}

types.test("Lifetime") {
captures.test("Lifetime") {
let fn1 = lifetimeTest1()
let fn2 = lifetimeTest2()
expectEqual((String, Set<Int>).self, fn1())
Expand Down Expand Up @@ -71,7 +71,7 @@ func testNonEscapingCapture<each T: Hashable>(_ t: repeat each T) -> [AnyHashabl
}
}

types.test("CapturedValue") {
captures.test("CapturedValue") {
let fn1 = testEscapingCapture(1, "hi")
let fn2 = testEscapingCapture(5.0, false)

Expand All @@ -82,4 +82,39 @@ types.test("CapturedValue") {
expectEqual([true, 7], testNonEscapingCapture(true, 7))
}

captures.test("Leaks") {
func callee<T>(_: T) {}

func takesEscapingClosure(_ fn: @escaping () -> ()) {
fn()
fn()
fn()
}

func takesNonEscapingClosure(_ fn: () -> ()) {
fn()
fn()
fn()
}

func formPackCaptures<each V>(_ v: repeat each V) {
takesEscapingClosure { repeat callee(each v) }
takesNonEscapingClosure { repeat callee(each v) }
{ repeat callee(each v) }()
}

struct S {
init<each V>(_ v: repeat each V) {
takesEscapingClosure { repeat callee(each v) }
takesNonEscapingClosure { repeat callee(each v) }
{ repeat callee(each v) }()
}
}

for _ in 0..<10 {
formPackCaptures(LifetimeTracked(0), LifetimeTracked(0), LifetimeTracked(0))
callee(S(LifetimeTracked(1), LifetimeTracked(1), LifetimeTracked(1)))
}
}

runAllTests()