Skip to content

Commit 4aa516a

Browse files
committed
SILGen: Fix if #available for unavailable custom domains in zippered modules.
When generating SIL for an `if #available(SomeDomain)` query in code being compiled for a zippered target, the generated code was mis-compiled if `SomeDomain` were disabled at compile time. Empty version ranges need to be handled explicitly by `SILGenFunction::emitZipperedOSVersionRangeCheck()`. SILGen still miscompiles `if #unavailable` queries generally in code compiled for a zippered target (rdar://147929876). Resolves rdar://150888941.
1 parent 1e403ec commit 4aa516a

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

lib/SILGen/SILGenDecl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,13 @@ SILValue SILGenFunction::emitZipperedOSVersionRangeCheck(
18511851
return B.createIntegerLiteral(loc, i1, true);
18521852
}
18531853

1854+
// If either version is "never" then the check is trivially false because it
1855+
// can never succeed.
1856+
if (OSVersion.isEmpty() || VariantOSVersion.isEmpty()) {
1857+
SILType i1 = SILType::getBuiltinIntegerType(1, getASTContext());
1858+
return B.createIntegerLiteral(loc, i1, false);
1859+
}
1860+
18541861
// The variant-only availability-checking entrypoint is not part
18551862
// of the Swift 5.0 ABI. It is only available in macOS 10.15 and above.
18561863
bool isVariantEntrypointAvailable = !TargetTriple.isMacOSXVersionLT(10, 15);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// RUN: %target-swift-emit-irgen -module-name Test %s -verify \
2+
// RUN: -enable-experimental-feature CustomAvailability \
3+
// RUN: -define-enabled-availability-domain EnabledDomain \
4+
// RUN: -define-disabled-availability-domain DisabledDomain \
5+
// RUN: -target %target-cpu-apple-macosx13 \
6+
// RUN: -target-variant %target-cpu-apple-ios16-macabi \
7+
// RUN: -Onone | %FileCheck %s --check-prefixes=CHECK
8+
9+
// RUN: %target-swift-emit-irgen -module-name Test %s -verify \
10+
// RUN: -enable-experimental-feature CustomAvailability \
11+
// RUN: -define-enabled-availability-domain EnabledDomain \
12+
// RUN: -define-disabled-availability-domain DisabledDomain \
13+
// RUN: -target %target-cpu-apple-macosx13 \
14+
// RUN: -target-variant %target-cpu-apple-ios16-macabi \
15+
// RUN: -O | %FileCheck %s --check-prefixes=CHECK
16+
17+
// REQUIRES: OS=macosx || OS=maccatalyst
18+
// REQUIRES: swift_feature_CustomAvailability
19+
20+
@_silgen_name("always")
21+
public func always()
22+
23+
@_silgen_name("never")
24+
public func never()
25+
26+
// CHECK-NOT: call swiftcc void @never()
27+
28+
// CHECK: call swiftcc void @always()
29+
// CHECK-NOT: call swiftcc void @never()
30+
if #available(EnabledDomain) {
31+
always()
32+
} else {
33+
never()
34+
}
35+
36+
// CHECK: call swiftcc void @always()
37+
// CHECK-NOT: call swiftcc void @never()
38+
if #available(DisabledDomain) {
39+
never()
40+
} else {
41+
always()
42+
}
43+
44+
// FIXME: [availability] These CHECK lines for if #unavailable are inverted (rdar://147929876)
45+
// CHECK-NOT: call swiftcc void @always()
46+
// CHECK: call swiftcc void @never()
47+
if #unavailable(EnabledDomain) {
48+
never()
49+
} else {
50+
always()
51+
}
52+
53+
// CHECK-NOT: call swiftcc void @always()
54+
// CHECK: call swiftcc void @never()
55+
if #unavailable(DisabledDomain) {
56+
always()
57+
} else {
58+
never()
59+
}

0 commit comments

Comments
 (0)