mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
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.
https://github.com/swiftlang/swift/issues/78150
rdar://138576752
This commit is contained in:
@@ -82,7 +82,7 @@ private struct VTableSpecializer {
|
||||
}
|
||||
|
||||
private func specializeEntries(of vTable: VTable, _ notifyNewFunction: (Function) -> ()) -> [VTable.Entry] {
|
||||
return vTable.entries.map { entry in
|
||||
return vTable.entries.compactMap { entry in
|
||||
if !entry.implementation.isGeneric {
|
||||
return entry
|
||||
}
|
||||
@@ -95,6 +95,14 @@ private struct VTableSpecializer {
|
||||
let specializedMethod = context.specialize(function: entry.implementation, for: methodSubs,
|
||||
convertIndirectToDirect: true, isMandatory: true)
|
||||
else {
|
||||
if let constructor = entry.methodDecl.decl as? ConstructorDecl,
|
||||
!constructor.isInheritable
|
||||
{
|
||||
// For some reason, SILGen is putting constructors in the vtable, though they are never
|
||||
// called through the vtable.
|
||||
// Dropping those vtable entries allows using constructors with generic arguments.
|
||||
return nil
|
||||
}
|
||||
return entry
|
||||
}
|
||||
notifyNewFunction(specializedMethod)
|
||||
|
||||
@@ -8,31 +8,57 @@
|
||||
// REQUIRES: optimized_stdlib
|
||||
// REQUIRES: swift_feature_Embedded
|
||||
|
||||
class MyClass {
|
||||
public class MyClass {
|
||||
init() { print("MyClass.init") }
|
||||
deinit { print("MyClass.deinit") }
|
||||
func foo() { print("MyClass.foo") }
|
||||
}
|
||||
|
||||
class MySubClass: MyClass {
|
||||
var x = 27
|
||||
public class MySubClass: MyClass {
|
||||
var x: Int
|
||||
|
||||
override init() {
|
||||
self.x = 27
|
||||
print("MySubClass.init")
|
||||
}
|
||||
|
||||
public init(p: some P) {
|
||||
self.x = p.get()
|
||||
super.init()
|
||||
print("MySubClass.init")
|
||||
}
|
||||
|
||||
override init() { print("MySubClass.init") }
|
||||
deinit { print("MySubClass.deinit") }
|
||||
override func foo() { print("MySubClass.foo") }
|
||||
|
||||
override func foo() { print("MySubClass.foo: \(x)") }
|
||||
|
||||
func printX() {
|
||||
print(x)
|
||||
}
|
||||
}
|
||||
|
||||
class MySubSubClass: MySubClass {
|
||||
override init() { print("MySubSubClass.init") }
|
||||
public protocol P {
|
||||
func get() -> Int
|
||||
}
|
||||
|
||||
struct S: P {
|
||||
let i: Int
|
||||
|
||||
func get() -> Int { i }
|
||||
}
|
||||
|
||||
public class MySubSubClass: MySubClass {
|
||||
override init() {
|
||||
print("MySubSubClass.init")
|
||||
super.init()
|
||||
}
|
||||
|
||||
deinit { print("MySubSubClass.deinit") }
|
||||
|
||||
override func foo() { print("MySubSubClass.foo") }
|
||||
}
|
||||
|
||||
class OtherSubClass: MyClass {}
|
||||
public class OtherSubClass: MyClass {}
|
||||
|
||||
func testCasting(_ title: StaticString, _ c: MyClass) {
|
||||
print(title, terminator: "")
|
||||
@@ -81,10 +107,15 @@ struct Main {
|
||||
o.1!.foo()
|
||||
o.2!.foo()
|
||||
// CHECK: MyClass.foo
|
||||
// CHECK: MySubClass.foo
|
||||
// CHECK: MySubClass.foo: 27
|
||||
// CHECK: MySubSubClass.foo
|
||||
print("")
|
||||
|
||||
print("4b") // CHECK: 4b
|
||||
o.1 = MySubClass(p: S(i: 42))
|
||||
o.1!.foo()
|
||||
// CHECK: MySubClass.foo: 42
|
||||
|
||||
print("5") // CHECK: 5
|
||||
o.0 = nil
|
||||
// CHECK: MyClass.deinit
|
||||
|
||||
Reference in New Issue
Block a user