Skip to content

Commit 1be46be

Browse files
committed
Reflection: Fix crash when reflecting existential with fileprivate protocol member
Fixes <rdar://problem/31661662>, <rdar://problem/31668126>, and probably many other crashes with the same backtrace.
1 parent d4cd0da commit 1be46be

File tree

6 files changed

+40
-11
lines changed

6 files changed

+40
-11
lines changed

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class TypeRefBuilder {
210210

211211
const ProtocolTypeRef *createProtocolType(const std::string &mangledName,
212212
const std::string &moduleName,
213+
const std::string &privateDiscriminator,
213214
const std::string &name) {
214215
return ProtocolTypeRef::create(*this, mangledName);
215216
}

include/swift/Remote/MetadataReader.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,16 @@ class TypeDecoder {
172172
}
173173
case NodeKind::Protocol: {
174174
auto moduleName = Node->getChild(0)->getText();
175-
auto name = Node->getChild(1)->getText();
175+
auto nameNode = Node->getChild(1);
176+
std::string privateDiscriminator, name;
177+
if (nameNode->getKind() == NodeKind::PrivateDeclName) {
178+
privateDiscriminator = nameNode->getChild(0)->getText();
179+
name = nameNode->getChild(1)->getText();
180+
} else if (nameNode->getKind() == NodeKind::Identifier) {
181+
name = Node->getChild(1)->getText();
182+
} else {
183+
return BuiltType();
184+
}
176185

177186
// Consistent handling of protocols and protocol compositions
178187
Demangle::Demangler Dem;
@@ -184,7 +193,8 @@ class TypeDecoder {
184193
protocolList->addChild(typeList, Dem);
185194

186195
auto mangledName = Demangle::mangleNode(protocolList);
187-
return Builder.createProtocolType(mangledName, moduleName, name);
196+
return Builder.createProtocolType(mangledName, moduleName,
197+
privateDiscriminator, name);
188198
}
189199
case NodeKind::DependentGenericParamType: {
190200
auto depth = Node->getChild(0)->getIndex();

lib/RemoteAST/RemoteAST.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,16 @@ class RemoteASTTypeBuilder {
306306

307307
Type createProtocolType(StringRef mangledName,
308308
StringRef moduleName,
309-
StringRef protocolName) {
309+
StringRef privateDiscriminator,
310+
StringRef name) {
310311
auto module = Ctx.getModuleByName(moduleName);
311312
if (!module) return Type();
312313

313-
Identifier name = Ctx.getIdentifier(protocolName);
314-
auto decl = findNominalTypeDecl(module, name, Identifier(),
314+
auto decl = findNominalTypeDecl(module,
315+
Ctx.getIdentifier(name),
316+
(privateDiscriminator.empty()
317+
? Identifier()
318+
: Ctx.getIdentifier(privateDiscriminator)),
315319
Demangle::Node::Kind::Protocol);
316320
if (!decl) return Type();
317321

test/Reflection/Inputs/Protocols.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ public protocol P4 {
1919
public protocol ClassBoundP: class {
2020
associatedtype Inner
2121
}
22+
23+
fileprivate protocol FileprivateProtocol {}
24+
25+
public struct HasFileprivateProtocol {
26+
fileprivate let x: FileprivateProtocol
27+
}

test/Reflection/typeref_decoding.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,14 @@
579579
// CHECK: TypesToReflect.ClassBoundP
580580
// CHECK: --------------------------
581581

582+
// CHECK: TypesToReflect.(FileprivateProtocol in _{{[0-9A-F]+}})
583+
// CHECK: -------------------------------------------------------------------------
584+
585+
// CHECK: TypesToReflect.HasFileprivateProtocol
586+
// CHECK: -------------------------------------
587+
// CHECK: x: TypesToReflect.(FileprivateProtocol in _{{[0-9A-F]+}})
588+
// CHECK: (protocol TypesToReflect.(FileprivateProtocol in _{{[0-9A-F]+}}))
589+
582590
// CHECK: ASSOCIATED TYPES:
583591
// CHECK: =================
584592
// CHECK: - TypesToReflect.C1 : TypesToReflect.ClassBoundP

unittests/Reflection/TypeRef.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ TEST(TypeRefTest, UniqueFunctionTypeRef) {
156156
TEST(TypeRefTest, UniqueProtocolTypeRef) {
157157
TypeRefBuilder Builder;
158158

159-
auto P1 = Builder.createProtocolType(ABC, Module, Protocol);
160-
auto P2 = Builder.createProtocolType(ABC, Module, Protocol);
161-
auto P3 = Builder.createProtocolType(ABCD, Module, Shmrotocol);
162-
auto P4 = Builder.createProtocolType(XYZ, Shmodule, Protocol);
159+
auto P1 = Builder.createProtocolType(ABC, Module, "", Protocol);
160+
auto P2 = Builder.createProtocolType(ABC, Module, "", Protocol);
161+
auto P3 = Builder.createProtocolType(ABCD, Module, "", Shmrotocol);
162+
auto P4 = Builder.createProtocolType(XYZ, Shmodule, "", Protocol);
163163

164164
EXPECT_EQ(P1, P2);
165165
EXPECT_NE(P2, P3);
@@ -220,8 +220,8 @@ TEST(TypeRefTest, UniqueDependentMemberTypeRef) {
220220

221221
auto N1 = Builder.createNominalType(ABC, nullptr);
222222
auto N2 = Builder.createNominalType(XYZ, nullptr);
223-
auto P1 = Builder.createProtocolType(ABC, Module, Protocol);
224-
auto P2 = Builder.createProtocolType(ABCD, Shmodule, Protocol);
223+
auto P1 = Builder.createProtocolType(ABC, Module, "", Protocol);
224+
auto P2 = Builder.createProtocolType(ABCD, Shmodule, "", Protocol);
225225

226226
auto DM1 = Builder.createDependentMemberType("Index", N1, P1);
227227
auto DM2 = Builder.createDependentMemberType("Index", N1, P1);

0 commit comments

Comments
 (0)