Skip to content

Commit 62db9d4

Browse files
committed
[Serialization] Cleanup allow module with errors references
1 parent bb9588e commit 62db9d4

File tree

7 files changed

+50
-31
lines changed

7 files changed

+50
-31
lines changed

lib/Serialization/Deserialization.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2512,7 +2512,7 @@ class DeclDeserializer {
25122512
name = VD->getName();
25132513
}
25142514

2515-
auto diagId = ctx.LangOpts.AllowModuleWithCompilerErrors
2515+
auto diagId = MF.allowCompilerErrors()
25162516
? diag::serialization_allowing_invalid_decl
25172517
: diag::serialization_invalid_decl;
25182518
ctx.Diags.diagnose(SourceLoc(), diagId, name,
@@ -3092,7 +3092,7 @@ class DeclDeserializer {
30923092
declOrOffset = param;
30933093

30943094
auto paramTy = MF.getType(interfaceTypeID);
3095-
if (paramTy->hasError() && !MF.isAllowModuleWithCompilerErrorsEnabled()) {
3095+
if (paramTy->hasError() && !MF.allowCompilerErrors()) {
30963096
// FIXME: This should never happen, because we don't serialize
30973097
// error types.
30983098
DC->printContext(llvm::errs());
@@ -5872,7 +5872,7 @@ class TypeDeserializer {
58725872
return origTyOrError.takeError();
58735873

58745874
auto origTy = *origTyOrError;
5875-
auto diagId = ctx.LangOpts.AllowModuleWithCompilerErrors
5875+
auto diagId = MF.allowCompilerErrors()
58765876
? diag::serialization_allowing_error_type
58775877
: diag::serialization_error_type;
58785878
// Generally not a super useful diagnostic, so only output once if there
@@ -5910,8 +5910,7 @@ Expected<Type> ModuleFile::getTypeChecked(TypeID TID) {
59105910

59115911
#ifndef NDEBUG
59125912
PrettyStackTraceType trace(getContext(), "deserializing", typeOrOffset.get());
5913-
if (typeOrOffset.get()->hasError() &&
5914-
!isAllowModuleWithCompilerErrorsEnabled()) {
5913+
if (typeOrOffset.get()->hasError() && !allowCompilerErrors()) {
59155914
typeOrOffset.get()->dump(llvm::errs());
59165915
llvm_unreachable("deserialization produced an invalid type "
59175916
"(rdar://problem/30382791)");
@@ -6372,7 +6371,7 @@ void ModuleFile::finishNormalConformance(NormalProtocolConformance *conformance,
63726371
auto isConformanceReq = [](const Requirement &req) {
63736372
return req.getKind() == RequirementKind::Conformance;
63746373
};
6375-
if (!isAllowModuleWithCompilerErrorsEnabled() &&
6374+
if (!allowCompilerErrors() &&
63766375
conformanceCount != llvm::count_if(proto->getRequirementSignature(),
63776376
isConformanceReq)) {
63786377
fatal(llvm::make_error<llvm::StringError>(

lib/Serialization/ModuleFile.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ ModuleFile::ModuleFile(std::shared_ptr<const ModuleFileSharedCore> core)
117117
allocateBuffer(Identifiers, core->Identifiers);
118118
}
119119

120+
bool ModuleFile::allowCompilerErrors() const {
121+
return getContext().LangOpts.AllowModuleWithCompilerErrors;
122+
}
123+
120124
Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
121125
bool recoverFromIncompatibility) {
122126
PrettyStackTraceModuleFile stackEntry(*this);

lib/Serialization/ModuleFile.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,14 @@ class ModuleFile
469469

470470
/// Whether this module is compiled while allowing errors
471471
/// ('-experimental-allow-module-with-compiler-errors').
472-
bool isAllowModuleWithCompilerErrorsEnabled() const {
472+
bool compiledAllowingCompilerErrors() const {
473473
return Core->Bits.IsAllowModuleWithCompilerErrorsEnabled;
474474
}
475475

476+
/// Whether currently allowing modules with compiler errors (ie.
477+
/// '-experimental-allow-module-with-compiler-errors' is currently enabled).
478+
bool allowCompilerErrors() const;
479+
476480
/// \c true if this module has incremental dependency information.
477481
bool hasIncrementalInfo() const { return Core->hasIncrementalInfo(); }
478482

lib/Serialization/Serialization.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ using namespace llvm::support;
7676
using swift::version::Version;
7777
using llvm::BCBlockRAII;
7878

79-
80-
ASTContext &SerializerBase::getASTContext() {
81-
return M->getASTContext();
82-
}
79+
ASTContext &SerializerBase::getASTContext() const { return M->getASTContext(); }
8380

8481
/// Used for static_assert.
8582
static constexpr bool declIDFitsIn32Bits() {
@@ -644,8 +641,9 @@ serialization::TypeID Serializer::addTypeRef(Type ty) {
644641

645642
#ifndef NDEBUG
646643
PrettyStackTraceType trace(M->getASTContext(), "serializing", typeToSerialize);
647-
assert(M->getASTContext().LangOpts.AllowModuleWithCompilerErrors ||
648-
!typeToSerialize || !typeToSerialize->hasError() && "serializing type with an error");
644+
assert((allowCompilerErrors() || !typeToSerialize ||
645+
!typeToSerialize->hasError()) &&
646+
"serializing type with an error");
649647
#endif
650648

651649
return TypesToSerialize.addRef(typeToSerialize);
@@ -995,7 +993,7 @@ void Serializer::writeHeader(const SerializationOptions &options) {
995993
Strategy.emit(ScratchRecord, unsigned(M->getResilienceStrategy()));
996994
}
997995

998-
if (getASTContext().LangOpts.AllowModuleWithCompilerErrors) {
996+
if (allowCompilerErrors()) {
999997
options_block::IsAllowModuleWithCompilerErrorsEnabledLayout
1000998
AllowErrors(Out);
1001999
AllowErrors.emit(ScratchRecord);
@@ -1419,8 +1417,7 @@ void Serializer::writeASTBlockEntity(
14191417
using namespace decls_block;
14201418

14211419
// The conformance must be complete, or we can't serialize it.
1422-
assert(conformance->isComplete() ||
1423-
getASTContext().LangOpts.AllowModuleWithCompilerErrors);
1420+
assert(conformance->isComplete() || allowCompilerErrors());
14241421
assert(NormalConformancesToSerialize.hasRef(conformance));
14251422

14261423
auto protocol = conformance->getProtocol();
@@ -2841,7 +2838,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
28412838
// Retrieve the type of the pattern.
28422839
auto getPatternType = [&] {
28432840
if (!pattern->hasType()) {
2844-
if (S.getASTContext().LangOpts.AllowModuleWithCompilerErrors)
2841+
if (S.allowCompilerErrors())
28452842
return ErrorType::get(S.getASTContext());
28462843
llvm_unreachable("all nodes should have types");
28472844
}
@@ -3605,8 +3602,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
36053602
getRawStableDefaultArgumentKind(argKind),
36063603
defaultArgumentText);
36073604

3608-
if (interfaceType->hasError() &&
3609-
!S.getASTContext().LangOpts.AllowModuleWithCompilerErrors) {
3605+
if (interfaceType->hasError() && !S.allowCompilerErrors()) {
36103606
param->getDeclContext()->printContext(llvm::errs());
36113607
interfaceType->dump(llvm::errs());
36123608
llvm_unreachable("error in interface type of parameter");
@@ -3973,8 +3969,8 @@ void Serializer::writeASTBlockEntity(const Decl *D) {
39733969
}
39743970
};
39753971

3976-
assert(getASTContext().LangOpts.AllowModuleWithCompilerErrors ||
3977-
!D->isInvalid() && "cannot create a module with an invalid decl");
3972+
assert((allowCompilerErrors() || !D->isInvalid()) &&
3973+
"cannot create a module with an invalid decl");
39783974
if (isDeclXRef(D)) {
39793975
writeCrossReference(D);
39803976
return;
@@ -4136,7 +4132,7 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
41364132
void visitType(const TypeBase *) = delete;
41374133

41384134
void visitErrorType(const ErrorType *ty) {
4139-
if (S.getASTContext().LangOpts.AllowModuleWithCompilerErrors) {
4135+
if (S.allowCompilerErrors()) {
41404136
using namespace decls_block;
41414137
unsigned abbrCode = S.DeclTypeAbbrCodes[ErrorTypeLayout::Code];
41424138
ErrorTypeLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
@@ -5232,8 +5228,7 @@ static void collectInterestingNestedDeclarations(
52325228
if (!nominalParent) {
52335229
const DeclContext *DC = member->getDeclContext();
52345230
nominalParent = DC->getSelfNominalTypeDecl();
5235-
assert(nominalParent ||
5236-
nestedType->getASTContext().LangOpts.AllowModuleWithCompilerErrors &&
5231+
assert((nominalParent || S.allowCompilerErrors()) &&
52375232
"parent context is not a type or extension");
52385233
}
52395234
nestedTypeDecls[nestedType->getName()].push_back({
@@ -5519,6 +5514,10 @@ void Serializer::writeToStream(
55195514
S.writeToStream(os);
55205515
}
55215516

5517+
bool Serializer::allowCompilerErrors() const {
5518+
return getASTContext().LangOpts.AllowModuleWithCompilerErrors;
5519+
}
5520+
55225521
void swift::serializeToBuffers(
55235522
ModuleOrSourceFile DC, const SerializationOptions &options,
55245523
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,

lib/Serialization/Serialization.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class SerializerBase {
7272
public:
7373
SerializerBase(ArrayRef<unsigned char> signature, ModuleOrSourceFile DC);
7474

75-
ASTContext &getASTContext();
75+
ASTContext &getASTContext() const;
7676
};
7777

7878
class Serializer : public SerializerBase {
@@ -538,6 +538,8 @@ class Serializer : public SerializerBase {
538538
/// Writes a set of generic requirements.
539539
void writeGenericRequirements(ArrayRef<Requirement> requirements,
540540
const std::array<unsigned, 256> &abbrCodes);
541+
542+
bool allowCompilerErrors() const;
541543
};
542544

543545
/// Serialize module documentation to the given stream.

test/SourceKit/Misc/load-module-with-errors.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func testInvalidTopLevelCompletion() {
7676
// RUN: %target-swift-frontend -merge-modules -emit-module -experimental-allow-module-with-compiler-errors %t/errors.a.swiftmodule %t/errors.b.swiftmodule %t/errors.c.swiftmodule -module-name errors -o %t/errors.swiftmodule
7777

7878
// Read the module back in to make sure it can be deserialized
79-
// RUN: %target-swift-ide-test -print-module -source-filename dummy -module-to-print errors -I %t | %FileCheck %s
79+
// RUN: %target-swift-ide-test -print-module -source-filename dummy -module-to-print errors -I %t -allow-compiler-errors | %FileCheck %s
8080
// CHECK: typealias InvalidAlias = <<error type>>
8181
// CHECK: class InvalidClass : <<error type>>, InvalidProtocol
8282
// CHECK: var classMemberA: <<error type>>
@@ -123,22 +123,22 @@ func testInvalidTopLevelCompletion() {
123123
// CHECK: func typeUsesFunc
124124

125125
// Check completions
126-
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t-completions -I %t
126+
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t-completions -I %t -allow-compiler-errors
127127

128128
// Check cursor info for the various symbols
129-
// RUN: %sourcekitd-test -req=cursor -pos=4:3 %s -- -I %t -target %target-triple %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
129+
// RUN: %sourcekitd-test -req=cursor -pos=4:3 %s -- -Xfrontend -experimental-allow-module-with-compiler-errors -I %t -target %target-triple %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
130130
// CHECK-GLOBAL: source.lang.swift.ref.var.global
131131
// CHECK-GLOBAL: invalidGlobalMissingInit
132132

133-
// RUN: %sourcekitd-test -req=cursor -pos=8:3 %s -- -I %t -target %target-triple %s | %FileCheck %s -check-prefix=CHECK-FUNC
133+
// RUN: %sourcekitd-test -req=cursor -pos=8:3 %s -- -Xfrontend -experimental-allow-module-with-compiler-errors -I %t -target %target-triple %s | %FileCheck %s -check-prefix=CHECK-FUNC
134134
// CHECK-FUNC: source.lang.swift.ref.function.free
135135
// CHECK-FUNC: invalidPartialFunc
136136

137-
// RUN: %sourcekitd-test -req=cursor -pos=12:12 %s -- -I %t -target %target-triple %s | %FileCheck %s -check-prefix=CHECK-STRUCT
137+
// RUN: %sourcekitd-test -req=cursor -pos=12:12 %s -- -Xfrontend -experimental-allow-module-with-compiler-errors -I %t -target %target-triple %s | %FileCheck %s -check-prefix=CHECK-STRUCT
138138
// CHECK-STRUCT: source.lang.swift.ref.struct
139139
// CHECK-STRUCT: InvalidStruct
140140

141141
// Currently doesn't work for any members with invalid types, even within the same module: rdar://71514163
142-
// RUN: %sourcekitd-test -req=cursor -pos=13:7 %s -- -I %t -target %target-triple %s | not %FileCheck %s -check-prefix=CHECK-MEMBER
142+
// RUN: %sourcekitd-test -req=cursor -pos=13:7 %s -- -Xfrontend -experimental-allow-module-with-compiler-errors -I %t -target %target-triple %s | not %FileCheck %s -check-prefix=CHECK-MEMBER
143143
// CHECK-MEMBER: source.lang.swift.ref.var.instance
144144
// CHECK-MEMBER: memberB

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,11 @@ static llvm::cl::list<std::string>
761761
AccessNotesPath("access-notes-path", llvm::cl::desc("Path to access notes file"),
762762
llvm::cl::cat(Category));
763763

764+
static llvm::cl::opt<bool>
765+
AllowCompilerErrors("allow-compiler-errors",
766+
llvm::cl::desc("Whether to attempt to continue despite compiler errors"),
767+
llvm::cl::init(false));
768+
764769
} // namespace options
765770

766771
static std::unique_ptr<llvm::MemoryBuffer>
@@ -3916,6 +3921,12 @@ int main(int argc, char *argv[]) {
39163921
options::ExplicitSwiftModuleMap;
39173922
InitInvok.getFrontendOptions().DisableImplicitModules = true;
39183923
}
3924+
3925+
if (options::AllowCompilerErrors) {
3926+
InitInvok.getFrontendOptions().AllowModuleWithCompilerErrors = true;
3927+
InitInvok.getLangOptions().AllowModuleWithCompilerErrors = true;
3928+
}
3929+
39193930
// Process the clang arguments last and allow them to override previously
39203931
// set options.
39213932
if (!CCArgs.empty()) {

0 commit comments

Comments
 (0)