Skip to content

PerformanceDiagnostic: give an error if a generic non-copyable value with a deinit is captured by an escaping closure. #74988

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
Jul 8, 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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSIL.def
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ ERROR(embedded_swift_allocating_type,none,
"cannot use allocating type %0 in -no-allocations mode", (Type))
ERROR(embedded_swift_allocating,none,
"cannot use allocating operation in -no-allocations mode", ())
ERROR(embedded_capture_of_generic_value_with_deinit,none,
"capturing generic non-copyable type with deinit in escaping closure not supported in embedded Swift", ())
NOTE(performance_called_from,none,
"called from here", ())

Expand Down
50 changes: 50 additions & 0 deletions lib/SILOptimizer/Mandatory/PerformanceDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,46 @@ static bool isEffectFreeArraySemanticCall(SILInstruction *inst) {
}
}

static bool hasGenericValueDeinit(SILType ty, SILFunction *f) {
if (!ty.isMoveOnly())
return false;
NominalTypeDecl *nominal = ty.getNominalOrBoundGenericNominal();
if (!nominal)
return false;

if (nominal->getGenericSignature() && nominal->getValueTypeDestructor())
return true;

if (isa<StructDecl>(nominal)) {
for (unsigned i = 0, n = ty.getNumNominalFields(); i < n; ++i) {
if (hasGenericValueDeinit(ty.getFieldType(i, f), f))
return true;
}
} else if (auto *en = dyn_cast<EnumDecl>(nominal)) {
for (EnumElementDecl *element : en->getAllElements()) {
if (element->hasAssociatedValues()) {
if (hasGenericValueDeinit(ty.getEnumElementType(element, f), f))
return true;
}
}
}

return false;
}

static bool allocsGenericValueTypeWithDeinit(AllocBoxInst *abi) {
CanSILBoxType boxTy = abi->getBoxType();
SILFunction *f = abi->getFunction();
unsigned numFields = boxTy->getLayout()->getFields().size();
for (unsigned fieldIdx = 0; fieldIdx < numFields; ++fieldIdx) {
SILType fieldTy = getSILBoxFieldType(TypeExpansionContext(*f), boxTy,
abi->getModule().Types, fieldIdx);
if (hasGenericValueDeinit(fieldTy, f))
return true;
}
return false;
}

/// Prints Embedded Swift specific performance diagnostics (no existentials,
/// no metatypes, optionally no allocations) for \p function.
bool PerformanceDiagnostics::visitFunctionEmbeddedSwift(
Expand Down Expand Up @@ -220,6 +260,16 @@ bool PerformanceDiagnostics::visitFunctionEmbeddedSwift(
default:
break;
}
} else if (auto *abi = dyn_cast<AllocBoxInst>(&inst)) {
// It needs a bit of work to support alloc_box of generic non-copyable
// structs/enums with deinit, because we need to specialize the deinit
// functions, though they are not explicitly referenced in SIL.
// Until this is supported, give an error in such cases. Otherwise
// IRGen would crash.
if (allocsGenericValueTypeWithDeinit(abi)) {
LocWithParent loc(abi->getLoc().getSourceLoc(), parentLoc);
diagnose(loc, diag::embedded_capture_of_generic_value_with_deinit);
}
}
}
}
Expand Down
112 changes: 112 additions & 0 deletions test/embedded/noncopyable-captures.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// RUN: %target-swift-emit-ir %s -DIGNORE_FAILS -enable-experimental-feature Embedded -wmo -o /dev/null
// RUN: %target-swift-emit-ir %s -enable-experimental-feature Embedded -wmo -verify

struct MyStruct<Item> : ~Copyable {
var member = "42"

init() {}
deinit {}
mutating func foo() {}
}

var escape: (()->())?

#if !IGNORE_FAILS

public func test() {
var s = MyStruct<Int>() // expected-error {{capturing generic non-copyable type with deinit in escaping closure not supported in embedded Swift}}
s.foo()
escape = {
s.foo()
}
}

//

struct Outer: ~Copyable {
var inner: MyStruct<Int>
}

public func testNested() {
var s = Outer(inner: MyStruct<Int>()) // expected-error {{capturing generic non-copyable type with deinit in escaping closure not supported in embedded Swift}}
s.inner.foo()
escape = {
s.inner.foo()
}
}

//

enum E: ~Copyable {
case A(MyStruct<Int>)
case B

mutating func foo() {}
}

public func testEnum() {
var s = E.A(MyStruct<Int>()) // expected-error {{capturing generic non-copyable type with deinit in escaping closure not supported in embedded Swift}}
s.foo()
escape = {
s.foo()
}
}

#endif

//

struct StructWithoutDeinit<Item> {
var member = "42"

init() {}
mutating func foo() {}
}

public func testWithoutDeinit() {
var s = StructWithoutDeinit<Int>()
s.foo()
escape = {
s.foo()
}
}

//

struct NonCopyableStructWithoutDeinit<Item>: ~Copyable {
var member = "42"

init() {}
mutating func foo() {}
}

public func testNonCopyableWithoutDeinit() {
var s = NonCopyableStructWithoutDeinit<Int>()
s.foo()
escape = {
s.foo()
}
}

//

struct NonGenericStruct : ~Copyable {
var member = "42"

init() {
}

deinit {
}

mutating func foo() {
}
}

public func testNonGeneric() {
var s = NonGenericStruct()
s.foo()
escape = {
s.foo()
}
}