Skip to content

Commit d0f8cce

Browse files
authored
Merge pull request #85857 from eeckstein/fix-vtable-specialization
Embedded: allow generic class constructors
2 parents 767c569 + 8b496f6 commit d0f8cce

File tree

6 files changed

+66
-21
lines changed

6 files changed

+66
-21
lines changed

SwiftCompilerSources/Sources/AST/Declarations.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,12 @@ final public class ParamDecl: VarDecl {
182182
final public class SubscriptDecl: AbstractStorageDecl, GenericContext {}
183183

184184
public class AbstractFunctionDecl: ValueDecl, GenericContext {
185-
public var isOverridden: Bool { bridged.AbstractFunction_isOverridden() }
185+
final public var isOverridden: Bool { bridged.AbstractFunction_isOverridden() }
186186
}
187187

188-
final public class ConstructorDecl: AbstractFunctionDecl {}
188+
final public class ConstructorDecl: AbstractFunctionDecl {
189+
public var isInheritable: Bool { bridged.Constructor_isInheritable() }
190+
}
189191

190192
final public class DestructorDecl: AbstractFunctionDecl {
191193
final public var isIsolated: Bool { bridged.Destructor_isIsolated() }

SwiftCompilerSources/Sources/Optimizer/Utilities/GenericSpecialization.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private struct VTableSpecializer {
8282
}
8383

8484
private func specializeEntries(of vTable: VTable, _ notifyNewFunction: (Function) -> ()) -> [VTable.Entry] {
85-
return vTable.entries.map { entry in
85+
return vTable.entries.compactMap { entry in
8686
if !entry.implementation.isGeneric {
8787
return entry
8888
}
@@ -95,6 +95,14 @@ private struct VTableSpecializer {
9595
let specializedMethod = context.specialize(function: entry.implementation, for: methodSubs,
9696
convertIndirectToDirect: true, isMandatory: true)
9797
else {
98+
if let constructor = entry.methodDecl.decl as? ConstructorDecl,
99+
!constructor.isInheritable
100+
{
101+
// For some reason, SILGen is putting constructors in the vtable, though they are never
102+
// called through the vtable.
103+
// Dropping those vtable entries allows using constructors with generic arguments.
104+
return nil
105+
}
98106
return entry
99107
}
100108
notifyNewFunction(specializedMethod)

include/swift/AST/ASTBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ struct BridgedDeclObj {
351351
BRIDGED_INLINE bool ProtocolDecl_requiresClass() const;
352352
BRIDGED_INLINE bool ProtocolDecl_isMarkerProtocol() const;
353353
BRIDGED_INLINE bool AbstractFunction_isOverridden() const;
354+
BRIDGED_INLINE bool Constructor_isInheritable() const;
354355
BRIDGED_INLINE bool Destructor_isIsolated() const;
355356
BRIDGED_INLINE bool EnumElementDecl_hasAssociatedValues() const;
356357
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedParameterList

include/swift/AST/ASTBridgingImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ bool BridgedDeclObj::AbstractFunction_isOverridden() const {
272272
return getAs<swift::AbstractFunctionDecl>()->isOverridden();
273273
}
274274

275+
bool BridgedDeclObj::Constructor_isInheritable() const {
276+
return getAs<swift::ConstructorDecl>()->isInheritable();
277+
}
278+
275279
bool BridgedDeclObj::Destructor_isIsolated() const {
276280
auto dd = getAs<swift::DestructorDecl>();
277281
auto ai = swift::getActorIsolation(dd);

lib/SIL/IR/SILVTable.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,14 @@ void SILVTable::updateVTableCache(const Entry &entry) {
7878
void SILVTable::replaceEntries(ArrayRef<Entry> newEntries) {
7979
auto entries = getMutableEntries();
8080
ASSERT(newEntries.size() <= entries.size());
81-
for (unsigned i = 0; i < entries.size(); ++i) {
82-
entries[i].getImplementation()->decrementRefCount();
83-
if (i < newEntries.size()) {
84-
entries[i] = newEntries[i];
85-
entries[i].getImplementation()->incrementRefCount();
86-
updateVTableCache(entries[i]);
87-
} else {
88-
removeFromVTableCache(entries[i]);
89-
}
81+
for (Entry &entry : getMutableEntries()) {
82+
entry.getImplementation()->decrementRefCount();
83+
removeFromVTableCache(entry);
84+
}
85+
for (unsigned i = 0; i < newEntries.size(); ++i) {
86+
entries[i] = newEntries[i];
87+
entries[i].getImplementation()->incrementRefCount();
88+
updateVTableCache(entries[i]);
9089
}
9190
NumEntries = newEntries.size();
9291
}

test/embedded/classes.swift

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,57 @@
88
// REQUIRES: optimized_stdlib
99
// REQUIRES: swift_feature_Embedded
1010

11-
class MyClass {
11+
public class MyClass {
1212
init() { print("MyClass.init") }
1313
deinit { print("MyClass.deinit") }
1414
func foo() { print("MyClass.foo") }
1515
}
1616

17-
class MySubClass: MyClass {
18-
var x = 27
17+
public class MySubClass: MyClass {
18+
var x: Int
19+
20+
override init() {
21+
self.x = 27
22+
print("MySubClass.init")
23+
}
24+
25+
public init(p: some P) {
26+
self.x = p.get()
27+
super.init()
28+
print("MySubClass.init")
29+
}
1930

20-
override init() { print("MySubClass.init") }
2131
deinit { print("MySubClass.deinit") }
22-
override func foo() { print("MySubClass.foo") }
32+
33+
override func foo() { print("MySubClass.foo: \(x)") }
2334

2435
func printX() {
2536
print(x)
2637
}
2738
}
2839

29-
class MySubSubClass: MySubClass {
30-
override init() { print("MySubSubClass.init") }
40+
public protocol P {
41+
func get() -> Int
42+
}
43+
44+
struct S: P {
45+
let i: Int
46+
47+
func get() -> Int { i }
48+
}
49+
50+
public class MySubSubClass: MySubClass {
51+
override init() {
52+
print("MySubSubClass.init")
53+
super.init()
54+
}
55+
3156
deinit { print("MySubSubClass.deinit") }
57+
3258
override func foo() { print("MySubSubClass.foo") }
3359
}
3460

35-
class OtherSubClass: MyClass {}
61+
public class OtherSubClass: MyClass {}
3662

3763
func testCasting(_ title: StaticString, _ c: MyClass) {
3864
print(title, terminator: "")
@@ -81,10 +107,15 @@ struct Main {
81107
o.1!.foo()
82108
o.2!.foo()
83109
// CHECK: MyClass.foo
84-
// CHECK: MySubClass.foo
110+
// CHECK: MySubClass.foo: 27
85111
// CHECK: MySubSubClass.foo
86112
print("")
87113

114+
print("4b") // CHECK: 4b
115+
o.1 = MySubClass(p: S(i: 42))
116+
o.1!.foo()
117+
// CHECK: MySubClass.foo: 42
118+
88119
print("5") // CHECK: 5
89120
o.0 = nil
90121
// CHECK: MyClass.deinit

0 commit comments

Comments
 (0)