Skip to content

Commit e889eb0

Browse files
authored
Merge pull request swiftlang#9273 from slavapestov/small-reflection-fixes
Small reflection fixes
2 parents 77a85f8 + ab79816 commit e889eb0

File tree

8 files changed

+45
-12
lines changed

8 files changed

+45
-12
lines changed

include/swift/Reflection/ReflectionContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,11 @@ class ReflectionContext
376376
// solve the problem.
377377
while (!CaptureTypes.empty()) {
378378
const TypeRef *OrigCaptureTR = CaptureTypes[0];
379+
380+
// If we failed to demangle the capture type, we cannot proceed.
381+
if (OrigCaptureTR == nullptr)
382+
return nullptr;
383+
379384
const TypeRef *SubstCaptureTR = nullptr;
380385

381386
// If we have enough substitutions to make this captured value's

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
@@ -305,12 +305,16 @@ class RemoteASTTypeBuilder {
305305

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

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

stdlib/public/Reflection/TypeRef.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ struct TypeRefIsConcrete
255255
}
256256

257257
bool visitFunctionTypeRef(const FunctionTypeRef *F) {
258-
std::vector<TypeRef *> SubstitutedArguments;
259258
for (auto Argument : F->getArguments())
260259
if (!visit(Argument))
261260
return false;

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)