Skip to content

Commit 06265dc

Browse files
committed
Sema: Correct availability for @inlinable global var accessors bodies.
The bodies of unavailable functions should be typechecked as if they would always run at the deployment target, even if they are `@inlinable`. This was not the case in the bodies of unavailable `@inlinable` global var accessor, though, because the availability of the accessor, rather than enclosing var, was being queried. The fix is to use `getSemanticUnavailableAttr()`. Resolves rdar://113642576
1 parent 204bdda commit 06265dc

File tree

2 files changed

+143
-26
lines changed

2 files changed

+143
-26
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ class TypeRefinementContextBuilder : private ASTWalker {
597597
// constrained to the deployment target. There's not much benefit to
598598
// checking these declarations at a lower availability version floor since
599599
// neither can be used by API clients.
600-
if (D->isSPI() || AvailableAttr::isUnavailable(D))
600+
if (D->isSPI() || D->getSemanticUnavailableAttr())
601601
return true;
602602

603603
return !::isExported(D);

test/attr/attr_inlinable_available.swift

Lines changed: 142 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@
1010

1111
// Primary execution of this test. Uses the default minimum inlining version,
1212
// which is the version when Swift was introduced.
13-
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-library-evolution -module-name Test -target %target-next-stable-abi-triple -target-min-inlining-version min
13+
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-library-evolution -parse-as-library -module-name Test -target %target-next-stable-abi-triple -target-min-inlining-version min
1414

1515

1616
// Check that `-library-level api` implies `-target-min-inlining-version min`
17-
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-library-evolution -module-name Test -target %target-next-stable-abi-triple -library-level api -require-explicit-availability=ignore
17+
// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-library-evolution -parse-as-library -module-name Test -target %target-next-stable-abi-triple -library-level api -require-explicit-availability=ignore
1818

1919

2020
// Check that these rules are only applied when requested and that at least some
2121
// diagnostics are not present without it.
22-
// RUN: not %target-typecheck-verify-swift -swift-version 5 -enable-library-evolution -module-name Test -target %target-next-stable-abi-triple 2>&1 | %FileCheck --check-prefix NON_MIN %s
22+
// RUN: not %target-typecheck-verify-swift -swift-version 5 -enable-library-evolution -parse-as-library -module-name Test -target %target-next-stable-abi-triple 2>&1 | %FileCheck --check-prefix NON_MIN %s
2323

2424

2525
// Check that -target-min-inlining-version overrides -library-level, allowing
2626
// library owners to disable this behavior for API libraries if needed.
27-
// RUN: not %target-typecheck-verify-swift -swift-version 5 -enable-library-evolution -module-name Test -target %target-next-stable-abi-triple -target-min-inlining-version target -library-level api -require-explicit-availability=ignore 2>&1 | %FileCheck --check-prefix NON_MIN %s
27+
// RUN: not %target-typecheck-verify-swift -swift-version 5 -enable-library-evolution -parse-as-library -module-name Test -target %target-next-stable-abi-triple -target-min-inlining-version target -library-level api -require-explicit-availability=ignore 2>&1 | %FileCheck --check-prefix NON_MIN %s
2828

2929

3030
// Check that we respect -target-min-inlining-version by cranking it up high
3131
// enough to suppress any possible errors.
32-
// RUN: %target-swift-frontend -typecheck -disable-objc-attr-requires-foundation-module %s -swift-version 5 -enable-library-evolution -module-name Test -target %target-next-stable-abi-triple -target-min-inlining-version 42.0
32+
// RUN: %target-swift-frontend -typecheck -disable-objc-attr-requires-foundation-module %s -swift-version 5 -enable-library-evolution -parse-as-library -module-name Test -target %target-next-stable-abi-triple -target-min-inlining-version 42.0
3333

3434

3535
// NON_MIN: error: expected error not produced
@@ -579,6 +579,143 @@ public func spiDeployedUseNoAvailable( // expected-note 3 {{add @available attri
579579
}
580580
}
581581

