Skip to content

[5.7] IRGen: Weakly link associated conformance descriptors when either the protocol or the associated requirement is weak #60075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/IRGen/Linking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1142,9 +1142,12 @@ bool LinkEntity::isWeakImported(ModuleDecl *module) const {

case Kind::AssociatedConformanceDescriptor:
case Kind::DefaultAssociatedConformanceAccessor: {
// Associated conformance descriptors use the protocol as
// their declaration, but are weak linked if the associated
// type stored in extra storage area is weak linked.
// Associated conformance descriptors use the protocol as their declaration
// and are weak linked if either the protocol or the associated type stored
// in extra storage area is weak linked.
if (cast<ProtocolDecl>(getDecl())->isWeakImported(module))
return true;

auto assocConformance = getAssociatedConformance();
auto *depMemTy = assocConformance.first->castTo<DependentMemberType>();
return depMemTy->getAssocType()->isWeakImported(module);
Expand Down
58 changes: 58 additions & 0 deletions test/IRGen/weak_import_associated_conformance_descriptor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-swift-frontend -emit-module -emit-module-path %t/Library.swiftmodule -parse-as-library %t/Library.swift -enable-library-evolution
// RUN: %target-swift-frontend -primary-file %t/Client.swift -I %t -emit-ir | %FileCheck %s

// REQUIRES: OS=macosx

//--- Library.swift

public protocol P { }
public protocol Q: P { }

public protocol StrongProtoWithAssoc {
associatedtype Assoc: P
}

public protocol StrongProtoWithWeakLinkedAssoc {
@_weakLinked associatedtype Assoc: P
}

@available(macOS 10.50, *)
public protocol WeakProtoWithAssoc {
associatedtype Assoc: P
}

@available(macOS 10.50, *)
public protocol WeakProtoWithStrongInheritedAssoc: StrongProtoWithAssoc where Self.Assoc: Q { }


//--- Client.swift

import Library

struct ConformsToP: P { }
struct ConformsToQ: Q { }

// CHECK: @"$s7Library20StrongProtoWithAssocP0E0AC_AA1PTn" = external global %swift.protocol_requirement, align 4
struct ConformsToStrongProtoWithAssoc: StrongProtoWithAssoc {
typealias Assoc = ConformsToP
}

// CHECK: @"$s7Library30StrongProtoWithWeakLinkedAssocP0G0AC_AA1PTn" = extern_weak global %swift.protocol_requirement, align 4
struct ConformsToStrongProtoWithWeakLinkedAssoc: StrongProtoWithWeakLinkedAssoc {
typealias Assoc = ConformsToP
}

// CHECK: @"$s7Library18WeakProtoWithAssocP0E0AC_AA1PTn" = extern_weak global %swift.protocol_requirement, align 4
@available(macOS 10.50, *)
struct ConformsToWeakProtoWithAssoc: WeakProtoWithAssoc {
typealias Assoc = ConformsToP
}

// CHECK: @"$s7Library33WeakProtoWithStrongInheritedAssocP0G0AA0ecdG0P_AA1QTn" = extern_weak global %swift.protocol_requirement, align 4
@available(macOS 10.50, *)
struct ConformsToWeakProtoWithStrongInheritedAssoc: WeakProtoWithStrongInheritedAssoc {
typealias Assoc = ConformsToQ
}