1818#include " swift/AST/ASTVisitor.h"
1919#include " swift/AST/AutoDiff.h"
2020#include " swift/AST/DiagnosticsCommon.h"
21+ #include " swift/AST/DiagnosticsSema.h"
2122#include " swift/AST/Expr.h"
2223#include " swift/AST/FileSystem.h"
2324#include " swift/AST/ForeignAsyncConvention.h"
@@ -3045,12 +3046,12 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
30453046 if (D->getResolvedMacro (const_cast <CustomAttr *>(theAttr)))
30463047 return ;
30473048
3048- auto typeID = S. addTypeRef (
3049- D->getResolvedCustomAttrType (const_cast <CustomAttr *>(theAttr))) ;
3050- if (!typeID && !S. allowCompilerErrors ()) {
3051- llvm::PrettyStackTraceString message ( " CustomAttr has no type " ) ;
3052- abort ();
3053- }
3049+ auto attrType =
3050+ D->getResolvedCustomAttrType (const_cast <CustomAttr *>(theAttr));
3051+ if (S. skipTypeIfInvalid (attrType, theAttr-> getTypeRepr ()))
3052+ return ;
3053+
3054+ auto typeID = S. addTypeRef (attrType);
30543055 CustomDeclAttrLayout::emitRecord (S.Out , S.ScratchRecord , abbrCode,
30553056 theAttr->isImplicit (),
30563057 typeID, theAttr->isArgUnsafe ());
@@ -3656,27 +3657,37 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
36563657 }
36573658 case PatternKind::Named: {
36583659 auto named = cast<NamedPattern>(pattern);
3660+ auto ty = getPatternType ();
3661+ if (S.skipTypeIfInvalid (ty, named->getLoc ()))
3662+ break ;
36593663
36603664 unsigned abbrCode = S.DeclTypeAbbrCodes [NamedPatternLayout::Code];
36613665 NamedPatternLayout::emitRecord (S.Out , S.ScratchRecord , abbrCode,
36623666 S.addDeclRef (named->getDecl ()),
3663- S.addTypeRef (getPatternType () ));
3667+ S.addTypeRef (ty ));
36643668 break ;
36653669 }
36663670 case PatternKind::Any: {
3671+ auto ty = getPatternType ();
3672+ if (S.skipTypeIfInvalid (ty, pattern->getLoc ()))
3673+ break ;
3674+
36673675 unsigned abbrCode = S.DeclTypeAbbrCodes [AnyPatternLayout::Code];
36683676 auto anyPattern = cast<AnyPattern>(pattern);
36693677 AnyPatternLayout::emitRecord (S.Out , S.ScratchRecord , abbrCode,
3670- S.addTypeRef (getPatternType () ),
3678+ S.addTypeRef (ty ),
36713679 anyPattern->isAsyncLet ());
36723680 break ;
36733681 }
36743682 case PatternKind::Typed: {
36753683 auto typed = cast<TypedPattern>(pattern);
3684+ auto ty = getPatternType ();
3685+ if (S.skipTypeIfInvalid (ty, typed->getTypeRepr ()))
3686+ break ;
36763687
36773688 unsigned abbrCode = S.DeclTypeAbbrCodes [TypedPatternLayout::Code];
36783689 TypedPatternLayout::emitRecord (S.Out , S.ScratchRecord , abbrCode,
3679- S.addTypeRef (getPatternType () ));
3690+ S.addTypeRef (ty ));
36803691 writePattern (typed->getSubPattern ());
36813692 break ;
36823693 }
@@ -4996,13 +5007,8 @@ void Serializer::writeASTBlockEntity(const Decl *D) {
49965007 PrettyStackTraceDecl trace (" serializing" , D);
49975008 assert (DeclsToSerialize.hasRef (D));
49985009
4999- if (D->isInvalid ()) {
5000- assert (allowCompilerErrors () &&
5001- " cannot create a module with an invalid decl" );
5002-
5003- if (canSkipWhenInvalid (D))
5004- return ;
5005- }
5010+ if (skipDeclIfInvalid (D))
5011+ return ;
50065012
50075013 BitOffset initialOffset = Out.GetCurrentBitNo ();
50085014 SWIFT_DEFER {
@@ -6780,6 +6786,11 @@ void Serializer::writeToStream(
67806786 S.writeInputBlock ();
67816787 S.writeSIL (SILMod, options.SerializeAllSIL );
67826788 S.writeAST (DC);
6789+
6790+ if (S.hadError )
6791+ S.getASTContext ().Diags .diagnose (SourceLoc (), diag::serialization_failed,
6792+ S.M );
6793+
67836794 if (!options.DisableCrossModuleIncrementalInfo && DepGraph) {
67846795 fine_grained_dependencies::writeFineGrainedDependencyGraph (
67856796 S.Out , *DepGraph, fine_grained_dependencies::Purpose::ForSwiftModule);
@@ -6793,6 +6804,48 @@ bool Serializer::allowCompilerErrors() const {
67936804 return getASTContext ().LangOpts .AllowModuleWithCompilerErrors ;
67946805}
67956806
6807+ bool Serializer::skipDeclIfInvalid (const Decl *decl) {
6808+ if (!decl->isInvalid ())
6809+ return false ;
6810+
6811+ if (allowCompilerErrors ())
6812+ return canSkipWhenInvalid (decl);
6813+
6814+ if (Options.EnableSerializationRemarks ) {
6815+ getASTContext ().Diags .diagnose (
6816+ decl->getLoc (), diag::serialization_skipped_invalid_decl, decl);
6817+ }
6818+
6819+ hadError = true ;
6820+ return true ;
6821+ }
6822+
6823+ bool Serializer::skipTypeIfInvalid (Type ty, TypeRepr *tyRepr) {
6824+ if ((ty && !ty->hasError ()) || allowCompilerErrors ())
6825+ return false ;
6826+
6827+ if (Options.EnableSerializationRemarks ) {
6828+ getASTContext ().Diags .diagnose (
6829+ tyRepr->getLoc (), diag::serialization_skipped_invalid_type, tyRepr);
6830+ }
6831+
6832+ hadError = true ;
6833+ return true ;
6834+ }
6835+
6836+ bool Serializer::skipTypeIfInvalid (Type ty, SourceLoc loc) {
6837+ if ((ty && !ty->hasError ()) || allowCompilerErrors ())
6838+ return false ;
6839+
6840+ if (Options.EnableSerializationRemarks ) {
6841+ getASTContext ().Diags .diagnose (
6842+ loc, diag::serialization_skipped_invalid_type_unknown_name);
6843+ }
6844+
6845+ hadError = true ;
6846+ return true ;
6847+ }
6848+
67966849void serialization::writeToStream (
67976850 raw_ostream &os, ModuleOrSourceFile DC, const SILModule *M,
67986851 const SerializationOptions &options,
0 commit comments