@@ -2369,6 +2369,8 @@ class DeclDeserializer {
2369
2369
ASTContext &ctx;
2370
2370
Serialized<Decl *> &declOrOffset;
2371
2371
2372
+ bool IsInvalid = false ;
2373
+
2372
2374
DeclAttribute *DAttrs = nullptr ;
2373
2375
DeclAttribute **AttrsNext = &DAttrs;
2374
2376
@@ -2421,6 +2423,22 @@ class DeclDeserializer {
2421
2423
if (!decl)
2422
2424
return ;
2423
2425
2426
+ if (IsInvalid) {
2427
+ decl->setInvalidBit ();
2428
+
2429
+ DeclName name;
2430
+ if (auto *VD = dyn_cast<ValueDecl>(decl)) {
2431
+ name = VD->getName ();
2432
+ }
2433
+
2434
+ auto diagId = ctx.LangOpts .AllowModuleWithCompilerErrors
2435
+ ? diag::serialization_allowing_invalid_decl
2436
+ : diag::serialization_invalid_decl;
2437
+ ctx.Diags .diagnose (SourceLoc (), diagId, name,
2438
+ decl->getDescriptiveKind (),
2439
+ MF.getAssociatedModule ()->getNameStr ());
2440
+ }
2441
+
2424
2442
if (DAttrs)
2425
2443
decl->getAttrs ().setRawAttributeChain (DAttrs);
2426
2444
@@ -2436,10 +2454,9 @@ class DeclDeserializer {
2436
2454
}
2437
2455
}
2438
2456
2439
- // / Deserializes decl attribute and attribute-like records from
2440
- // / \c MF.DeclTypesCursor until a non-attribute record is found,
2441
- // / passing each one to AddAttribute.
2442
- llvm::Error deserializeDeclAttributes ();
2457
+ // / Deserializes records common to all decls from \c MF.DeclTypesCursor (ie.
2458
+ // / the invalid flag, attributes, and discriminators)
2459
+ llvm::Error deserializeDeclCommon ();
2443
2460
2444
2461
Expected<Decl *> getDeclCheckedImpl (
2445
2462
llvm::function_ref<bool (DeclAttributes)> matchAttributes = nullptr);
@@ -4083,19 +4100,6 @@ ModuleFile::getDeclChecked(
4083
4100
matchAttributes);
4084
4101
if (!deserialized)
4085
4102
return deserialized;
4086
-
4087
- auto *decl = declOrOffset.get ();
4088
- if (isAllowModuleWithCompilerErrorsEnabled () && decl->isInvalid ()) {
4089
- if (!isa<ParamDecl>(decl) && !decl->isImplicit ()) {
4090
- // The parent function will be invalid if the parameter is invalid,
4091
- // implicits should have an invalid explicit as well
4092
- if (auto *VD = dyn_cast<ValueDecl>(decl)) {
4093
- getContext ().Diags .diagnose (
4094
- VD->getLoc (), diag::serialization_allowing_invalid_decl,
4095
- VD->getName (), VD->getModuleContext ()->getNameStr ());
4096
- }
4097
- }
4098
- }
4099
4103
} else if (matchAttributes) {
4100
4104
// Decl was cached but we may need to filter it
4101
4105
if (!matchAttributes (declOrOffset.get ()->getAttrs ()))
@@ -4115,7 +4119,7 @@ ModuleFile::getDeclChecked(
4115
4119
return declOrOffset;
4116
4120
}
4117
4121
4118
- llvm::Error DeclDeserializer::deserializeDeclAttributes () {
4122
+ llvm::Error DeclDeserializer::deserializeDeclCommon () {
4119
4123
using namespace decls_block ;
4120
4124
4121
4125
SmallVector<uint64_t , 64 > scratch;
@@ -4132,7 +4136,10 @@ llvm::Error DeclDeserializer::deserializeDeclAttributes() {
4132
4136
unsigned recordID = MF.fatalIfUnexpected (
4133
4137
MF.DeclTypeCursor .readRecord (entry.ID , scratch, &blobData));
4134
4138
4135
- if (isDeclAttrRecord (recordID)) {
4139
+ if (recordID == ERROR_FLAG) {
4140
+ assert (!IsInvalid && " Error flag written multiple times" );
4141
+ IsInvalid = true ;
4142
+ } else if (isDeclAttrRecord (recordID)) {
4136
4143
DeclAttribute *Attr = nullptr ;
4137
4144
bool skipAttr = false ;
4138
4145
switch (recordID) {
@@ -4459,7 +4466,7 @@ llvm::Error DeclDeserializer::deserializeDeclAttributes() {
4459
4466
// because it requires `DifferentiableAttr::setOriginalDeclaration` to
4460
4467
// be called first. `DifferentiableAttr::setOriginalDeclaration` cannot
4461
4468
// be called here because the original declaration is not accessible in
4462
- // this function (`DeclDeserializer::deserializeDeclAttributes `).
4469
+ // this function (`DeclDeserializer::deserializeDeclCommon `).
4463
4470
diffAttrParamIndicesMap[diffAttr] = indices;
4464
4471
diffAttr->setDerivativeGenericSignature (derivativeGenSig);
4465
4472
Attr = diffAttr;
@@ -4616,9 +4623,9 @@ Expected<Decl *>
4616
4623
DeclDeserializer::getDeclCheckedImpl (
4617
4624
llvm::function_ref<bool (DeclAttributes)> matchAttributes) {
4618
4625
4619
- auto attrError = deserializeDeclAttributes ();
4620
- if (attrError )
4621
- return std::move (attrError );
4626
+ auto commonError = deserializeDeclCommon ();
4627
+ if (commonError )
4628
+ return std::move (commonError );
4622
4629
4623
4630
if (matchAttributes) {
4624
4631
// Deserialize the full decl only if matchAttributes finds a match.
@@ -5764,19 +5771,28 @@ class TypeDeserializer {
5764
5771
5765
5772
Expected<Type> deserializeErrorType (ArrayRef<uint64_t > scratch,
5766
5773
StringRef blobData) {
5767
- if (!MF.isAllowModuleWithCompilerErrorsEnabled ())
5768
- MF.fatal ();
5769
-
5770
5774
TypeID origID;
5771
5775
decls_block::ErrorTypeLayout::readRecord (scratch, origID);
5772
5776
5773
- auto origTy = MF.getTypeChecked (origID);
5774
- if (!origTy)
5775
- return origTy.takeError ();
5777
+ auto origTyOrError = MF.getTypeChecked (origID);
5778
+ if (!origTyOrError)
5779
+ return origTyOrError.takeError ();
5780
+
5781
+ auto origTy = *origTyOrError;
5782
+ auto diagId = ctx.LangOpts .AllowModuleWithCompilerErrors
5783
+ ? diag::serialization_allowing_error_type
5784
+ : diag::serialization_error_type;
5785
+ // Generally not a super useful diagnostic, so only output once if there
5786
+ // hasn't been any other diagnostic yet to ensure nothing slips by and
5787
+ // causes SILGen to crash.
5788
+ if (!ctx.hadError ()) {
5789
+ ctx.Diags .diagnose (SourceLoc (), diagId, StringRef (origTy.getString ()),
5790
+ MF.getAssociatedModule ()->getNameStr ());
5791
+ }
5776
5792
5777
- if (!origTy. get () )
5793
+ if (!origTy)
5778
5794
return ErrorType::get (ctx);
5779
- return ErrorType::get (origTy. get () );
5795
+ return ErrorType::get (origTy);
5780
5796
}
5781
5797
};
5782
5798
}
0 commit comments