Skip to content

Commit cb9c1f9

Browse files
committed
Sema: Allow @available on stored properties as long as its as new as the deployment target
1 parent 0bcfd22 commit cb9c1f9

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,12 +1471,6 @@ void AttributeChecker::visitAvailableAttr(AvailableAttr *attr) {
14711471

14721472
SourceLoc attrLoc = attr->getLocation();
14731473

1474-
Optional<Diag<>> MaybeNotAllowed =
1475-
TypeChecker::diagnosticIfDeclCannotBePotentiallyUnavailable(D);
1476-
if (MaybeNotAllowed.hasValue()) {
1477-
diagnose(attrLoc, MaybeNotAllowed.getValue());
1478-
}
1479-
14801474
// Find the innermost enclosing declaration with an availability
14811475
// range annotation and ensure that this attribute's available version range
14821476
// is fully contained within that declaration's range. If there is no such
@@ -1494,16 +1488,27 @@ void AttributeChecker::visitAvailableAttr(AvailableAttr *attr) {
14941488
EnclosingDecl = getEnclosingDeclForDecl(EnclosingDecl);
14951489
}
14961490

1497-
if (!EnclosingDecl)
1498-
return;
1499-
15001491
AvailabilityContext AttrRange{
15011492
VersionRange::allGTE(attr->Introduced.getValue())};
15021493

1503-
if (!AttrRange.isContainedIn(EnclosingAnnotatedRange.getValue())) {
1504-
diagnose(attr->getLocation(), diag::availability_decl_more_than_enclosing);
1505-
diagnose(EnclosingDecl->getLoc(),
1506-
diag::availability_decl_more_than_enclosing_enclosing_here);
1494+
if (EnclosingDecl) {
1495+
if (!AttrRange.isContainedIn(EnclosingAnnotatedRange.getValue())) {
1496+
diagnose(attr->getLocation(), diag::availability_decl_more_than_enclosing);
1497+
diagnose(EnclosingDecl->getLoc(),
1498+
diag::availability_decl_more_than_enclosing_enclosing_here);
1499+
}
1500+
}
1501+
1502+
Optional<Diag<>> MaybeNotAllowed =
1503+
TypeChecker::diagnosticIfDeclCannotBePotentiallyUnavailable(D);
1504+
if (MaybeNotAllowed.hasValue()) {
1505+
AvailabilityContext DeploymentRange
1506+
= AvailabilityContext::forDeploymentTarget(Ctx);
1507+
if (EnclosingAnnotatedRange.hasValue())
1508+
DeploymentRange.intersectWith(*EnclosingAnnotatedRange);
1509+
1510+
if (!DeploymentRange.isContainedIn(AttrRange))
1511+
diagnose(attrLoc, MaybeNotAllowed.getValue());
15071512
}
15081513
}
15091514

test/Sema/availability_stored.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// Code should type check with a new enough deployment target:
4+
// RUN: %target-swift-frontend -typecheck %s -target x86_64-apple-macos50
5+
6+
// REQUIRES: OS=macosx
7+
8+
@available(macOS 50, *)
9+
struct NewStruct {}
10+
11+
@available(macOS 50, *)
12+
struct GoodReference {
13+
var x: NewStruct
14+
}
15+
16+
@available(macOS 50, *)
17+
struct GoodNestedReference {
18+
struct Inner {
19+
var x: NewStruct
20+
}
21+
}
22+
23+
struct BadReference1 {
24+
// expected-error@+1 {{stored properties cannot be marked potentially unavailable with '@available'}}
25+
@available(macOS 50, *)
26+
var x: NewStruct
27+
}
28+
29+
@available(macOS 40, *)
30+
struct BadReference2 {
31+
// expected-error@+1 {{stored properties cannot be marked potentially unavailable with '@available'}}
32+
@available(macOS 50, *)
33+
var x: NewStruct
34+
}

test/Sema/availability_versions.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ class SubOfClassWithUnavailableInitializer : SuperWithWithUnavailableInitializer
309309
class ClassWithUnavailableProperties {
310310
// expected-note@-1 4{{add @available attribute to enclosing class}}
311311

312-
@available(OSX, introduced: 10.9) // expected-error {{stored properties cannot be marked potentially unavailable with '@available'}}
313312
var nonLazyAvailableOn10_9Stored: Int = 9
314313

315314
@available(OSX, introduced: 10.51) // expected-error {{stored properties cannot be marked potentially unavailable with '@available'}}

0 commit comments

Comments
 (0)