Skip to content

[AST & Runtime] Correctly mangle extended existentials with inverse requirements #81365

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 5 commits into from
May 12, 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
7 changes: 1 addition & 6 deletions include/swift/ABI/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -2352,12 +2352,7 @@ struct TargetExtendedExistentialTypeShape
}

bool isCopyable() const {
if (!hasGeneralizationSignature()) {
return true;
}
auto *reqts = getGenSigRequirements();
for (unsigned i = 0, e = getNumGenSigRequirements(); i < e; ++i) {
auto &reqt = reqts[i];
for (auto &reqt : getRequirementSignature().getRequirements()) {
if (reqt.getKind() != GenericRequirementKind::InvertedProtocols) {
continue;
}
Expand Down
15 changes: 15 additions & 0 deletions include/swift/AST/ExistentialLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,24 @@ struct ExistentialLayout {

LayoutConstraint getLayoutConstraint() const;

/// Whether this layout has any inverses within its signature.
bool hasInverses() const {
return !inverses.empty();
}

/// Whether this existential needs to have an extended existential shape. This
/// is relevant for the mangler to mangle as a symbolic link where possible
/// and for IRGen directly emitting some existentials.
///
/// If 'allowInverses' is false, then regardless of if this existential layout
/// has inverse requirements those will not influence the need for having a
/// shape.
bool needsExtendedShape(bool allowInverses = true) const;

private:
SmallVector<ProtocolDecl *, 4> protocols;
SmallVector<ParameterizedProtocolType *, 4> parameterized;
InvertibleProtocolSet inverses;
};

}
Expand Down
11 changes: 6 additions & 5 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,7 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
// ExtendedExistentialTypeShapes consider existential metatypes to
// be part of the existential, so if we're symbolically referencing
// shapes, we need to handle that at this level.
if (EMT->hasParameterizedExistential()) {
if (EMT->getExistentialLayout().needsExtendedShape(AllowInverses)) {
auto referent = SymbolicReferent::forExtendedExistentialTypeShape(EMT);
if (canSymbolicReference(referent)) {
appendSymbolicExtendedExistentialType(referent, EMT, sig, forDecl);
Expand All @@ -1622,7 +1622,7 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
}

if (EMT->getInstanceType()->isExistentialType() &&
EMT->hasParameterizedExistential())
EMT->getExistentialLayout().needsExtendedShape(AllowInverses))
appendConstrainedExistential(EMT->getInstanceType(), sig, forDecl);
else
appendType(EMT->getInstanceType(), sig, forDecl);
Expand Down Expand Up @@ -1678,8 +1678,7 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
return appendType(strippedTy, sig, forDecl);
}

if (PCT->hasParameterizedExistential()
|| (PCT->hasInverse() && AllowInverses))
if (PCT->getExistentialLayout().needsExtendedShape(AllowInverses))
return appendConstrainedExistential(PCT, sig, forDecl);

// We mangle ProtocolType and ProtocolCompositionType using the
Expand All @@ -1693,7 +1692,8 @@ void ASTMangler::appendType(Type type, GenericSignature sig,

case TypeKind::Existential: {
auto *ET = cast<ExistentialType>(tybase);
if (ET->hasParameterizedExistential()) {

if (ET->getExistentialLayout().needsExtendedShape(AllowInverses)) {
auto referent = SymbolicReferent::forExtendedExistentialTypeShape(ET);
if (canSymbolicReference(referent)) {
appendSymbolicExtendedExistentialType(referent, ET, sig, forDecl);
Expand All @@ -1703,6 +1703,7 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
return appendConstrainedExistential(ET->getConstraintType(), sig,
forDecl);
}

return appendType(ET->getConstraintType(), sig, forDecl);
}

Expand Down
14 changes: 13 additions & 1 deletion lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ ExistentialLayout::ExistentialLayout(CanProtocolType type) {
!protoDecl->isMarkerProtocol());
representsAnyObject = false;

inverses = InvertibleProtocolSet();

protocols.push_back(protoDecl);
expandDefaults(protocols, InvertibleProtocolSet(), type->getASTContext());
}
Expand Down Expand Up @@ -353,7 +355,7 @@ ExistentialLayout::ExistentialLayout(CanProtocolCompositionType type) {
protocols.push_back(protoDecl);
}

auto inverses = type->getInverses();
inverses = type->getInverses();
expandDefaults(protocols, inverses, type->getASTContext());

representsAnyObject = [&]() {
Expand Down Expand Up @@ -433,6 +435,16 @@ Type ExistentialLayout::getSuperclass() const {
return Type();
}

bool ExistentialLayout::needsExtendedShape(bool allowInverses) const {
if (!getParameterizedProtocols().empty())
return true;

if (allowInverses && hasInverses())
return true;

return false;
}

bool TypeBase::isObjCExistentialType() {
return getCanonicalType().isObjCExistentialType();
}
Expand Down
4 changes: 3 additions & 1 deletion lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7852,8 +7852,10 @@ irgen::emitExtendedExistentialTypeShape(IRGenModule &IGM,

// You can have a superclass with a generic parameter pack in a composition,
// like `C<each T> & P<Int>`
assert(genHeader->GenericPackArguments.empty() &&
if (genSig) {
assert(genHeader->GenericPackArguments.empty() &&
"Generic parameter packs not supported here yet");
}

return b.finishAndCreateFuture();
}, [&](llvm::GlobalVariable *var) {
Expand Down
41 changes: 3 additions & 38 deletions lib/IRGen/MetadataRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,41 +264,6 @@ MetadataDependency MetadataDependencyCollector::finish(IRGenFunction &IGF) {
return result;
}

static bool usesExtendedExistentialMetadata(CanType type) {
auto layout = type.getExistentialLayout();
// If there are parameterized protocol types that we want to
// treat as equal to unparameterized protocol types (maybe
// something like `P<some Any>`?), then AST type canonicalization
// should turn them into unparameterized protocol types. If the
// structure makes it to IRGen, we have to honor that decision that
// they represent different types.
return !layout.getParameterizedProtocols().empty();
}

static std::optional<std::pair<CanExistentialType, /*depth*/ unsigned>>
usesExtendedExistentialMetadata(CanExistentialMetatypeType type) {
unsigned depth = 1;
auto cur = type.getInstanceType();
while (auto metatype = dyn_cast<ExistentialMetatypeType>(cur)) {
cur = metatype.getInstanceType();
depth++;
}

// The only existential types that don't currently use ExistentialType
// are Any and AnyObject, which don't use extended metadata.
if (usesExtendedExistentialMetadata(cur)) {
// HACK: The AST for an existential metatype of a (parameterized) protocol
// still directly wraps the existential type as its instance, which means
// we need to reconstitute the enclosing ExistentialType.
assert(cur->isExistentialType());
if (!cur->is<ExistentialType>()) {
cur = ExistentialType::get(cur)->getCanonicalType();
}
return std::make_pair(cast<ExistentialType>(cur), depth);
}
return std::nullopt;
}

llvm::Constant *IRGenModule::getAddrOfStringForMetadataRef(
StringRef symbolName,
unsigned alignment,
Expand Down Expand Up @@ -1998,7 +1963,7 @@ namespace {

// Existential metatypes for extended existentials don't use
// ExistentialMetatypeMetadata.
if (usesExtendedExistentialMetadata(type)) {
if (type->getExistentialLayout().needsExtendedShape()) {
auto metadata = emitExtendedExistentialTypeMetadata(type);
return setLocal(type, MetadataResponse::forComplete(metadata));
}
Expand Down Expand Up @@ -3110,8 +3075,8 @@ static bool shouldAccessByMangledName(IRGenModule &IGM, CanType type) {
void visitExistentialMetatypeType(CanExistentialMetatypeType meta) {
// Extended existential metatypes just emit a different shape
// and don't do any wrapping.
if (auto typeAndDepth = usesExtendedExistentialMetadata(meta)) {
return visit(typeAndDepth.first);
if (meta->getExistentialLayout().needsExtendedShape()) {
// return visit(unwrapExistentialMetatype(meta));
}

// The number of accesses turns out the same as the instance type,
Expand Down
71 changes: 64 additions & 7 deletions stdlib/public/runtime/Demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,69 @@ _buildDemanglingForNominalType(const Metadata *type, Demangle::Demangler &Dem) {
return _buildDemanglingForContext(description, demangledGenerics, Dem);
}

static Demangle::NodePointer
_replaceGeneralizationArg(Demangle::NodePointer type,
SubstGenericParametersFromMetadata substitutions,
Demangle::Demangler &Dem) {
assert(type->getKind() == Node::Kind::Type);
auto genericParam = type->getChild(0);

if (genericParam->getKind() != Node::Kind::DependentGenericParamType)
return type;

auto depth = genericParam->getChild(0)->getIndex();
auto index = genericParam->getChild(1)->getIndex();

auto arg = substitutions.getMetadata(depth, index);
assert(arg.isMetadata());
return _swift_buildDemanglingForMetadata(arg.getMetadata(), Dem);
}

static Demangle::NodePointer
_buildDemanglingForExtendedExistential(const Metadata *type,
Demangle::Demangler &Dem) {
auto ee = cast<ExtendedExistentialTypeMetadata>(type);

auto demangledExistential = Dem.demangleType(ee->Shape->ExistentialType.get(),
ResolveToDemanglingForContext(Dem));

if (!ee->Shape->hasGeneralizationSignature())
return demangledExistential;

SubstGenericParametersFromMetadata substitutions(ee->Shape,
ee->getGeneralizationArguments());

// Dig out the requirement list.
auto constrainedExistential = demangledExistential->getChild(0);
assert(constrainedExistential->getKind() == Node::Kind::ConstrainedExistential);
auto reqList = constrainedExistential->getChild(1);
assert(reqList->getKind() == Node::Kind::ConstrainedExistentialRequirementList);

auto newReqList = Dem.createNode(Node::Kind::ConstrainedExistentialRequirementList);

for (auto req : *reqList) {
// Currently, the only requirements that can create generalization arguments
// are same types.
if (req->getKind() != Node::Kind::DependentGenericSameTypeRequirement) {
newReqList->addChild(req, Dem);
continue;
}

auto lhs = _replaceGeneralizationArg(req->getChild(0), substitutions, Dem);
auto rhs = _replaceGeneralizationArg(req->getChild(1), substitutions, Dem);

auto newReq = Dem.createNode(Node::Kind::DependentGenericSameTypeRequirement);
newReq->addChild(lhs, Dem);
newReq->addChild(rhs, Dem);

newReqList->addChild(newReq, Dem);
}

constrainedExistential->replaceChild(1, newReqList);

return demangledExistential;
}

// Build a demangled type tree for a type.
//
// FIXME: This should use MetadataReader.h.
Expand Down Expand Up @@ -596,13 +659,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type,
return proto_list;
}
case MetadataKind::ExtendedExistential: {
// FIXME: Implement this by demangling the extended existential and
// substituting the generalization arguments into the demangle tree.
// For now, unconditional casts will report '<<< invalid type >>>' when
// they fail.
// TODO: for clients that need to guarantee round-tripping, demangle
// to a SymbolicExtendedExistentialType.
return nullptr;
return _buildDemanglingForExtendedExistential(type, Dem);
}
case MetadataKind::ExistentialMetatype: {
auto metatype = static_cast<const ExistentialMetatypeMetadata *>(type);
Expand Down
49 changes: 49 additions & 0 deletions test/Interpreter/extended_existential.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -O -target %target-cpu-apple-macos15.0 %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: OS=macosx
// REQUIRES: executable_test

protocol A<B>: ~Copyable {
associatedtype B
}

protocol B: ~Copyable {}

let a: Any = (any ~Copyable).self
// CHECK: any Any<Self: ~Swift.Copyable>
print(a)

let b: Any = (any ~Escapable).self
// CHECK: any Any<Self: ~Swift.Escapable>
print(b)

let c: Any = (any ~Copyable & ~Escapable).self
// CHECK: any Any<Self: ~Swift.Copyable, Self: ~Swift.Escapable>
print(c)

let d: Any = (any A).self
// CHECK: A
print(d)

let e: Any = (any B).self
// CHECK: B
print(e)

let f: Any = (any A & B).self
// CHECK: A & B
print(f)

let g: Any = (any A & ~Copyable).self
// CHECK: any A<Self: ~Swift.Copyable>
print(g)

let h: Any = (any B & ~Copyable).self
// CHECK: any B<Self: ~Swift.Copyable>
print(h)

let i: Any = (any A & B & ~Copyable).self
// CHECK: any A & B<Self: ~Swift.Copyable>
print(i)