Skip to content

Commit 8cf1e3c

Browse files
committed
Sema: Fix availability of inherited designated initializers
Inherited designated initializers got the same availability as the corresponding initialier in the superclass. However if the superclass was more available than the subclass, we would generate a diagnostic that a member cannot be more available than its containing type. This diagnostic had an unknown source location, since the location was for a synthesized declaration, causing confusion. Fixes <https://bugs.swift.org/browse/SR-7881> aka <rdar://problem/40853731>.
1 parent 1a8b66e commit 8cf1e3c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/Sema/CodeSynthesis.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,9 +2037,12 @@ static void configureDesignatedInitAttributes(TypeChecker &tc,
20372037
ctor->getAttrs().add(clonedAttr);
20382038
}
20392039

2040-
// Make sure the constructor is only as available as its superclass's
2041-
// constructor.
2042-
AvailabilityInference::applyInferredAvailableAttrs(ctor, superclassCtor, ctx);
2040+
// If the superclass has its own availability, make sure the synthesized
2041+
// constructor is only as available as its superclass's constructor.
2042+
if (superclassCtor->getAttrs().hasAttribute<AvailableAttr>()) {
2043+
AvailabilityInference::applyInferredAvailableAttrs(
2044+
ctor, {classDecl, superclassCtor}, ctx);
2045+
}
20432046

20442047
if (superclassCtor->isObjC()) {
20452048
// Inherit the @objc name from the superclass initializer, if it

test/Sema/availability_versions.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,19 @@ func useUnavailableExtension() {
985985
// expected-note@-1 {{add 'if #available' version check}}
986986
}
987987

988+
// Availability of synthesized designated initializers.
989+
990+
@available(OSX, introduced: 10.51)
991+
class WidelyAvailableBase {
992+
init() {}
993+
994+
@available(OSX, introduced: 10.52)
995+
init(thing: ()) {}
996+
}
997+
998+
@available(OSX, introduced: 10.53)
999+
class EsotericSmallBatchHipsterThing : WidelyAvailableBase {}
1000+
9881001
// Useless #available(...) checks
9891002

9901003
func functionWithDefaultAvailabilityAndUselessCheck(_ p: Bool) {

0 commit comments

Comments
 (0)