Skip to content

Commit 3bf5a17

Browse files
committed
AST: Check the availability context from Decl::isWeakImported()
Fixes <rdar://problem/46674512>.
1 parent 69d1caf commit 3bf5a17

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed

lib/AST/Decl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,11 @@ bool Decl::isWeakImported(ModuleDecl *fromModule,
547547
if (containingModule == fromModule)
548548
return false;
549549

550+
auto containingContext =
551+
AvailabilityInference::availableRange(this, fromModule->getASTContext());
552+
if (!fromContext.isContainedIn(containingContext))
553+
return true;
554+
550555
if (getAttrs().hasAttribute<WeakLinkedAttr>())
551556
return true;
552557

@@ -563,7 +568,6 @@ bool Decl::isWeakImported(ModuleDecl *fromModule,
563568
if (auto *ntd = dyn_cast<NominalTypeDecl>(dc))
564569
return ntd->isWeakImported(fromModule, fromContext);
565570

566-
// FIXME: Also check availability when containingModule is resilient.
567571
return false;
568572
}
569573

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@available(macOS 10.50, *)
2+
public func conditionallyAvailableFunction() {}
3+
4+
@available(macOS 10.50, *)
5+
public var conditionallyAvailableGlobal: Int {
6+
get {return 0}
7+
set {}
8+
}
9+
10+
@available(macOS 10.50, *)
11+
public struct ConditionallyAvailableStruct {
12+
public func conditionallyAvailableMethod() {}
13+
}
14+
15+
public protocol AlwaysAvailableProtocol {}
16+
17+
public struct AlwaysAvailableStruct {}
18+
19+
@available(macOS 10.50, *)
20+
extension AlwaysAvailableStruct : AlwaysAvailableProtocol {}
21+
22+
public enum AlwaysAvailableEnum {
23+
case alwaysAvailableCase
24+
25+
@available(macOS 10.50, *)
26+
case conditionallyAvailableCase
27+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/weak_import_availability_helper.swiftmodule -parse-as-library %S/Inputs/weak_import_availability_helper.swift -enable-resilience
3+
//
4+
// RUN: %target-swift-frontend -primary-file %s -I %t -emit-ir | %FileCheck %s
5+
6+
// REQUIRES: OS=macosx
7+
8+
import weak_import_availability_helper
9+
10+
public func useConditionallyAvailableCase(e: AlwaysAvailableEnum) {
11+
switch e {
12+
case .alwaysAvailableCase: break
13+
case .conditionallyAvailableCase: break
14+
}
15+
}
16+
17+
// CHECK-LABEL: @"$s31weak_import_availability_helper19AlwaysAvailableEnumO013conditionallyF4CaseyA2CmFWC" = extern_weak constant i32
18+
19+
func useConformance<T : AlwaysAvailableProtocol>(_: T.Type) {}
20+
21+
@available(macOS 10.50, *)
22+
public func useConditionallyAvailableConformance() {
23+
useConformance(AlwaysAvailableStruct.self)
24+
}
25+
26+
// FIXME: We reference the witness table directly -- that's a bug since the module is resilient. Should reference the
27+
// conformance descriptor instead.
28+
29+
// CHECK-LABEL: @"$s31weak_import_availability_helper21AlwaysAvailableStructVAA0eF8ProtocolAAWP" = extern_weak global i8*
30+
31+
@available(macOS 10.50, *)
32+
public func callConditionallyAvailableFunction() {
33+
conditionallyAvailableFunction()
34+
}
35+
36+
// CHECK-LABEL: declare extern_weak swiftcc void @"$s31weak_import_availability_helper30conditionallyAvailableFunctionyyF"()
37+
38+
@available(macOS 10.50, *)
39+
public func useConditionallyAvailableGlobal() {
40+
_ = conditionallyAvailableGlobal
41+
conditionallyAvailableGlobal = 0
42+
conditionallyAvailableGlobal += 1
43+
}
44+
45+
// CHECK-LABEL: declare extern_weak swiftcc i64 @"$s31weak_import_availability_helper28conditionallyAvailableGlobalSivg"()
46+
47+
// CHECK-LABEL: declare extern_weak swiftcc void @"$s31weak_import_availability_helper28conditionallyAvailableGlobalSivs"(i64)
48+
49+
func blackHole<T>(_: T) {}
50+
51+
@available(macOS 10.50, *)
52+
public func useConditionallyAvailableStruct() {
53+
blackHole(ConditionallyAvailableStruct.self)
54+
}
55+
56+
// CHECK-LABEL: declare extern_weak swiftcc %swift.metadata_response @"$s31weak_import_availability_helper28ConditionallyAvailableStructVMa"(i64)
57+
58+
@available(macOS 10.50, *)
59+
public func useConditionallyAvailableMethod(s: ConditionallyAvailableStruct) {
60+
s.conditionallyAvailableMethod()
61+
}
62+
63+
// CHECK-LABEL: declare extern_weak swiftcc void @"$s31weak_import_availability_helper28ConditionallyAvailableStructV013conditionallyF6MethodyyF"(%swift.opaque* noalias nocapture swiftself)

test/IRGen/weak_import_native.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/weak_import_native_helper.swiftmodule %S/Inputs/weak_import_native_helper.swift -enable-resilience
2+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/weak_import_native_helper.swiftmodule -parse-as-library %S/Inputs/weak_import_native_helper.swift -enable-resilience
33
//
44
// RUN: %target-swift-frontend -primary-file %s -I %t -emit-ir | %FileCheck %s
55

0 commit comments

Comments
 (0)