Skip to content

Commit 8b496f6

Browse files
committed
Embedded: allow generic class constructors
For example: ``` public class C: MyClass { public init(p: some P) { // ... } } ``` Constructors are not called via the vtable (except "required" constructors). Therefore we can allow generic constructors. #78150 rdar://138576752
1 parent 332b253 commit 8b496f6

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

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)

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)