Skip to content

Commit 1b37407

Browse files
committed
Fix MetadataReader handling of @objc protocols in existentials
1 parent e3efd00 commit 1b37407

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

include/swift/Remote/MetadataReader.h

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -498,20 +498,39 @@ class MetadataReader {
498498
#if SWIFT_OBJC_INTEROP
499499
// Check whether we have an Objective-C protocol.
500500
if (ProtocolAddress.isObjC()) {
501-
auto MangledNameStr =
501+
auto Name =
502502
readObjCProtocolName(ProtocolAddress.getObjCProtocol());
503+
StringRef NameStr(Name);
504+
505+
// If this is a Swift-defined protocol, demangle it.
506+
if (NameStr.startswith("_TtP")) {
507+
Demangle::Context DCtx;
508+
auto Demangled = DCtx.demangleSymbolAsNode(NameStr);
509+
if (!Demangled)
510+
return BuiltType();
511+
512+
// FIXME: This appears in _swift_buildDemanglingForMetadata().
513+
while (Demangled->getKind() == Node::Kind::Global ||
514+
Demangled->getKind() == Node::Kind::TypeMangling ||
515+
Demangled->getKind() == Node::Kind::Type ||
516+
Demangled->getKind() == Node::Kind::ProtocolList ||
517+
Demangled->getKind() == Node::Kind::TypeList ||
518+
Demangled->getKind() == Node::Kind::Type) {
519+
if (Demangled->getNumChildren() != 1)
520+
return BuiltType();
521+
Demangled = Demangled->getFirstChild();
522+
}
523+
524+
auto Protocol = Builder.createProtocolDecl(Demangled);
525+
if (!Protocol)
526+
return BuiltType();
527+
528+
Protocols.push_back(Protocol);
529+
continue;
530+
}
503531

504-
StringRef MangledName =
505-
Demangle::dropSwiftManglingPrefix(MangledNameStr);
506-
507-
Demangle::Context DCtx;
508-
auto Demangled = DCtx.demangleTypeAsNode(MangledName);
509-
if (!Demangled)
510-
return BuiltType();
511-
512-
// FIXME: Imported protocols?
513-
514-
auto Protocol = Builder.createProtocolDecl(Demangled);
532+
// Otherwise, this is an imported protocol.
533+
auto Protocol = Builder.createObjCProtocolDecl(NameStr);
515534
if (!Protocol)
516535
return BuiltType();
517536

@@ -520,6 +539,7 @@ class MetadataReader {
520539
}
521540
#endif
522541

542+
// Swift-native protocol.
523543
Demangle::Demangler Dem;
524544
auto Demangled = readDemanglingForContextDescriptor(
525545
ProtocolAddress.getSwiftProtocol(), Dem);

test/RemoteAST/objc_classes.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,19 @@ let observer = NSObject()
2626
a.addObserver(observer, forKeyPath: "property", options: [], context: nil)
2727
printDynamicType(a)
2828
// CHECK: A<Int>
29+
30+
// FIXME: The ... & AnyObject is redundant here:
31+
32+
printType(NSFastEnumeration.self)
33+
// CHECK: NSFastEnumeration & AnyObject
34+
35+
printType(Optional<NSFastEnumeration>.self)
36+
// CHECK: Optional<NSFastEnumeration & AnyObject>
37+
38+
@objc protocol OurObjCProtocol {}
39+
40+
printType(OurObjCProtocol.self)
41+
// CHECK: OurObjCProtocol & AnyObject
42+
43+
printType(Optional<OurObjCProtocol>.self)
44+
// CHECK: Optional<OurObjCProtocol & AnyObject>

0 commit comments

Comments
 (0)