Skip to content

Commit b6893e6

Browse files
committed
AST: A conformance is weak-linked if its protocol, conforming type or extension is @_weakLinked
Fixes <rdar://problem/46316197>.
1 parent 52561b4 commit b6893e6

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

include/swift/AST/ProtocolConformance.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ class RootProtocolConformance : public ProtocolConformance {
356356

357357
bool isInvalid() const;
358358

359+
/// Whether this conformance is weak-imported.
360+
bool isWeakImported(ModuleDecl *fromModule) const;
361+
359362
bool hasWitness(ValueDecl *requirement) const;
360363
Witness getWitness(ValueDecl *requirement, LazyResolver *resolver) const;
361364

lib/AST/ProtocolConformance.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,29 @@ SourceLoc RootProtocolConformance::getLoc() const {
392392
ROOT_CONFORMANCE_SUBCLASS_DISPATCH(getLoc, ())
393393
}
394394

395+
bool RootProtocolConformance::isWeakImported(ModuleDecl *fromModule) const {
396+
auto *dc = getDeclContext();
397+
if (dc->getParentModule() == fromModule)
398+
return false;
399+
400+
// If the protocol is weak imported, so are any conformances to it.
401+
if (getProtocol()->isWeakImported(fromModule))
402+
return true;
403+
404+
// If the conforming type is weak imported, so are any of its conformances.
405+
if (auto *nominal = getType()->getAnyNominal())
406+
if (nominal->isWeakImported(fromModule))
407+
return true;
408+
409+
// If the conformance is declared in an extension with the @_weakLinked
410+
// attribute, it is weak imported.
411+
if (auto *ext = dyn_cast<ExtensionDecl>(dc))
412+
if (ext->isWeakImported(fromModule))
413+
return true;
414+
415+
return false;
416+
}
417+
395418
bool RootProtocolConformance::hasWitness(ValueDecl *requirement) const {
396419
ROOT_CONFORMANCE_SUBCLASS_DISPATCH(hasWitness, (requirement))
397420
}

lib/IRGen/Linking.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,11 @@ bool LinkEntity::isWeakImported(ModuleDecl *module) const {
917917
case Kind::DynamicallyReplaceableFunctionImpl:
918918
return getDecl()->isWeakImported(module);
919919

920+
case Kind::ProtocolWitnessTable:
921+
case Kind::ProtocolConformanceDescriptor:
922+
return getProtocolConformance()->getRootConformance()
923+
->isWeakImported(module);
924+
920925
// TODO: Revisit some of the below, for weak conformances.
921926
case Kind::TypeMetadataPattern:
922927
case Kind::TypeMetadataInstantiationCache:
@@ -925,12 +930,10 @@ bool LinkEntity::isWeakImported(ModuleDecl *module) const {
925930
case Kind::TypeMetadataCompletionFunction:
926931
case Kind::ExtensionDescriptor:
927932
case Kind::AnonymousDescriptor:
928-
case Kind::ProtocolWitnessTable:
929933
case Kind::ProtocolWitnessTablePattern:
930934
case Kind::GenericProtocolWitnessTableInstantiationFunction:
931935
case Kind::AssociatedTypeWitnessTableAccessFunction:
932936
case Kind::ReflectionAssociatedTypeDescriptor:
933-
case Kind::ProtocolConformanceDescriptor:
934937
case Kind::ProtocolWitnessTableLazyAccessFunction:
935938
case Kind::ProtocolWitnessTableLazyCacheVariable:
936939
case Kind::ValueWitness:

test/IRGen/Inputs/weak_import_native_helper.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,6 @@ public protocol ProtocolWithWeakMembers {
118118
extension ProtocolWithWeakMembers {
119119
@_weakLinked public func f() {}
120120
}
121+
122+
public protocol BaseP {}
123+
@_weakLinked extension S : BaseP {}

test/IRGen/weak_import_native.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,8 @@ class WeakGenericSub: GenericC<Int> {
170170
// CHECK-DAG: declare extern_weak swiftcc {{.+}} @"$s25weak_import_native_helper8GenericCCfd"
171171
}
172172
}
173+
174+
protocol RefinesP : BaseP {}
175+
176+
// CHECK-DAG: @"$s25weak_import_native_helper1SVAA5BasePAAWP" = extern_weak global i8*
177+
extension S : RefinesP {}

0 commit comments

Comments
 (0)