582+
// MARK: - @inlinable global property accessors
583+
584+
@inlinable public var inlinedNoAvailableGlobal: Any { // expected-note 3 {{add @available attribute to enclosing var}}
585+
_ = NoAvailable()
586+
_ = BeforeInliningTarget()
587+
_ = AtInliningTarget()
588+
_ = BetweenTargets() // expected-error {{'BetweenTargets' is only available in macOS 10.14.5 or newer; clients of 'Test' may have a lower deployment target}} expected-note {{add 'if #available' version check}}
589+
_ = AtDeploymentTarget() // expected-error {{'AtDeploymentTarget' is only available in macOS 10.15 or newer; clients of 'Test' may have a lower deployment target}} expected-note {{add 'if #available' version check}}
590+
_ = AfterDeploymentTarget() // expected-error {{'AfterDeploymentTarget' is only available in macOS 11 or newer}} expected-note {{add 'if #available'}}
591+
592+
if #available(macOS 11, *) {
593+
_ = AfterDeploymentTarget()
594+
}
595+
596+
return ()
597+
}
598+
599+
@inlinable public var inlinedNoAvailableGlobalExplicitGetter: Any { // expected-note 3 {{add @available attribute to enclosing var}}
600+
get {
601+
_ = NoAvailable()
602+
_ = BeforeInliningTarget()
603+
_ = AtInliningTarget()
604+
_ = BetweenTargets() // expected-error {{'BetweenTargets' is only available in macOS 10.14.5 or newer; clients of 'Test' may have a lower deployment target}} expected-note {{add 'if #available' version check}}
605+
_ = AtDeploymentTarget() // expected-error {{'AtDeploymentTarget' is only available in macOS 10.15 or newer; clients of 'Test' may have a lower deployment target}} expected-note {{add 'if #available' version check}}
606+
_ = AfterDeploymentTarget() // expected-error {{'AfterDeploymentTarget' is only available in macOS 11 or newer}} expected-note {{add 'if #available'}}
607+
608+
if #available(macOS 11, *) {
609+
_ = AfterDeploymentTarget()
610+
}
611+
612+
return ()
613+
}
614+
}
615+
616+
@available(macOS 10.15, *)
617+
@inlinable public var inlinedAtDeploymentTargetGlobal: Any {
618+
_ = NoAvailable()
619+
_ = BeforeInliningTarget()
620+
_ = AtInliningTarget()
621+
_ = BetweenTargets()
622+
_ = AtDeploymentTarget()
623+
_ = AfterDeploymentTarget() // expected-error {{'AfterDeploymentTarget' is only available in macOS 11 or newer}} expected-note {{add 'if #available'}}
624+
625+
if #available(macOS 11, *) {
626+
_ = AfterDeploymentTarget()
627+
}
628+
629+
return ()
630+
}
631+
632+
@inlinable public var inlinedGlobalGetterAtDeploymentTarget: Any { // expected-note {{add @available attribute to enclosing var}}
633+
@available(macOS 10.15, *)
634+
get {
635+
_ = NoAvailable()
636+
_ = BeforeInliningTarget()
637+
_ = AtInliningTarget()
638+
_ = BetweenTargets()
639+
_ = AtDeploymentTarget()
640+
_ = AfterDeploymentTarget() // expected-error {{'AfterDeploymentTarget' is only available in macOS 11 or newer}} expected-note {{add 'if #available'}}
641+
642+
if #available(macOS 11, *) {
643+
_ = AfterDeploymentTarget()
644+
}
645+
646+
return ()
647+
}
648+
}
649+
650+
@_spi(Private)
651+
@inlinable public var inlinedNoAvailableSPIGlobal: Any { // expected-note {{add @available attribute to enclosing var}}
652+
_ = NoAvailable()
653+
_ = BeforeInliningTarget()
654+
_ = AtInliningTarget()
655+
_ = BetweenTargets()
656+
_ = AtDeploymentTarget()
657+
_ = AfterDeploymentTarget() // expected-error {{'AfterDeploymentTarget' is only available in macOS 11 or newer}} expected-note {{add 'if #available'}}
658+
659+
if #available(macOS 11, *) {
660+
_ = AfterDeploymentTarget()
661+
}
662+
663+
return ()
664+
}
665+
666+
@inlinable public var inlinedNoAvailableGlobalSPISetter: Any { // expected-note {{add @available attribute to enclosing var}}
667+
get {
668+
fatalError()
669+
}
670+
@_spi(Private)
671+
set {
672+
_ = NoAvailable()
673+
_ = BeforeInliningTarget()
674+
_ = AtInliningTarget()
675+
_ = BetweenTargets()
676+
_ = AtDeploymentTarget()
677+
_ = AfterDeploymentTarget() // expected-error {{'AfterDeploymentTarget' is only available in macOS 11 or newer}} expected-note {{add 'if #available'}}
678+
679+
if #available(macOS 11, *) {
680+
_ = AfterDeploymentTarget()
681+
}
682+
}
683+
}
684+
685+
@available(macOS, unavailable)
686+
@inlinable public var inlinedUnavailableGlobal: Any {
687+
_ = NoAvailable()
688+
_ = BeforeInliningTarget()
689+
_ = AtInliningTarget()
690+
_ = BetweenTargets()
691+
_ = AtDeploymentTarget()
692+
_ = AfterDeploymentTarget() // expected-error {{'AfterDeploymentTarget' is only available in macOS 11 or newer}} expected-note {{add 'if #available'}}
693+
694+
if #available(macOS 11, *) {
695+
_ = AfterDeploymentTarget()
696+
}
697+
698+
return ()
699+
}
700+
701+
@inlinable public var inlinedNoAvailableGlobalUnavailableSetter: Any { // expected-note {{add @available attribute to enclosing var}}
702+
get {
703+
fatalError()
704+
}
705+
@available(macOS, unavailable)
706+
set {
707+
_ = NoAvailable()
708+
_ = BeforeInliningTarget()
709+
_ = AtInliningTarget()
710+
_ = BetweenTargets()
711+
_ = AtDeploymentTarget()
712+
_ = AfterDeploymentTarget() // expected-error {{'AfterDeploymentTarget' is only available in macOS 11 or newer}} expected-note {{add 'if #available'}}
713+
714+
if #available(macOS 11, *) {
715+
_ = AfterDeploymentTarget()
716+
}
717+
}
718+
}
582719

