Skip to content

Small reflection fixes #9273

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 3 commits into from
May 5, 2017
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
5 changes: 5 additions & 0 deletions include/swift/Reflection/ReflectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ class ReflectionContext
// solve the problem.
while (!CaptureTypes.empty()) {
const TypeRef *OrigCaptureTR = CaptureTypes[0];

// If we failed to demangle the capture type, we cannot proceed.
if (OrigCaptureTR == nullptr)
return nullptr;

const TypeRef *SubstCaptureTR = nullptr;

// If we have enough substitutions to make this captured value's
Expand Down
1 change: 1 addition & 0 deletions include/swift/Reflection/TypeRefBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class TypeRefBuilder {

const ProtocolTypeRef *createProtocolType(const std::string &mangledName,
const std::string &moduleName,
const std::string &privateDiscriminator,
const std::string &name) {
return ProtocolTypeRef::create(*this, mangledName);
}
Expand Down
14 changes: 12 additions & 2 deletions include/swift/Remote/MetadataReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,16 @@ class TypeDecoder {
}
case NodeKind::Protocol: {
auto moduleName = Node->getChild(0)->getText();
auto name = Node->getChild(1)->getText();
auto nameNode = Node->getChild(1);
std::string privateDiscriminator, name;
if (nameNode->getKind() == NodeKind::PrivateDeclName) {
privateDiscriminator = nameNode->getChild(0)->getText();
name = nameNode->getChild(1)->getText();
} else if (nameNode->getKind() == NodeKind::Identifier) {
name = Node->getChild(1)->getText();
} else {
return BuiltType();
}

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

auto mangledName = Demangle::mangleNode(protocolList);
return Builder.createProtocolType(mangledName, moduleName, name);
return Builder.createProtocolType(mangledName, moduleName,
privateDiscriminator, name);
}
case NodeKind::DependentGenericParamType: {
auto depth = Node->getChild(0)->getIndex();
Expand Down
10 changes: 7 additions & 3 deletions lib/RemoteAST/RemoteAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,16 @@ class RemoteASTTypeBuilder {

Type createProtocolType(StringRef mangledName,
StringRef moduleName,
StringRef protocolName) {
StringRef privateDiscriminator,
StringRef name) {
auto module = Ctx.getModuleByName(moduleName);
if (!module) return Type();

Identifier name = Ctx.getIdentifier(protocolName);
auto decl = findNominalTypeDecl(module, name, Identifier(),
auto decl = findNominalTypeDecl(module,
Ctx.getIdentifier(name),
(privateDiscriminator.empty()
? Identifier()
: Ctx.getIdentifier(privateDiscriminator)),
Demangle::Node::Kind::Protocol);
if (!decl) return Type();

Expand Down
1 change: 0 additions & 1 deletion stdlib/public/Reflection/TypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ struct TypeRefIsConcrete
}

bool visitFunctionTypeRef(const FunctionTypeRef *F) {
std::vector<TypeRef *> SubstitutedArguments;
for (auto Argument : F->getArguments())
if (!visit(Argument))
return false;
Expand Down
6 changes: 6 additions & 0 deletions test/Reflection/Inputs/Protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ public protocol P4 {
public protocol ClassBoundP: class {
associatedtype Inner
}

fileprivate protocol FileprivateProtocol {}

public struct HasFileprivateProtocol {
fileprivate let x: FileprivateProtocol
}
8 changes: 8 additions & 0 deletions test/Reflection/typeref_decoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,14 @@
// CHECK: TypesToReflect.ClassBoundP
// CHECK: --------------------------

// CHECK: TypesToReflect.(FileprivateProtocol in _{{[0-9A-F]+}})
// CHECK: -------------------------------------------------------------------------

// CHECK: TypesToReflect.HasFileprivateProtocol
// CHECK: -------------------------------------
// CHECK: x: TypesToReflect.(FileprivateProtocol in _{{[0-9A-F]+}})
// CHECK: (protocol TypesToReflect.(FileprivateProtocol in _{{[0-9A-F]+}}))

// CHECK: ASSOCIATED TYPES:
// CHECK: =================
// CHECK: - TypesToReflect.C1 : TypesToReflect.ClassBoundP
Expand Down
12 changes: 6 additions & 6 deletions unittests/Reflection/TypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ TEST(TypeRefTest, UniqueFunctionTypeRef) {
TEST(TypeRefTest, UniqueProtocolTypeRef) {
TypeRefBuilder Builder;

auto P1 = Builder.createProtocolType(ABC, Module, Protocol);
auto P2 = Builder.createProtocolType(ABC, Module, Protocol);
auto P3 = Builder.createProtocolType(ABCD, Module, Shmrotocol);
auto P4 = Builder.createProtocolType(XYZ, Shmodule, Protocol);
auto P1 = Builder.createProtocolType(ABC, Module, "", Protocol);
auto P2 = Builder.createProtocolType(ABC, Module, "", Protocol);
auto P3 = Builder.createProtocolType(ABCD, Module, "", Shmrotocol);
auto P4 = Builder.createProtocolType(XYZ, Shmodule, "", Protocol);

EXPECT_EQ(P1, P2);
EXPECT_NE(P2, P3);
Expand Down Expand Up @@ -220,8 +220,8 @@ TEST(TypeRefTest, UniqueDependentMemberTypeRef) {

auto N1 = Builder.createNominalType(ABC, nullptr);
auto N2 = Builder.createNominalType(XYZ, nullptr);
auto P1 = Builder.createProtocolType(ABC, Module, Protocol);
auto P2 = Builder.createProtocolType(ABCD, Shmodule, Protocol);
auto P1 = Builder.createProtocolType(ABC, Module, "", Protocol);
auto P2 = Builder.createProtocolType(ABCD, Shmodule, "", Protocol);

auto DM1 = Builder.createDependentMemberType("Index", N1, P1);
auto DM2 = Builder.createDependentMemberType("Index", N1, P1);
Expand Down