Skip to content

Commit 16de339

Browse files
committed
AST: Enable -unavailable-code-optimization for zippered libraries.
Correct the determination of whether a declaration is unreachable at runtime when compiling a zippered library. Resolves rdar://125930716.
1 parent b9e1cbd commit 16de339

File tree

7 files changed

+64
-44
lines changed

7 files changed

+64
-44
lines changed

include/swift/AST/AvailabilityDomain.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,16 @@ class AvailabilityDomain final {
154154
return AvailabilityDomain(domain);
155155
}
156156

157-
/// Returns the most specific platform domain that applies to the compilation
158-
/// context.
157+
/// Returns the most specific platform domain for the target of the
158+
/// compilation context.
159159
static std::optional<AvailabilityDomain>
160160
forTargetPlatform(const ASTContext &ctx);
161161

162+
/// Returns the most specific platform domain for the target variant of the
163+
/// compilation context.
164+
static std::optional<AvailabilityDomain>
165+
forTargetVariantPlatform(const ASTContext &ctx);
166+
162167
/// Returns the built-in availability domain identified by the given string.
163168
static std::optional<AvailabilityDomain>
164169
builtinDomainForString(StringRef string, const DeclContext *declContext);

include/swift/AST/PlatformKind.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ bool isPlatformActive(PlatformKind Platform, const LangOptions &LangOpts,
8383
/// Returns the target platform for the given language options.
8484
PlatformKind targetPlatform(const LangOptions &LangOpts);
8585

86+
/// Returns the target variant platform for the given language options.
87+
PlatformKind targetVariantPlatform(const LangOptions &LangOpts);
88+
8689
/// Returns true when availability attributes from the "parent" platform
8790
/// should also apply to the "child" platform for declarations without
8891
/// an explicit attribute for the child.

lib/AST/Availability.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,9 @@ availabilityDomainsForABICompatibility(const ASTContext &ctx) {
642642

643643
if (auto targetDomain = AvailabilityDomain::forTargetPlatform(ctx))
644644
domains.push_back(targetDomain->getABICompatibilityDomain());
645+
646+
if (auto variantDomain = AvailabilityDomain::forTargetVariantPlatform(ctx))
647+
domains.push_back(variantDomain->getABICompatibilityDomain());
645648

646649
return domains;
647650
}
@@ -708,13 +711,6 @@ static bool isUnavailableForAllABICompatiblePlatforms(const Decl *decl) {
708711
if (availableDescendantDomains.size() > 0)
709712
return false;
710713

711-
// FIXME: [availability] Support zippered frameworks (rdar://125371621)
712-
// If we have a target variant (e.g. we're building a zippered macOS
713-
// framework) then the decl is only unreachable if it is unavailable for both
714-
// the primary target and the target variant.
715-
if (decl->getASTContext().LangOpts.TargetVariant.has_value())
716-
return false;
717-
718714
return true;
719715
}
720716

lib/AST/AvailabilityDomain.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ AvailabilityDomain::forTargetPlatform(const ASTContext &ctx) {
2727
return forPlatform(platform);
2828
}
2929

30+
std::optional<AvailabilityDomain>
31+
AvailabilityDomain::forTargetVariantPlatform(const ASTContext &ctx) {
32+
auto platform = swift::targetVariantPlatform(ctx.LangOpts);
33+
if (platform == PlatformKind::none)
34+
return std::nullopt;
35+
36+
return forPlatform(platform);
37+
}
38+
3039
std::optional<AvailabilityDomain>
3140
AvailabilityDomain::builtinDomainForString(StringRef string,
3241
const DeclContext *declContext) {

lib/AST/PlatformKind.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -181,44 +181,58 @@ bool swift::isPlatformActive(PlatformKind Platform, const LangOptions &LangOpts,
181181
LangOpts.EnableAppExtensionRestrictions, ForRuntimeQuery);
182182
}
183183

184-
PlatformKind swift::targetPlatform(const LangOptions &LangOpts) {
185-
if (LangOpts.Target.isMacOSX()) {
186-
return (LangOpts.EnableAppExtensionRestrictions
184+
static PlatformKind platformForTriple(const llvm::Triple &triple,
185+
bool enableAppExtensionRestrictions) {
186+
if (triple.isMacOSX()) {
187+
return (enableAppExtensionRestrictions
187188
? PlatformKind::macOSApplicationExtension
188189
: PlatformKind::macOS);
189190
}
190191

191-
if (LangOpts.Target.isTvOS()) {
192-
return (LangOpts.EnableAppExtensionRestrictions
193-
? PlatformKind::tvOSApplicationExtension
194-
: PlatformKind::tvOS);
192+
if (triple.isTvOS()) {
193+
return (enableAppExtensionRestrictions
194+
? PlatformKind::tvOSApplicationExtension
195+
: PlatformKind::tvOS);
195196
}
196197

197-
if (LangOpts.Target.isWatchOS()) {
198-
return (LangOpts.EnableAppExtensionRestrictions
199-
? PlatformKind::watchOSApplicationExtension
200-
: PlatformKind::watchOS);
198+
if (triple.isWatchOS()) {
199+
return (enableAppExtensionRestrictions
200+
? PlatformKind::watchOSApplicationExtension
201+
: PlatformKind::watchOS);
201202
}
202203

203-
if (LangOpts.Target.isiOS()) {
204-
if (tripleIsMacCatalystEnvironment(LangOpts.Target))
205-
return (LangOpts.EnableAppExtensionRestrictions
204+
if (triple.isiOS()) {
205+
if (tripleIsMacCatalystEnvironment(triple))
206+
return (enableAppExtensionRestrictions
206207
? PlatformKind::macCatalystApplicationExtension
207208
: PlatformKind::macCatalyst);
208-
return (LangOpts.EnableAppExtensionRestrictions
209+
return (enableAppExtensionRestrictions
209210
? PlatformKind::iOSApplicationExtension
210211
: PlatformKind::iOS);
211212
}
212213

213-
if (LangOpts.Target.isXROS()) {
214-
return (LangOpts.EnableAppExtensionRestrictions
215-
? PlatformKind::visionOSApplicationExtension
216-
: PlatformKind::visionOS);
214+
if (triple.isXROS()) {
215+
return (enableAppExtensionRestrictions
216+
? PlatformKind::visionOSApplicationExtension
217+
: PlatformKind::visionOS);
217218
}
218219

219220
return PlatformKind::none;
220221
}
221222

223+
PlatformKind swift::targetPlatform(const LangOptions &LangOpts) {
224+
return platformForTriple(LangOpts.Target,
225+
LangOpts.EnableAppExtensionRestrictions);
226+
}
227+
228+
PlatformKind swift::targetVariantPlatform(const LangOptions &LangOpts) {
229+
if (auto variant = LangOpts.TargetVariant)
230+
return platformForTriple(*LangOpts.TargetVariant,
231+
LangOpts.EnableAppExtensionRestrictions);
232+
233+
return PlatformKind::none;
234+
}
235+
222236
bool swift::inheritsAvailabilityFromPlatform(PlatformKind Child,
223237
PlatformKind Parent) {
224238
if (auto ChildPlatformBase = basePlatformForExtensionPlatform(Child)) {

test/SILGen/unavailable_decl_optimization_stub_macos_zippered.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@ public func unavailableOnMacOSFunc() {}
2121
@available(iOS, unavailable)
2222
public func unavailableOniOSFunc() {}
2323

24-
// FIXME: This function should diagnose (rdar://125930716)
2524
// CHECK-LABEL: sil{{.*}}@$s4Test28unavailableOnMacCatalystFuncyyF
2625
// CHECK-NOT: _diagnoseUnavailableCodeReached
2726
// CHECK: } // end sil function '$s4Test28unavailableOnMacCatalystFuncyyF'
2827
@available(macCatalyst, unavailable)
2928
public func unavailableOnMacCatalystFunc() {}
3029

31-
// FIXME: This function should diagnose (rdar://125930716)
3230
// CHECK-LABEL: sil{{.*}}@$s4Test28unavailableOnMacOSAndiOSFuncyyF
33-
// CHECK-NOT: _diagnoseUnavailableCodeReached
31+
// CHECK: [[FNREF:%.*]] = function_ref @$ss31_diagnoseUnavailableCodeReacheds5NeverOyFTwb : $@convention(thin) () -> Never
32+
// CHECK-NEXT: [[APPLY:%.*]] = apply [[FNREF]]()
3433
// CHECK: } // end sil function '$s4Test28unavailableOnMacOSAndiOSFuncyyF'
3534
@available(macOS, unavailable)
3635
@available(iOS, unavailable)
@@ -43,7 +42,7 @@ public struct UnavailableOniOS {
4342
// CHECK: } // end sil function '$s4Test16UnavailableOniOSV15availableMethodyyF'
4443
public func availableMethod() {}
4544

46-
// FIXME: This function should diagnose (rdar://125930716)
45+
// FIXME: [availability] This method should diagnose
4746
// CHECK-LABEL: sil{{.*}}@$s4Test16UnavailableOniOSV24unavailableOnMacOSMethodyyF
4847
// CHECK-NOT: _diagnoseUnavailableCodeReached
4948
// CHECK: } // end sil function '$s4Test16UnavailableOniOSV24unavailableOnMacOSMethodyyF'

test/decl/enum/derived_hashable_equatable_macos_zippered.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ enum HasElementsWithAvailability: Hashable {
4747
// CHECK-NEXT: index_a = 2
4848
// CHECK-NEXT: case .unavailableMacCatalyst:
4949
// CHECK-NEXT: index_a = 3
50-
// FIXME: This case should diagnose (rdar://125930716)
5150
// CHECK-NEXT: case .unavailableMacOSAndiOS:
52-
// CHECK-NEXT: index_a = 4
53-
// FIXME: This case should diagnose (rdar://125930716)
51+
// CHECK-NEXT: _diagnoseUnavailableCodeReached{{.*}}
5452
// CHECK-NEXT: case .unavailableMacOSAndMacCatalyst:
55-
// CHECK-NEXT: index_a = 5
53+
// CHECK-NEXT: index_a = 4
5654
// CHECK-NEXT: }
5755
// CHECK-NEXT: var index_b: Int
5856
// CHECK-NEXT: switch b {
@@ -66,12 +64,10 @@ enum HasElementsWithAvailability: Hashable {
6664
// CHECK-NEXT: index_b = 2
6765
// CHECK-NEXT: case .unavailableMacCatalyst:
6866
// CHECK-NEXT: index_b = 3
69-
// FIXME: This case should diagnose (rdar://125930716)
7067
// CHECK-NEXT: case .unavailableMacOSAndiOS:
71-
// CHECK-NEXT: index_b = 4
72-
// FIXME: This case should diagnose (rdar://125930716)
68+
// CHECK-NEXT: _diagnoseUnavailableCodeReached{{.*}}
7369
// CHECK-NEXT: case .unavailableMacOSAndMacCatalyst:
74-
// CHECK-NEXT: index_b = 5
70+
// CHECK-NEXT: index_b = 4
7571
// CHECK-NEXT: }
7672
// CHECK-NEXT: return index_a == index_b
7773
// CHECK-NEXT: }
@@ -89,12 +85,10 @@ enum HasElementsWithAvailability: Hashable {
8985
// CHECK-NEXT: discriminator = 2
9086
// CHECK-NEXT: case .unavailableMacCatalyst:
9187
// CHECK-NEXT: discriminator = 3
92-
// FIXME: This case should diagnose (rdar://125930716)
9388
// CHECK-NEXT: case .unavailableMacOSAndiOS:
94-
// CHECK-NEXT: discriminator = 4
95-
// FIXME: This case should diagnose (rdar://125930716)
89+
// CHECK-NEXT: _diagnoseUnavailableCodeReached{{.*}}
9690
// CHECK-NEXT: case .unavailableMacOSAndMacCatalyst:
97-
// CHECK-NEXT: discriminator = 5
91+
// CHECK-NEXT: discriminator = 4
9892
// CHECK-NEXT: }
9993
// CHECK-NEXT: hasher.combine(discriminator)
10094
// CHECK-NEXT: }

0 commit comments

Comments
 (0)