18
18
#include " swift/AST/ASTVisitor.h"
19
19
#include " swift/AST/AutoDiff.h"
20
20
#include " swift/AST/DiagnosticsCommon.h"
21
+ #include " swift/AST/DiagnosticsSema.h"
21
22
#include " swift/AST/Expr.h"
22
23
#include " swift/AST/FileSystem.h"
23
24
#include " swift/AST/ForeignAsyncConvention.h"
@@ -3045,12 +3046,12 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
3045
3046
if (D->getResolvedMacro (const_cast <CustomAttr *>(theAttr)))
3046
3047
return ;
3047
3048
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);
3054
3055
CustomDeclAttrLayout::emitRecord (S.Out , S.ScratchRecord , abbrCode,
3055
3056
theAttr->isImplicit (),
3056
3057
typeID, theAttr->isArgUnsafe ());
@@ -3656,27 +3657,37 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
3656
3657
}
3657
3658
case PatternKind::Named: {
3658
3659
auto named = cast<NamedPattern>(pattern);
3660
+ auto ty = getPatternType ();
3661
+ if (S.skipTypeIfInvalid (ty, named->getLoc ()))
3662
+ break ;
3659
3663
3660
3664
unsigned abbrCode = S.DeclTypeAbbrCodes [NamedPatternLayout::Code];
3661
3665
NamedPatternLayout::emitRecord (S.Out , S.ScratchRecord , abbrCode,
3662
3666
S.addDeclRef (named->getDecl ()),
3663
- S.addTypeRef (getPatternType () ));
3667
+ S.addTypeRef (ty ));
3664
3668
break ;
3665
3669
}
3666
3670
case PatternKind::Any: {
3671
+ auto ty = getPatternType ();
3672
+ if (S.skipTypeIfInvalid (ty, pattern->getLoc ()))
3673
+ break ;
3674
+
3667
3675
unsigned abbrCode = S.DeclTypeAbbrCodes [AnyPatternLayout::Code];
3668
3676
auto anyPattern = cast<AnyPattern>(pattern);
3669
3677
AnyPatternLayout::emitRecord (S.Out , S.ScratchRecord , abbrCode,
3670
- S.addTypeRef (getPatternType () ),
3678
+ S.addTypeRef (ty ),
3671
3679
anyPattern->isAsyncLet ());
3672
3680
break ;
3673
3681
}
3674
3682
case PatternKind::Typed: {
3675
3683
auto typed = cast<TypedPattern>(pattern);
3684
+ auto ty = getPatternType ();
3685
+ if (S.skipTypeIfInvalid (ty, typed->getTypeRepr ()))
3686
+ break ;
3676
3687
3677
3688
unsigned abbrCode = S.DeclTypeAbbrCodes [TypedPatternLayout::Code];
3678
3689
TypedPatternLayout::emitRecord (S.Out , S.ScratchRecord , abbrCode,
3679
- S.addTypeRef (getPatternType () ));
3690
+ S.addTypeRef (ty ));
3680
3691
writePattern (typed->getSubPattern ());
3681
3692
break ;
3682
3693
}
@@ -4996,13 +5007,8 @@ void Serializer::writeASTBlockEntity(const Decl *D) {
4996
5007
PrettyStackTraceDecl trace (" serializing" , D);
4997
5008
assert (DeclsToSerialize.hasRef (D));
4998
5009
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 ;
5006
5012
5007
5013
BitOffset initialOffset = Out.GetCurrentBitNo ();
5008
5014
SWIFT_DEFER {
@@ -6780,6 +6786,11 @@ void Serializer::writeToStream(
6780
6786
S.writeInputBlock ();
6781
6787
S.writeSIL (SILMod, options.SerializeAllSIL );
6782
6788
S.writeAST (DC);
6789
+
6790
+ if (S.hadError )
6791
+ S.getASTContext ().Diags .diagnose (SourceLoc (), diag::serialization_failed,
6792
+ S.M );
6793
+
6783
6794
if (!options.DisableCrossModuleIncrementalInfo && DepGraph) {
6784
6795
fine_grained_dependencies::writeFineGrainedDependencyGraph (
6785
6796
S.Out , *DepGraph, fine_grained_dependencies::Purpose::ForSwiftModule);
@@ -6793,6 +6804,48 @@ bool Serializer::allowCompilerErrors() const {
6793
6804
return getASTContext ().LangOpts .AllowModuleWithCompilerErrors ;
6794
6805
}
6795
6806
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
+
6796
6849
void serialization::writeToStream (
6797
6850
raw_ostream &os, ModuleOrSourceFile DC, const SILModule *M,
6798
6851
const SerializationOptions &options,
0 commit comments