583720
// MARK: - @_alwaysEmitIntoClient functions
584721

@@ -1467,23 +1604,3 @@ public enum UnavailableEnumWithClasses {
14671604
public class InheritsAfterDeploymentTarget: AfterDeploymentTargetClass {} // expected-error {{'AfterDeploymentTargetClass' is only available in}} expected-note 2 {{add @available attribute to enclosing class}}
14681605
public class InheritsUnavailable: UnavailableClass {}
14691606
}
1470-
1471-
// MARK: - Top-level code
1472-
1473-
// Top-level code, if somehow present in a resilient module, is treated like
1474-
// a non-inlinable function.
1475-
defer {
1476-
_ = AtDeploymentTarget()
1477-
_ = AfterDeploymentTarget() // expected-error {{'AfterDeploymentTarget' is only available in macOS 11 or newer}} expected-note {{add 'if #available'}}
1478-
}
1479-
_ = NoAvailable()
1480-
_ = BeforeInliningTarget()
1481-
_ = AtInliningTarget()
1482-
_ = BetweenTargets()
1483-
_ = AtDeploymentTarget()
1484-
_ = AfterDeploymentTarget() // expected-error {{'AfterDeploymentTarget' is only available in macOS 11 or newer}} expected-note {{add 'if #available'}}
1485-
1486-
if #available(macOS 11, *) {
1487-
_ = AfterDeploymentTarget()
1488-
}
1489-

0 commit comments

Comments
 (0)