Skip to content

embedded: fix specialization of associated conformance entries in witness tables #79908

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
merged 3 commits into from
Mar 11, 2025
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
5 changes: 5 additions & 0 deletions SwiftCompilerSources/Sources/AST/Conformance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public struct Conformance: CustomStringConvertible, NoReflectionChildren {
assert(isSpecialized)
return SubstitutionMap(bridged: bridged.getSpecializedSubstitutions())
}

public func getAssociatedConformance(ofAssociatedType assocType: Type, to proto: ProtocolDecl) -> Conformance {
assert(isConcrete)
return bridged.getAssociatedConformance(assocType.bridged, proto.bridged).conformance
}
}

public struct ConformanceArray : RandomAccessCollection, CustomReflectable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,14 @@ func specializeWitnessTable(forConformance conformance: Conformance,
case .associatedType(let requirement, let witness):
let substType = witness.subst(with: conformance.specializedSubstitutions)
return .associatedType(requirement: requirement, witness: substType)
case .associatedConformance(let requirement, let proto, let witness):
if witness.isSpecialized {
specializeWitnessTable(forConformance: witness, errorLocation: errorLocation, context, notifyNewWitnessTable)
case .associatedConformance(let requirement, let proto, _):
let concreteAssociateConf = conformance.getAssociatedConformance(ofAssociatedType: requirement.type, to: proto)
if concreteAssociateConf.isSpecialized {
specializeWitnessTable(forConformance: concreteAssociateConf,
errorLocation: errorLocation,
context, notifyNewWitnessTable)
}
return .associatedConformance(requirement: requirement, protocol: proto, witness: witness)
return .associatedConformance(requirement: requirement, protocol: proto, witness: concreteAssociateConf)
}
}
let newWT = context.createWitnessTable(entries: newEntries,conformance: conformance,
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -3066,6 +3066,8 @@ struct BridgedConformance {
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance getGenericConformance() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance getInheritedConformance() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSubstitutionMap getSpecializedSubstitutions() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance getAssociatedConformance(BridgedASTType assocType,
BridgedDeclObj proto) const;
};

struct BridgedConformanceArray {
Expand Down
5 changes: 5 additions & 0 deletions include/swift/AST/ASTBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ BridgedSubstitutionMap BridgedConformance::getSpecializedSubstitutions() const {
return {specPC->getSubstitutionMap()};
}

BridgedConformance BridgedConformance::getAssociatedConformance(BridgedASTType assocType, BridgedDeclObj proto) const {
return {unbridged().getConcrete()->getAssociatedConformance(assocType.unbridged(),
proto.getAs<swift::ProtocolDecl>())};
}

BridgedConformance BridgedConformanceArray::getAt(SwiftInt index) const {
return pcArray.unbridged<swift::ProtocolConformanceRef>()[index];
}
Expand Down
2 changes: 1 addition & 1 deletion include/swift/IRGen/Linking.h
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ class LinkEntity {

static LinkEntity forProtocolWitnessTable(const ProtocolConformance *C) {
if (isEmbedded(C)) {
assert(C->getProtocol()->requiresClass());
ASSERT(C->getProtocol()->requiresClass());
}

LinkEntity entity;
Expand Down
22 changes: 20 additions & 2 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,13 @@ namespace {
}

void addAssociatedConformance(const AssociatedConformance &req) {
if (req.getAssociation()->getASTContext().LangOpts.hasFeature(Feature::Embedded) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to push this check up to SILWitnessVisitor, which already includes this check for example:

        // ObjC protocols do not have witnesses.
        if (!Lowering::TypeConverter::protocolRequiresWitnessTable(requirement))
          continue;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work because the FragileWitnessTableBuilder needs to pull the witness table entry before returning.

!req.getAssociatedRequirement()->requiresClass()) {
// If it's not a class protocol, the associated type can never be used to create
// an existential. Therefore this witness entry is never used at runtime
// in embedded swift.
return;
}
Entries.push_back(WitnessTableEntry::forAssociatedConformance(req));
}

Expand Down Expand Up @@ -1748,6 +1755,14 @@ class AccessorConformanceInfo : public ConformanceInfo {
(void)entry;
SILEntries = SILEntries.slice(1);

if (IGM.Context.LangOpts.hasFeature(Feature::Embedded) &&
!requirement.getAssociatedRequirement()->requiresClass()) {
// If it's not a class protocol, the associated type can never be used to create
// an existential. Therefore this witness entry is never used at runtime
// in embedded swift.
return;
}

auto associate =
ConformanceInContext.getAssociatedType(
requirement.getAssociation())->getCanonicalType();
Expand Down Expand Up @@ -1775,7 +1790,10 @@ class AccessorConformanceInfo : public ConformanceInfo {
if (IGM.Context.LangOpts.hasFeature(Feature::Embedded)) {
// In Embedded Swift associated-conformance entries simply point to the witness table
// of the associated conformance.
llvm::Constant *witnessEntry = IGM.getAddrOfWitnessTable(associatedConformance.getConcrete());
ProtocolConformanceRef assocConf =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for assocConf to differ from associatedConformance above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because ConformanceInContext is not the same as SILWT->getConformance().
(I tried that first and it didn't work).

SILWT->getConformance()->getAssociatedConformance(requirement.getAssociation(),
requirement.getAssociatedRequirement());
llvm::Constant *witnessEntry = IGM.getAddrOfWitnessTable(assocConf.getConcrete());
auto &schema = IGM.getOptions().PointerAuth
.ProtocolAssociatedTypeWitnessTableAccessFunctions;
Table.addSignedPointer(witnessEntry, schema, requirement);
Expand Down Expand Up @@ -3734,7 +3752,7 @@ llvm::Value *irgen::emitWitnessTableRef(IRGenFunction &IGF,

// In Embedded Swift, only class-bound wtables are allowed.
if (srcType->getASTContext().LangOpts.hasFeature(Feature::Embedded)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IGF.IGM.Context is cleaner IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. I will change it next time I work on this code

assert(proto->requiresClass());
ASSERT(proto->requiresClass());
}

assert(Lowering::TypeConverter::protocolRequiresWitnessTable(proto)
Expand Down
80 changes: 80 additions & 0 deletions test/embedded/existential-class-bound1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,82 @@ struct S: P2 {
}
}

protocol Q3 {
func bar()
}

protocol P3<T>: AnyObject {
associatedtype T: Q3

var t: T { get }

func foo()
}

extension P3 {
func foo() {
t.bar()
}
}

final class C3<T: Q3>: P3 {
var t: T


init(t: T) { self.t = t }
}

struct S3<I: BinaryInteger>: Q3 {
var x: I

func bar() {
print(x)
}
}

@inline(never)
func testP3() -> any P3 {
return C3(t: S3(x: 102))
}

protocol P4<T>: AnyObject {
associatedtype T: Q

var t: T { get }

func foo()
}

extension P4 {
func foo() {
t.bar()
}
}

final class C4<T: Q>: P4 {
var t: T


init(t: T) { self.t = t }
}

class K4<I: BinaryInteger>: Q {
var x: I

init(x: I) { self.x = x }

func bar() {
print(x)
}
}

@inline(never)
func testP4() -> any P4 {
return C4(t: K4(x: 437))
}



@main
struct Main {
static func main() {
Expand All @@ -181,6 +257,10 @@ struct Main {
// CHECK: Derived2.bar()
testConditionalConformance(t: S(i: 27))
// CHECK: 27
testP3().foo()
// CHECK: 102
testP4().foo()
// CHECK: 437
}
}