Skip to content

Serialization: Recover from failure to deserialize inheritance clause entries #24912

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
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
42 changes: 24 additions & 18 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2301,13 +2301,23 @@ class swift::DeclDeserializer {
AttrsNext = Attr->getMutableNext();
};

void handleInherited(TypeDecl *nominal, ArrayRef<uint64_t> rawInheritedIDs) {
auto inheritedTypes = ctx.Allocate<TypeLoc>(rawInheritedIDs.size());
for_each(inheritedTypes, rawInheritedIDs,
[this](TypeLoc &tl, uint64_t rawID) {
tl = TypeLoc::withoutLoc(MF.getType(rawID));
});
nominal->setInherited(inheritedTypes);
void handleInherited(llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
ArrayRef<uint64_t> rawInheritedIDs) {
SmallVector<TypeLoc, 2> inheritedTypes;
for (auto rawID : rawInheritedIDs) {
auto maybeType = MF.getTypeChecked(rawID);
if (!maybeType) {
llvm::consumeError(maybeType.takeError());
continue;
}
inheritedTypes.push_back(TypeLoc::withoutLoc(MF.getType(rawID)));
}

auto inherited = ctx.AllocateCopy(inheritedTypes);
if (auto *typeDecl = decl.dyn_cast<TypeDecl *>())
typeDecl->setInherited(inherited);
else
decl.get<ExtensionDecl *>()->setInherited(inherited);
}

public:
Expand Down Expand Up @@ -3507,13 +3517,13 @@ class swift::DeclDeserializer {
GenericEnvironmentID genericEnvID;
TypeID rawTypeID;
uint8_t rawAccessLevel;
unsigned numConformances, numInheritedTypes;
unsigned numConformances, numInherited;
ArrayRef<uint64_t> rawInheritedAndDependencyIDs;

decls_block::EnumLayout::readRecord(scratch, nameID, contextID,
isImplicit, isObjC, genericEnvID,
rawTypeID, rawAccessLevel,
numConformances, numInheritedTypes,
numConformances, numInherited,
rawInheritedAndDependencyIDs);

auto DC = MF.getDeclContext(contextID);
Expand All @@ -3522,7 +3532,7 @@ class swift::DeclDeserializer {

Identifier name = MF.getIdentifier(nameID);
for (TypeID dependencyID :
rawInheritedAndDependencyIDs.slice(numInheritedTypes)) {
rawInheritedAndDependencyIDs.slice(numInherited)) {
auto dependency = MF.getTypeChecked(dependencyID);
if (!dependency) {
return llvm::make_error<TypeError>(
Expand Down Expand Up @@ -3557,8 +3567,8 @@ class swift::DeclDeserializer {

theEnum->computeType();

handleInherited(theEnum,
rawInheritedAndDependencyIDs.slice(0, numInheritedTypes));
auto rawInheritedIDs = rawInheritedAndDependencyIDs.slice(0, numInherited);
handleInherited(theEnum, rawInheritedIDs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just merged the class and struct versions for this, so you can switch to it if you want.


theEnum->setMemberLoader(&MF, MF.DeclTypeCursor.GetCurrentBitNo());
skipRecord(MF.DeclTypeCursor, decls_block::MEMBERS);
Expand Down Expand Up @@ -3816,12 +3826,8 @@ class swift::DeclDeserializer {
if (isImplicit)
extension->setImplicit();

auto inheritedTypes = ctx.Allocate<TypeLoc>(numInherited);
for_each(inheritedTypes, inheritedAndDependencyIDs.slice(0, numInherited),
[this](TypeLoc &tl, uint64_t rawID) {
tl = TypeLoc::withoutLoc(MF.getType(rawID));
});
extension->setInherited(inheritedTypes);
auto rawInheritedIDs = inheritedAndDependencyIDs.slice(0, numInherited);
handleInherited(extension, rawInheritedIDs);

extension->setMemberLoader(&MF, MF.DeclTypeCursor.GetCurrentBitNo());
skipRecord(MF.DeclTypeCursor, decls_block::MEMBERS);
Expand Down
60 changes: 60 additions & 0 deletions test/Serialization/Recovery/private-conformances.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// RUN: %empty-directory(%t)

// Build the protocol library and conforming library to include PrivateProtocol.
// RUN: %target-swift-frontend -emit-module -DPROTOCOL_LIB -DBEFORE %s -module-name protocol_lib -emit-module-path %t/protocol_lib.swiftmodule
// RUN: %target-swift-frontend -emit-module -DCONFORMS_LIB %s -module-name conforms_lib -emit-module-path %t/conforms_lib.swiftmodule -I %t
// RUN: %target-swift-frontend -emit-module -DINHERITS_LIB %s -module-name inherits_lib -emit-module-path %t/inherits_lib.swiftmodule -I %t

// Rebuild the protocol library to omit PrivateProtocol.
// RUN: %target-swift-frontend -emit-module -DPROTOCOL_LIB -DAFTER %s -module-name protocol_lib -emit-module-path %t/protocol_lib.swiftmodule

// The conforming library's types should still deserialize.
// RUN: %target-swift-ide-test -print-module -source-filename %s -D CONFORMS_LIB -module-to-print conforms_lib -I %t | %FileCheck %s

// Make sure we *cannot* recover from a missing inherited protocol.
// RUN: not --crash %target-swift-ide-test -print-module -source-filename %s -D INHERITS_LIB -module-to-print inherits_lib -I %t

#if PROTOCOL_LIB

#if BEFORE
public protocol PrivateProtocol {}
#endif

#elseif CONFORMS_LIB
import protocol_lib

public protocol PublicProtocol {}

// CHECK: class C1 : PublicProtocol {
public class C1 : PrivateProtocol, PublicProtocol {}

// CHECK: class C2 {
public class C2 {}

// CHECK: extension C2 : PublicProtocol {
extension C2 : PrivateProtocol, PublicProtocol {}

// CHECK: enum E1 : PublicProtocol {
public enum E1 : PrivateProtocol, PublicProtocol {}

// CHECK: enum E2 {
public enum E2 {}

// CHECK: extension E2 : PublicProtocol {
extension E2 : PrivateProtocol, PublicProtocol {}

// CHECK: struct S1 : PublicProtocol {
public struct S1 : PrivateProtocol, PublicProtocol {}

// CHECK: struct S2 {
public struct S2 {}

// CHECK: extension S2 : PublicProtocol {
extension S2 : PrivateProtocol, PublicProtocol {}

#elseif INHERITS_LIB
import protocol_lib

public protocol InheritsPrivateProtocol : PrivateProtocol {}

#endif