Skip to content

Commit 2e865c8

Browse files
committed
prevent serialization of allocating inits for actors
A public designated initializer of a class would have its allocating entry-point serialized in the module, meaning with `-O` that entry-point can get inlined into programs linking against that module. Once that entry-point is inlined, the program will _require_ that it remain non-delegating, because it will depend on the 2nd entry-point (for actual initializing) to be in the library. As a result of this change, public initializers of an actor should be resilient in a library, whether their underlying implementation is delegating or not.
1 parent 73bc4b6 commit 2e865c8

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

lib/SIL/IR/SILDeclRef.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -669,13 +669,14 @@ IsSerialized_t SILDeclRef::isSerialized() const {
669669
return IsSerialized;
670670

671671
// The allocating entry point for designated initializers are serialized
672-
// if the class is @usableFromInline or public.
672+
// if the class is @usableFromInline or public. Actors are excluded because
673+
// whether the init is designated is not clearly reflected in the source code.
673674
if (kind == SILDeclRef::Kind::Allocator) {
674675
auto *ctor = cast<ConstructorDecl>(d);
675-
if (ctor->isDesignatedInit() &&
676-
ctor->getDeclContext()->getSelfClassDecl()) {
677-
if (!ctor->hasClangNode())
678-
return IsSerialized;
676+
if (auto classDecl = ctor->getDeclContext()->getSelfClassDecl()) {
677+
if (!classDecl->isAnyActor() && ctor->isDesignatedInit())
678+
if (!ctor->hasClangNode())
679+
return IsSerialized;
679680
}
680681
}
681682

test/SILGen/inlinable_attribute.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-emit-silgen -module-name inlinable_attribute -emit-verbose-sil -warnings-as-errors %s | %FileCheck %s
1+
// RUN: %target-swift-emit-silgen -module-name inlinable_attribute -emit-verbose-sil -warnings-as-errors -disable-availability-checking %s | %FileCheck %s
22

33
// CHECK-LABEL: sil [serialized] [ossa] @$s19inlinable_attribute15fragileFunctionyyF : $@convention(thin) () -> ()
44
@inlinable public func fragileFunction() {
@@ -37,6 +37,32 @@ public class MyCls {
3737
}
3838
}
3939

40+
public actor MyAct {
41+
// CHECK-LABEL: sil [serialized] [ossa] @$s19inlinable_attribute5MyActCfD : $@convention(method) (@owned MyAct) -> ()
42+
@inlinable deinit {}
43+
44+
/// whether delegating or not, the initializers for an actor are not serialized unless marked inlinable.
45+
46+
// CHECK-LABEL: sil [exact_self_class] [ossa] @$s19inlinable_attribute5MyActC14designatedInitACyt_tcfC : $@convention(method) (@thick MyAct.Type) -> @owned MyAct
47+
// CHECK-LABEL: sil [ossa] @$s19inlinable_attribute5MyActC14designatedInitACyt_tcfc : $@convention(method) (@owned MyAct) -> @owned MyAct
48+
public init(designatedInit: ()) {}
49+
50+
// CHECK-LABEL: sil [ossa] @$s19inlinable_attribute5MyActC15convenienceInitACyt_tcfC : $@convention(method) (@thick MyAct.Type) -> @owned MyAct
51+
public init(convenienceInit: ()) {
52+
self.init(designatedInit: ())
53+
}
54+
55+
56+
// CHECK-LABEL: sil [serialized] [exact_self_class] [ossa] @$s19inlinable_attribute5MyActC0A14DesignatedInitACyt_tcfC : $@convention(method) (@thick MyAct.Type) -> @owned MyAct
57+
// CHECK-LABEL: sil [serialized] [ossa] @$s19inlinable_attribute5MyActC0A14DesignatedInitACyt_tcfc : $@convention(method) (@owned MyAct) -> @owned MyAct
58+
@inlinable public init(inlinableDesignatedInit: ()) {}
59+
60+
// CHECK-LABEL: sil [serialized] [ossa] @$s19inlinable_attribute5MyActC0A15ConvenienceInitACyt_tcfC : $@convention(method) (@thick MyAct.Type) -> @owned MyAct
61+
@inlinable public init(inlinableConvenienceInit: ()) {
62+
self.init(designatedInit: ())
63+
}
64+
}
65+
4066
// Make sure enum case constructors for public and versioned enums are
4167
// [serialized].
4268
@usableFromInline enum MyEnum {

0 commit comments

Comments
 (0)