Skip to content

Commit 336f51e

Browse files
committed
Sema: Fix availability of inherited designated initializers, part 2
We need to explicitly handle nested types here. Fixes the (re-opened) <rdar://problem/40853731>, <https://bugs.swift.org/browse/SR-7881>.
1 parent cf501b4 commit 336f51e

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

lib/Sema/CodeSynthesis.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2404,8 +2404,20 @@ static void configureDesignatedInitAttributes(TypeChecker &tc,
24042404
// If the superclass has its own availability, make sure the synthesized
24052405
// constructor is only as available as its superclass's constructor.
24062406
if (superclassCtor->getAttrs().hasAttribute<AvailableAttr>()) {
2407+
SmallVector<Decl *, 2> asAvailableAs;
2408+
2409+
// We don't have to look at enclosing contexts of the superclass constructor,
2410+
// because designated initializers must always be defined in the superclass
2411+
// body, and we already enforce that a superclass is at least as available as
2412+
// a subclass.
2413+
asAvailableAs.push_back(superclassCtor);
2414+
Decl *parentDecl = classDecl;
2415+
while (parentDecl != nullptr) {
2416+
asAvailableAs.push_back(parentDecl);
2417+
parentDecl = parentDecl->getDeclContext()->getAsDeclOrDeclExtensionContext();
2418+
}
24072419
AvailabilityInference::applyInferredAvailableAttrs(
2408-
ctor, {classDecl, superclassCtor}, ctx);
2420+
ctor, asAvailableAs, ctx);
24092421
}
24102422

24112423
// Wire up the overrides.

test/Sema/availability_versions.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,11 @@ class WidelyAvailableBase {
998998
@available(OSX, introduced: 10.53)
999999
class EsotericSmallBatchHipsterThing : WidelyAvailableBase {}
10001000

1001+
@available(OSX, introduced: 10.53)
1002+
class NestedClassTest {
1003+
class InnerClass : WidelyAvailableBase {}
1004+
}
1005+
10011006
// Useless #available(...) checks
10021007

10031008
func functionWithDefaultAvailabilityAndUselessCheck(_ p: Bool) {

test/Sema/availability_versions_multi.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ func useFromOtherOn10_51() {
3535
_ = o10_51.returns10_52Introduced10_52() // expected-error {{'returns10_52Introduced10_52()' is only available on OS X 10.52 or newer}}
3636
// expected-note@-1 {{add 'if #available' version check}}
3737

38-
_ = OtherIntroduced10_52() // expected-error {{'OtherIntroduced10_52' is only available on OS X 10.52 or newer}}
39-
// expected-note@-1 {{add 'if #available' version check}}
38+
_ = OtherIntroduced10_52()
39+
// expected-error@-1 {{'OtherIntroduced10_52' is only available on OS X 10.52 or newer}}
40+
// expected-note@-2 {{add 'if #available' version check}}
4041

4142
o10_51.extensionMethodOnOtherIntroduced10_51AvailableOn10_52() // expected-error {{'extensionMethodOnOtherIntroduced10_51AvailableOn10_52()' is only available on OS X 10.52 or newer}}
4243
// expected-note@-1 {{add 'if #available' version check}}
4344

44-
_ = OtherIntroduced10_51.NestedIntroduced10_52() // expected-error {{'NestedIntroduced10_52' is only available on OS X 10.52 or newer}}
45-
// expected-note@-1 {{add 'if #available' version check}}
45+
_ = OtherIntroduced10_51.NestedIntroduced10_52()
46+
// expected-error@-1 {{'NestedIntroduced10_52' is only available on OS X 10.52 or newer}}
47+
// expected-note@-2 {{add 'if #available' version check}}
4648
}
4749

4850
@available(OSX, introduced: 10.52)

0 commit comments

Comments
 (0)