@@ -527,7 +527,10 @@ ModuleFile::readConformanceChecked(llvm::BitstreamCursor &Cursor,
527
527
" reading specialized conformance for" ,
528
528
conformingType);
529
529
530
- auto subMap = getSubstitutionMap (substitutionMapID);
530
+ auto subMapOrError = getSubstitutionMapChecked (substitutionMapID);
531
+ if (!subMapOrError)
532
+ return subMapOrError.takeError ();
533
+ auto subMap = subMapOrError.get ();
531
534
532
535
ProtocolConformanceRef genericConformance =
533
536
readConformance (Cursor, genericEnv);
@@ -571,7 +574,11 @@ ModuleFile::readConformanceChecked(llvm::BitstreamCursor &Cursor,
571
574
case NORMAL_PROTOCOL_CONFORMANCE_ID: {
572
575
NormalConformanceID conformanceID;
573
576
NormalProtocolConformanceIdLayout::readRecord (scratch, conformanceID);
574
- return ProtocolConformanceRef (readNormalConformance (conformanceID));
577
+
578
+ auto conformance = readNormalConformanceChecked (conformanceID);
579
+ if (!conformance)
580
+ return conformance.takeError ();
581
+ return ProtocolConformanceRef (conformance.get ());
575
582
}
576
583
577
584
case PROTOCOL_CONFORMANCE_XREF: {
@@ -614,7 +621,7 @@ ModuleFile::readConformanceChecked(llvm::BitstreamCursor &Cursor,
614
621
}
615
622
}
616
623
617
- NormalProtocolConformance *ModuleFile::readNormalConformance (
624
+ Expected< NormalProtocolConformance *> ModuleFile::readNormalConformanceChecked (
618
625
NormalConformanceID conformanceID) {
619
626
auto &conformanceEntry = NormalConformances[conformanceID-1 ];
620
627
if (conformanceEntry.isComplete ()) {
@@ -647,13 +654,21 @@ NormalProtocolConformance *ModuleFile::readNormalConformance(
647
654
rawIDs);
648
655
649
656
ASTContext &ctx = getContext ();
650
- DeclContext *dc = getDeclContext (contextID);
657
+ auto doOrError = getDeclContextChecked (contextID);
658
+ if (!doOrError)
659
+ return doOrError.takeError ();
660
+ DeclContext *dc = doOrError.get ();
661
+
651
662
assert (!isa<ClangModuleUnit>(dc->getModuleScopeContext ())
652
663
&& " should not have serialized a conformance from a clang module" );
653
664
Type conformingType = dc->getDeclaredInterfaceType ();
654
665
PrettyStackTraceType trace (ctx, " reading conformance for" , conformingType);
655
666
656
- auto proto = cast<ProtocolDecl>(getDecl (protoID));
667
+ auto protoOrError = getDeclChecked (protoID);
668
+ if (!protoOrError)
669
+ return protoOrError.takeError ();
670
+ auto proto = cast<ProtocolDecl>(protoOrError.get ());
671
+
657
672
PrettyStackTraceDecl traceTo (" ... to" , proto);
658
673
++NumNormalProtocolConformancesLoaded;
659
674
@@ -1069,7 +1084,10 @@ ModuleFile::getSubstitutionMapChecked(serialization::SubstitutionMapID id) {
1069
1084
conformances.reserve (numConformances);
1070
1085
for (unsigned i : range (numConformances)) {
1071
1086
(void )i;
1072
- conformances.push_back (readConformance (DeclTypeCursor));
1087
+ auto conformanceOrError = readConformanceChecked (DeclTypeCursor);
1088
+ if (!conformanceOrError)
1089
+ return conformanceOrError.takeError ();
1090
+ conformances.push_back (conformanceOrError.get ());
1073
1091
}
1074
1092
1075
1093
// Form the substitution map and record it.
@@ -2798,7 +2816,10 @@ class DeclDeserializer {
2798
2816
var->setIsSetterMutating (isSetterMutating);
2799
2817
declOrOffset = var;
2800
2818
2801
- Type interfaceType = MF.getType (interfaceTypeID);
2819
+ auto interfaceTypeOrError = MF.getTypeChecked (interfaceTypeID);
2820
+ if (!interfaceTypeOrError)
2821
+ return interfaceTypeOrError.takeError ();
2822
+ Type interfaceType = interfaceTypeOrError.get ();
2802
2823
var->setInterfaceType (interfaceType);
2803
2824
var->setImplicitlyUnwrappedOptional (isIUO);
2804
2825
@@ -3221,9 +3242,12 @@ class DeclDeserializer {
3221
3242
auto genericSig = MF.getGenericSignature (genericSigID);
3222
3243
if (genericSig)
3223
3244
opaqueDecl->setGenericSignature (genericSig);
3224
- if (underlyingTypeID)
3225
- opaqueDecl->setUnderlyingTypeSubstitutions (
3226
- MF.getSubstitutionMap (underlyingTypeID));
3245
+ if (underlyingTypeID) {
3246
+ auto subMapOrError = MF.getSubstitutionMapChecked (underlyingTypeID);
3247
+ if (!subMapOrError)
3248
+ return subMapOrError.takeError ();
3249
+ opaqueDecl->setUnderlyingTypeSubstitutions (subMapOrError.get ());
3250
+ }
3227
3251
SubstitutionMap subs;
3228
3252
if (genericSig) {
3229
3253
subs = genericSig->getIdentitySubstitutionMap ();
@@ -5102,7 +5126,11 @@ class TypeDeserializer {
5102
5126
decls_block::OpaqueArchetypeTypeLayout::readRecord (scratch,
5103
5127
opaqueDeclID, subsID);
5104
5128
5105
- auto opaqueDecl = cast<OpaqueTypeDecl>(MF.getDecl (opaqueDeclID));
5129
+ auto opaqueTypeOrError = MF.getDeclChecked (opaqueDeclID);
5130
+ if (!opaqueTypeOrError)
5131
+ return opaqueTypeOrError.takeError ();
5132
+
5133
+ auto opaqueDecl = cast<OpaqueTypeDecl>(opaqueTypeOrError.get ());
5106
5134
auto subs = MF.getSubstitutionMap (subsID);
5107
5135
5108
5136
return OpaqueTypeArchetypeType::get (opaqueDecl, subs);
@@ -5827,9 +5855,22 @@ ModuleFile::loadAllConformances(const Decl *D, uint64_t contextData,
5827
5855
fatalIfNotSuccess (DeclTypeCursor.JumpToBit (bitPosition));
5828
5856
5829
5857
while (numConformances--) {
5830
- auto conf = readConformance (DeclTypeCursor);
5831
- if (conf.isConcrete ())
5832
- conformances.push_back (conf.getConcrete ());
5858
+ auto conformance = readConformanceChecked (DeclTypeCursor);
5859
+
5860
+ if (!conformance) {
5861
+ // Missing module errors are most likely caused by an
5862
+ // implementation-only import hiding types and decls.
5863
+ // rdar://problem/60291019
5864
+ if (conformance.errorIsA <XRefNonLoadedModuleError>()) {
5865
+ consumeError (conformance.takeError ());
5866
+ return ;
5867
+ }
5868
+ else
5869
+ fatal (conformance.takeError ());
5870
+ }
5871
+
5872
+ if (conformance.get ().isConcrete ())
5873
+ conformances.push_back (conformance.get ().getConcrete ());
5833
5874
}
5834
5875
}
5835
5876
0 commit comments