Skip to content

Commit 55c37f0

Browse files
authored
Merge pull request #38482 from slavapestov/weak-link-opaque-type-descriptor
AST: Make sure that an opaque type descriptor is weak-linked if its naming declaration is
2 parents 0c6063f + 3affdd9 commit 55c37f0

5 files changed

+59
-0
lines changed

lib/AST/Decl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,9 @@ AvailabilityContext Decl::getAvailabilityForLinkage() const {
889889
if (auto *accessor = dyn_cast<AccessorDecl>(this))
890890
return accessor->getStorage()->getAvailabilityForLinkage();
891891

892+
if (auto *opaqueTypeDecl = dyn_cast<OpaqueTypeDecl>(this))
893+
return opaqueTypeDecl->getNamingDecl()->getAvailabilityForLinkage();
894+
892895
if (auto *ext = dyn_cast<ExtensionDecl>(this))
893896
if (auto *nominal = ext->getExtendedNominal())
894897
return nominal->getAvailabilityForLinkage();
@@ -914,6 +917,9 @@ bool Decl::isAlwaysWeakImported() const {
914917
if (auto *accessor = dyn_cast<AccessorDecl>(this))
915918
return accessor->getStorage()->isAlwaysWeakImported();
916919

920+
if (auto *opaqueTypeDecl = dyn_cast<OpaqueTypeDecl>(this))
921+
return opaqueTypeDecl->getNamingDecl()->isAlwaysWeakImported();
922+
917923
if (auto *ext = dyn_cast<ExtensionDecl>(this))
918924
if (auto *nominal = ext->getExtendedNominal())
919925
return nominal->isAlwaysWeakImported();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public protocol P {
2+
func blah()
3+
}
4+
5+
public struct S : P {
6+
public func blah() {}
7+
}
8+
9+
extension P {
10+
@available(macOS 100, *)
11+
@_weakLinked public func someAPI() -> some P { return S() }
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public protocol P {
2+
func blah()
3+
}
4+
5+
public struct S : P {
6+
public func blah() {}
7+
}
8+
9+
extension P {
10+
@_weakLinked public func someAPI() -> some P { return S() }
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/weak_availability_opaque_result_type_helper.swiftmodule -parse-as-library %S/Inputs/weak_availability_opaque_result_type_helper.swift -enable-library-evolution
3+
// RUN: %target-swift-frontend -disable-type-layout -primary-file %s -I %t -emit-ir | %FileCheck %s
4+
5+
// REQUIRES: OS=macosx
6+
7+
import weak_availability_opaque_result_type_helper
8+
9+
func useWeakImportedOpaqueResultType<T : P>(_ p: T) {
10+
if #available(macOS 100, *) {
11+
p.someAPI().blah()
12+
}
13+
}
14+
15+
// CHECK-LABEL: @"$s43weak_availability_opaque_result_type_helper1PPAAE7someAPIQryFQOMQ" = extern_weak global %swift.type_descriptor
16+
// CHECK-LABEL: declare extern_weak {{.+}} void @"$s43weak_availability_opaque_result_type_helper1PPAAE7someAPIQryF"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/weak_import_opaque_result_type_helper.swiftmodule -parse-as-library %S/Inputs/weak_import_opaque_result_type_helper.swift -enable-library-evolution -disable-availability-checking
3+
// RUN: %target-swift-frontend -disable-type-layout -disable-availability-checking -primary-file %s -I %t -emit-ir | %FileCheck %s
4+
5+
// UNSUPPORTED: OS=windows-msvc
6+
7+
import weak_import_opaque_result_type_helper
8+
9+
func useWeakImportedOpaqueResultType<T : P>(_ p: T) {
10+
p.someAPI().blah()
11+
}
12+
13+
// CHECK-LABEL: @"$s37weak_import_opaque_result_type_helper1PPAAE7someAPIQryFQOMQ" = extern_weak global %swift.type_descriptor
14+
// CHECK-LABEL: declare extern_weak {{.+}} void @"$s37weak_import_opaque_result_type_helper1PPAAE7someAPIQryF"

0 commit comments

Comments
 (0)