Skip to content

Commit d26bde7

Browse files
committed
Start building the runtime demangle tree for extended existentials
1 parent c165509 commit d26bde7

File tree

4 files changed

+114
-29
lines changed

4 files changed

+114
-29
lines changed

include/swift/ABI/Metadata.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,12 +2352,7 @@ struct TargetExtendedExistentialTypeShape
23522352
}
23532353

23542354
bool isCopyable() const {
2355-
if (!hasGeneralizationSignature()) {
2356-
return true;
2357-
}
2358-
auto *reqts = getGenSigRequirements();
2359-
for (unsigned i = 0, e = getNumGenSigRequirements(); i < e; ++i) {
2360-
auto &reqt = reqts[i];
2355+
for (auto &reqt : getRequirementSignature().getRequirements()) {
23612356
if (reqt.getKind() != GenericRequirementKind::InvertedProtocols) {
23622357
continue;
23632358
}

stdlib/public/runtime/Demangle.cpp

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,69 @@ _buildDemanglingForNominalType(const Metadata *type, Demangle::Demangler &Dem) {
465465
return _buildDemanglingForContext(description, demangledGenerics, Dem);
466466
}
467467

468+
static Demangle::NodePointer
469+
_replaceGeneralizationArg(Demangle::NodePointer type,
470+
SubstGenericParametersFromMetadata substitutions,
471+
Demangle::Demangler &Dem) {
472+
assert(type->getKind() == Node::Kind::Type);
473+
auto genericParam = type->getChild(0);
474+
475+
if (genericParam->getKind() != Node::Kind::DependentGenericParamType)
476+
return type;
477+
478+
auto depth = genericParam->getChild(0)->getIndex();
479+
auto index = genericParam->getChild(1)->getIndex();
480+
481+
auto arg = substitutions.getMetadata(depth, index);
482+
assert(arg.isMetadata());
483+
return _swift_buildDemanglingForMetadata(arg.getMetadata(), Dem);
484+
}
485+
486+
static Demangle::NodePointer
487+
_buildDemanglingForExtendedExistential(const Metadata *type,
488+
Demangle::Demangler &Dem) {
489+
auto ee = cast<ExtendedExistentialTypeMetadata>(type);
490+
491+
auto demangledExistential = Dem.demangleType(ee->Shape->ExistentialType.get(),
492+
ResolveToDemanglingForContext(Dem));
493+
494+
if (!ee->Shape->hasGeneralizationSignature())
495+
return demangledExistential;
496+
497+
SubstGenericParametersFromMetadata substitutions(ee->Shape,
498+
ee->getGeneralizationArguments());
499+
500+
// Dig out the requirement list.
501+
auto constrainedExistential = demangledExistential->getChild(0);
502+
assert(constrainedExistential->getKind() == Node::Kind::ConstrainedExistential);
503+
auto reqList = constrainedExistential->getChild(1);
504+
assert(reqList->getKind() == Node::Kind::ConstrainedExistentialRequirementList);
505+
506+
auto newReqList = Dem.createNode(Node::Kind::ConstrainedExistentialRequirementList);
507+
508+
for (auto req : *reqList) {
509+
// Currently, the only requirements that can create generalization arguments
510+
// are same types.
511+
if (req->getKind() != Node::Kind::DependentGenericSameTypeRequirement) {
512+
newReqList->addChild(req, Dem);
513+
continue;
514+
}
515+
516+
auto lhs = _replaceGeneralizationArg(req->getChild(0), substitutions, Dem);
517+
auto rhs = _replaceGeneralizationArg(req->getChild(1), substitutions, Dem);
518+
519+
auto newReq = Dem.createNode(Node::Kind::DependentGenericSameTypeRequirement);
520+
newReq->addChild(lhs, Dem);
521+
newReq->addChild(rhs, Dem);
522+
523+
newReqList->addChild(newReq, Dem);
524+
}
525+
526+
constrainedExistential->replaceChild(1, newReqList);
527+
528+
return demangledExistential;
529+
}
530+
468531
// Build a demangled type tree for a type.
469532
//
470533
// FIXME: This should use MetadataReader.h.
@@ -596,13 +659,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type,
596659
return proto_list;
597660
}
598661
case MetadataKind::ExtendedExistential: {
599-
// FIXME: Implement this by demangling the extended existential and
600-
// substituting the generalization arguments into the demangle tree.
601-
// For now, unconditional casts will report '<<< invalid type >>>' when
602-
// they fail.
603-
// TODO: for clients that need to guarantee round-tripping, demangle
604-
// to a SymbolicExtendedExistentialType.
605-
return nullptr;
662+
return _buildDemanglingForExtendedExistential(type, Dem);
606663
}
607664
case MetadataKind::ExistentialMetatype: {
608665
auto metatype = static_cast<const ExistentialMetatypeMetadata *>(type);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -O -target arm64-apple-macos15.0 %s -o %t/a.out
3+
// RUN: %target-codesign %t/a.out
4+
// RUN: %target-run %t/a.out | %FileCheck %s
5+
6+
// REQUIRES: OS=macosx
7+
// REQUIRES: executable_test
8+
9+
protocol A<B>: ~Copyable {
10+
associatedtype B
11+
}
12+
13+
protocol B: ~Copyable {}
14+
15+
let a: Any = (any ~Copyable).self
16+
// CHECK: any Any<Self: ~Swift.Copyable>
17+
print(a)
18+
19+
let b: Any = (any ~Escapable).self
20+
// CHECK: any Any<Self: ~Swift.Escapable>
21+
print(b)
22+
23+
let c: Any = (any ~Copyable & ~Escapable).self
24+
// CHECK: any Any<Self: ~Swift.Copyable, Self: ~Swift.Escapable>
25+
print(c)
26+
27+
let d: Any = (any A).self
28+
// CHECK: A
29+
print(d)
30+
31+
let e: Any = (any B).self
32+
// CHECK: B
33+
print(e)
34+
35+
let f: Any = (any A & B).self
36+
// CHECK: A & B
37+
print(f)
38+
39+
let g: Any = (any A & ~Copyable).self
40+
// CHECK: any A<Self: ~Swift.Copyable>
41+
print(g)
42+
43+
let h: Any = (any B & ~Copyable).self
44+
// CHECK: any B<Self: ~Swift.Copyable>
45+
print(h)
46+
47+
let i: Any = (any A & B & ~Copyable).self
48+
// CHECK: any A & B<Self: ~Swift.Copyable>
49+
print(i)

test/Interpreter/moveonly_existentials.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,3 @@ func mutate(_ b: inout any Boopable & ~Copyable) {
2828
borrow(S())
2929
var s = S() as any Boopable & ~Copyable
3030
mutate(&s)
31-
32-
let a: Any = (any ~Copyable).self
33-
// CHECK: << invalid type >>
34-
print(a)
35-
36-
let b: Any = (any ~Escapable).self
37-
// CHECK: << invalid type >>
38-
print(b)
39-
40-
let c: Any = (any ~Copyable & ~Escapable).self
41-
// CHECK: << invalid type >>
42-
print(c)
43-
44-
let d: Any = (any Boopable & ~Copyable).self
45-
// CHECK: << invalid type >>
46-
print(d)

0 commit comments

Comments
 (0)