Skip to content

Commit 78bb298

Browse files
committed
ABI/API checker: diagnose adding new designated initializers to open classes
If client's subclass provides an implementation of all of its superclass designated initializers, it automatically inherits all of the superclass convenience initializers. This means if a new designated init is added to the base class, the inherited convenience init may be missing and cause breakage. SR-11454
1 parent ec862c5 commit 78bb298

File tree

14 files changed

+83
-9
lines changed

14 files changed

+83
-9
lines changed

include/swift/AST/DiagnosticsModuleDiffer.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ ERROR(new_decl_without_intro,none,"%0 is a new API without @available attribute"
9898

9999
ERROR(objc_name_change,none,"%0 has ObjC name change from %1 to %2", (StringRef, StringRef, StringRef))
100100

101+
ERROR(desig_init_added,none,"%0 has been added as a designated initializer to an open class", (StringRef))
102+
101103
#ifndef DIAG_NO_UNDEF
102104
# if defined(DIAG)
103105
# undef DIAG

include/swift/IDE/DigesterEnums.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ KEY_STRING(IntrotvOS, intro_tvOS)
152152
KEY_STRING(IntrowatchOS, intro_watchOS)
153153
KEY_STRING(Introswift, intro_swift)
154154
KEY_STRING(ObjCName, objc_name)
155+
KEY_STRING(InitKind, init_kind)
155156

156157
KEY_STRING_ARR(SuperclassNames, superclassNames)
157158
KEY_STRING_ARR(ToolArgs, tool_arguments)

test/api-digester/Inputs/cake_baseline/cake.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,12 @@ public class SwiftObjcClass {
201201
@objc(OldObjCFool:OldObjCA:OldObjCB:)
202202
public func foo(a:Int, b:Int, c: Int) {}
203203
}
204+
205+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
206+
open class AddingNewDesignatedInit {
207+
public init() {}
208+
public convenience init(foo: Int) {
209+
self.init()
210+
print(foo)
211+
}
212+
}

test/api-digester/Inputs/cake_current/cake.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,13 @@ public class SwiftObjcClass {
208208
@objc(NewObjCFool:NewObjCA:NewObjCB:)
209209
public func foo(a:Int, b:Int, c: Int) {}
210210
}
211+
212+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
213+
open class AddingNewDesignatedInit {
214+
public init(_ b: Bool) {}
215+
public init() {}
216+
public convenience init(foo: Int) {
217+
self.init()
218+
print(foo)
219+
}
220+
}

test/api-digester/Outputs/Cake-abi.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,4 @@ cake: Var RequiementChanges.addedVar has been added as a protocol requirement
115115
cake: Class C4 has changed its super class from APINotesTest.OldType to APINotesTest.NewType
116116
cake: Class SubGenericClass has changed its super class from cake.GenericClass<cake.P1> to cake.GenericClass<cake.P2>
117117
cake: Class SuperClassRemoval has removed its super class cake.C3
118+
cake: Constructor AddingNewDesignatedInit.init(_:) has been added as a designated initializer to an open class

test/api-digester/Outputs/Cake.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ cake: Accessor ClassWithOpenMember.property.Get() is no longer open for subclass
6464
cake: Class C4 has changed its super class from APINotesTest.OldType to APINotesTest.NewType
6565
cake: Class SubGenericClass has changed its super class from cake.GenericClass<cake.P1> to cake.GenericClass<cake.P2>
6666
cake: Class SuperClassRemoval has removed its super class cake.C3
67+
cake: Constructor AddingNewDesignatedInit.init(_:) has been added as a designated initializer to an open class
6768
cake: Func ClassWithOpenMember.bar() is no longer open for subclassing
6869
cake: Func ClassWithOpenMember.foo() is no longer open for subclassing
6970
cake: Var ClassWithOpenMember.property is no longer open for subclassing

test/api-digester/Outputs/cake-abi.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@
564564
"implicit": true,
565565
"declAttributes": [
566566
"Inlinable"
567-
]
567+
],
568+
"init_kind": "Designated"
568569
},
569570
{
570571
"kind": "Var",
@@ -1823,5 +1824,5 @@
18231824
]
18241825
}
18251826
],
1826-
"json_format_version": 5
1827+
"json_format_version": 6
18271828
}

test/api-digester/Outputs/cake.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,8 @@
584584
"implicit": true,
585585
"declAttributes": [
586586
"Inlinable"
587-
]
587+
],
588+
"init_kind": "Designated"
588589
},
589590
{
590591
"kind": "Var",
@@ -1674,5 +1675,5 @@
16741675
]
16751676
}
16761677
],
1677-
"json_format_version": 5
1678+
"json_format_version": 6
16781679
}

test/api-digester/Outputs/clang-module-dump.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@
105105
"Override",
106106
"ObjC",
107107
"Dynamic"
108-
]
108+
],
109+
"init_kind": "Designated"
109110
}
110111
],
111112
"declKind": "Class",
@@ -183,5 +184,5 @@
183184
]
184185
}
185186
],
186-
"json_format_version": 5
187+
"json_format_version": 6
187188
}

test/api-digester/Outputs/empty-baseline.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"kind": "Root",
33
"name": "TopLevel",
44
"printedName": "TopLevel",
5-
"json_format_version": 5
5+
"json_format_version": 6
66
}

0 commit comments

Comments
 (0)