Skip to content

Commit b9a9758

Browse files
committed
[Serialization] Cleanup allow module with errors references
1 parent 01dd5a5 commit b9a9758

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());
@@ -5864,7 +5864,7 @@ class TypeDeserializer {
58645864
return origTyOrError.takeError();
58655865

58665866
auto origTy = *origTyOrError;
5867-
auto diagId = ctx.LangOpts.AllowModuleWithCompilerErrors
5867+
auto diagId = MF.allowCompilerErrors()
58685868
? diag::serialization_allowing_error_type
58695869
: diag::serialization_error_type;
58705870
// Generally not a super useful diagnostic, so only output once if there
@@ -5902,8 +5902,7 @@ Expected<Type> ModuleFile::getTypeChecked(TypeID TID) {
59025902

59035903
#ifndef NDEBUG
59045904
PrettyStackTraceType trace(getContext(), "deserializing", typeOrOffset.get());
5905-
if (typeOrOffset.get()->hasError() &&
5906-
!isAllowModuleWithCompilerErrorsEnabled()) {
5905+
if (typeOrOffset.get()->hasError() && !allowCompilerErrors()) {
59075906
typeOrOffset.get()->dump(llvm::errs());
59085907
llvm_unreachable("deserialization produced an invalid type "
59095908
"(rdar://problem/30382791)");
@@ -6364,7 +6363,7 @@ void ModuleFile::finishNormalConformance(NormalProtocolConformance *conformance,
63646363
auto isConformanceReq = [](const Requirement &req) {
63656364
return req.getKind() == RequirementKind::Conformance;
63666365
};
6367-
if (!isAllowModuleWithCompilerErrorsEnabled() &&
6366+
if (!allowCompilerErrors() &&
63686367
conformanceCount != llvm::count_if(proto->getRequirementSignature(),
63696368
isConformanceReq)) {
63706369
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
@@ -478,10 +478,14 @@ class ModuleFile
478478

479479
/// Whether this module is compiled while allowing errors
480480
/// ('-experimental-allow-module-with-compiler-errors').
481-
bool isAllowModuleWithCompilerErrorsEnabled() const {
481+
bool compiledAllowingCompilerErrors() const {
482482
return Core->Bits.IsAllowModuleWithCompilerErrorsEnabled;
483483
}
484484

485+
/// Whether currently allowing modules with compiler errors (ie.
486+
/// '-experimental-allow-module-with-compiler-errors' is currently enabled).
487+
bool allowCompilerErrors() const;
488+
485489
/// \c true if this module has incremental dependency information.
486490
bool hasIncrementalInfo() const { return Core->hasIncrementalInfo(); }
487491

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);
@@ -1015,7 +1013,7 @@ void Serializer::writeHeader(const SerializationOptions &options) {
10151013
Strategy.emit(ScratchRecord, unsigned(M->getResilienceStrategy()));
10161014
}
10171015

1018-
if (getASTContext().LangOpts.AllowModuleWithCompilerErrors) {
1016+
if (allowCompilerErrors()) {
10191017
options_block::IsAllowModuleWithCompilerErrorsEnabledLayout
10201018
AllowErrors(Out);
10211019
AllowErrors.emit(ScratchRecord);
@@ -1439,8 +1437,7 @@ void Serializer::writeASTBlockEntity(
14391437
using namespace decls_block;
14401438

14411439
// The conformance must be complete, or we can't serialize it.
1442-
assert(conformance->isComplete() ||
1443-
getASTContext().LangOpts.AllowModuleWithCompilerErrors);
1440+
assert(conformance->isComplete() || allowCompilerErrors());
14441441
assert(NormalConformancesToSerialize.hasRef(conformance));
14451442

14461443
auto protocol = conformance->getProtocol();
@@ -2853,7 +2850,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
28532850
// Retrieve the type of the pattern.
28542851
auto getPatternType = [&] {
28552852
if (!pattern->hasType()) {
2856-
if (S.getASTContext().LangOpts.AllowModuleWithCompilerErrors)
2853+
if (S.allowCompilerErrors())
28572854
return ErrorType::get(S.getASTContext());
28582855
llvm_unreachable("all nodes should have types");
28592856
}
@@ -3617,8 +3614,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
36173614
getRawStableDefaultArgumentKind(argKind),
36183615
defaultArgumentText);
36193616

3620-
if (interfaceType->hasError() &&
3621-
!S.getASTContext().LangOpts.AllowModuleWithCompilerErrors) {
3617+
if (interfaceType->hasError() && !S.allowCompilerErrors()) {
36223618
param->getDeclContext()->printContext(llvm::errs());
36233619
interfaceType->dump(llvm::errs());
36243620
llvm_unreachable("error in interface type of parameter");
@@ -3985,8 +3981,8 @@ void Serializer::writeASTBlockEntity(const Decl *D) {
39853981
}
39863982
};
39873983

3988-
assert(getASTContext().LangOpts.AllowModuleWithCompilerErrors ||
3989-
!D->isInvalid() && "cannot create a module with an invalid decl");
3984+
assert((allowCompilerErrors() || !D->isInvalid()) &&
3985+
"cannot create a module with an invalid decl");
39903986
if (isDeclXRef(D)) {
39913987
writeCrossReference(D);
39923988
return;
@@ -4148,7 +4144,7 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
41484144
void visitType(const TypeBase *) = delete;
41494145

41504146
void visitErrorType(const ErrorType *ty) {
4151-
if (S.getASTContext().LangOpts.AllowModuleWithCompilerErrors) {
4147+
if (S.allowCompilerErrors()) {
41524148
using namespace decls_block;
41534149
unsigned abbrCode = S.DeclTypeAbbrCodes[ErrorTypeLayout::Code];
41544150
ErrorTypeLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
@@ -5244,8 +5240,7 @@ static void collectInterestingNestedDeclarations(
52445240
if (!nominalParent) {
52455241
const DeclContext *DC = member->getDeclContext();
52465242
nominalParent = DC->getSelfNominalTypeDecl();
5247-
assert(nominalParent ||
5248-
nestedType->getASTContext().LangOpts.AllowModuleWithCompilerErrors &&
5243+
assert((nominalParent || S.allowCompilerErrors()) &&
52495244
"parent context is not a type or extension");
52505245
}
52515246
nestedTypeDecls[nestedType->getName()].push_back({
@@ -5531,6 +5526,10 @@ void Serializer::writeToStream(
55315526
S.writeToStream(os);
55325527
}
55335528

5529+
bool Serializer::allowCompilerErrors() const {
5530+
return getASTContext().LangOpts.AllowModuleWithCompilerErrors;
5531+
}
5532+
55345533
void swift::serializeToBuffers(
55355534
ModuleOrSourceFile DC, const SerializationOptions &options,
55365535
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>
@@ -3917,6 +3922,12 @@ int main(int argc, char *argv[]) {
39173922
options::ExplicitSwiftModuleMap;
39183923
InitInvok.getFrontendOptions().DisableImplicitModules = true;
39193924
}
3925+
3926+
if (options::AllowCompilerErrors) {
3927+
InitInvok.getFrontendOptions().AllowModuleWithCompilerErrors = true;
3928+
InitInvok.getLangOptions().AllowModuleWithCompilerErrors = true;
3929+
}
3930+
39203931
// Process the clang arguments last and allow them to override previously
39213932
// set options.
39223933
if (!CCArgs.empty()) {

0 commit comments

Comments
 (0)