@@ -431,6 +431,15 @@ SILLayout *ModuleFile::readSILLayout(llvm::BitstreamCursor &Cursor) {
431
431
ProtocolConformanceRef ModuleFile::readConformance (
432
432
llvm::BitstreamCursor &Cursor,
433
433
GenericEnvironment *genericEnv) {
434
+ auto conformance = readConformanceChecked (Cursor, genericEnv);
435
+ if (!conformance)
436
+ fatal (conformance.takeError ());
437
+ return conformance.get ();
438
+ }
439
+
440
+ Expected<ProtocolConformanceRef>
441
+ ModuleFile::readConformanceChecked (llvm::BitstreamCursor &Cursor,
442
+ GenericEnvironment *genericEnv) {
434
443
using namespace decls_block ;
435
444
436
445
SmallVector<uint64_t , 16 > scratch;
@@ -450,14 +459,24 @@ ProtocolConformanceRef ModuleFile::readConformance(
450
459
case ABSTRACT_PROTOCOL_CONFORMANCE: {
451
460
DeclID protoID;
452
461
AbstractProtocolConformanceLayout::readRecord (scratch, protoID);
453
- auto proto = cast<ProtocolDecl>(getDecl (protoID));
462
+
463
+ auto decl = getDeclChecked (protoID);
464
+ if (!decl)
465
+ return decl.takeError ();
466
+
467
+ auto proto = cast<ProtocolDecl>(decl.get ());
454
468
return ProtocolConformanceRef (proto);
455
469
}
456
470
457
471
case SELF_PROTOCOL_CONFORMANCE: {
458
472
DeclID protoID;
459
473
SelfProtocolConformanceLayout::readRecord (scratch, protoID);
460
- auto proto = cast<ProtocolDecl>(getDecl (protoID));
474
+
475
+ auto decl = getDeclChecked (protoID);
476
+ if (!decl)
477
+ return decl.takeError ();
478
+
479
+ auto proto = cast<ProtocolDecl>(decl.get ());
461
480
auto conformance = getContext ().getSelfConformance (proto);
462
481
return ProtocolConformanceRef (conformance);
463
482
}
@@ -663,6 +682,14 @@ GenericParamList *ModuleFile::maybeReadGenericParams(DeclContext *DC) {
663
682
void ModuleFile::readGenericRequirements (
664
683
SmallVectorImpl<Requirement> &requirements,
665
684
llvm::BitstreamCursor &Cursor) {
685
+ auto error = readGenericRequirementsChecked (requirements, Cursor);
686
+ if (error)
687
+ fatal (std::move (error));
688
+ }
689
+
690
+ llvm::Error ModuleFile::readGenericRequirementsChecked (
691
+ SmallVectorImpl<Requirement> &requirements,
692
+ llvm::BitstreamCursor &Cursor) {
666
693
using namespace decls_block ;
667
694
668
695
BCOffsetRAII lastRecordOffset (Cursor);
@@ -688,27 +715,42 @@ void ModuleFile::readGenericRequirements(
688
715
689
716
switch (rawKind) {
690
717
case GenericRequirementKind::Conformance: {
691
- auto subject = getType (rawTypeIDs[0 ]);
692
- auto constraint = getType (rawTypeIDs[1 ]);
718
+ auto subject = getTypeChecked (rawTypeIDs[0 ]);
719
+ if (!subject)
720
+ return subject.takeError ();
721
+
722
+ auto constraint = getTypeChecked (rawTypeIDs[1 ]);
723
+ if (!constraint)
724
+ return constraint.takeError ();
693
725
694
726
requirements.push_back (Requirement (RequirementKind::Conformance,
695
- subject, constraint));
727
+ subject. get () , constraint. get () ));
696
728
break ;
697
729
}
698
730
case GenericRequirementKind::Superclass: {
699
- auto subject = getType (rawTypeIDs[0 ]);
700
- auto constraint = getType (rawTypeIDs[1 ]);
731
+ auto subject = getTypeChecked (rawTypeIDs[0 ]);
732
+ if (!subject)
733
+ return subject.takeError ();
734
+
735
+ auto constraint = getTypeChecked (rawTypeIDs[1 ]);
736
+ if (!constraint)
737
+ return constraint.takeError ();
701
738
702
739
requirements.push_back (Requirement (RequirementKind::Superclass,
703
- subject, constraint));
740
+ subject. get () , constraint. get () ));
704
741
break ;
705
742
}
706
743
case GenericRequirementKind::SameType: {
707
- auto first = getType (rawTypeIDs[0 ]);
708
- auto second = getType (rawTypeIDs[1 ]);
744
+ auto first = getTypeChecked (rawTypeIDs[0 ]);
745
+ if (!first)
746
+ return first.takeError ();
747
+
748
+ auto second = getTypeChecked (rawTypeIDs[1 ]);
749
+ if (!second)
750
+ return second.takeError ();
709
751
710
752
requirements.push_back (Requirement (RequirementKind::SameType,
711
- first, second));
753
+ first. get () , second. get () ));
712
754
break ;
713
755
}
714
756
default :
@@ -725,7 +767,10 @@ void ModuleFile::readGenericRequirements(
725
767
LayoutRequirementLayout::readRecord (scratch, rawKind, rawTypeID,
726
768
size, alignment);
727
769
728
- auto first = getType (rawTypeID);
770
+ auto first = getTypeChecked (rawTypeID);
771
+ if (!first)
772
+ return first.takeError ();
773
+
729
774
LayoutConstraint layout;
730
775
LayoutConstraintKind kind = LayoutConstraintKind::UnknownLayout;
731
776
switch (rawKind) {
@@ -767,7 +812,7 @@ void ModuleFile::readGenericRequirements(
767
812
LayoutConstraint::getLayoutConstraint (kind, size, alignment, ctx);
768
813
769
814
requirements.push_back (
770
- Requirement (RequirementKind::Layout, first, layout));
815
+ Requirement (RequirementKind::Layout, first. get () , layout));
771
816
break ;
772
817
}
773
818
default :
@@ -779,6 +824,8 @@ void ModuleFile::readGenericRequirements(
779
824
if (!shouldContinue)
780
825
break ;
781
826
}
827
+
828
+ return llvm::Error::success ();
782
829
}
783
830
784
831
// / Advances past any records that might be part of a requirement signature.
@@ -809,6 +856,14 @@ static void skipGenericRequirements(llvm::BitstreamCursor &Cursor) {
809
856
810
857
GenericSignature *ModuleFile::getGenericSignature (
811
858
serialization::GenericSignatureID ID) {
859
+ auto signature = getGenericSignatureChecked (ID);
860
+ if (!signature)
861
+ fatal (signature.takeError ());
862
+ return signature.get ();
863
+ }
864
+
865
+ Expected<GenericSignature *>
866
+ ModuleFile::getGenericSignatureChecked (serialization::GenericSignatureID ID) {
812
867
using namespace decls_block ;
813
868
814
869
// Zero is a sentinel for having no generic signature.
@@ -880,7 +935,9 @@ GenericSignature *ModuleFile::getGenericSignature(
880
935
881
936
// Read the generic requirements.
882
937
SmallVector<Requirement, 4 > requirements;
883
- readGenericRequirements (requirements, DeclTypeCursor);
938
+ auto error = readGenericRequirementsChecked (requirements, DeclTypeCursor);
939
+ if (error)
940
+ return std::move (error);
884
941
885
942
// If we've already deserialized this generic signature, start over to return
886
943
// it directly.
@@ -897,6 +954,14 @@ GenericSignature *ModuleFile::getGenericSignature(
897
954
898
955
SubstitutionMap ModuleFile::getSubstitutionMap (
899
956
serialization::SubstitutionMapID id) {
957
+ auto map = getSubstitutionMapChecked (id);
958
+ if (!map)
959
+ fatal (map.takeError ());
960
+ return map.get ();
961
+ }
962
+
963
+ Expected<SubstitutionMap>
964
+ ModuleFile::getSubstitutionMapChecked (serialization::SubstitutionMapID id) {
900
965
using namespace decls_block ;
901
966
902
967
// Zero is a sentinel for having an empty substitution map.
@@ -932,7 +997,11 @@ SubstitutionMap ModuleFile::getSubstitutionMap(
932
997
replacementTypeIDs);
933
998
934
999
// Generic signature.
935
- auto genericSig = getGenericSignature (genericSigID);
1000
+ auto genericSigOrError = getGenericSignatureChecked (genericSigID);
1001
+ if (!genericSigOrError)
1002
+ return genericSigOrError.takeError ();
1003
+
1004
+ auto genericSig = genericSigOrError.get ();
936
1005
if (!genericSig)
937
1006
fatal ();
938
1007
@@ -4770,9 +4839,13 @@ class swift::TypeDeserializer {
4770
4839
4771
4840
decls_block::DependentMemberTypeLayout::readRecord (scratch, baseID,
4772
4841
assocTypeID);
4842
+ auto assocType = MF.getDeclChecked (assocTypeID);
4843
+ if (!assocType)
4844
+ return assocType.takeError ();
4845
+
4773
4846
return DependentMemberType::get (
4774
4847
MF.getType (baseID),
4775
- cast<AssociatedTypeDecl>(MF. getDecl (assocTypeID )));
4848
+ cast<AssociatedTypeDecl>(assocType. get ( )));
4776
4849
}
4777
4850
4778
4851
Expected<Type> deserializeBoundGenericType (ArrayRef<uint64_t > scratch,
@@ -5447,23 +5520,26 @@ void ModuleFile::finishNormalConformance(NormalProtocolConformance *conformance,
5447
5520
continue ;
5448
5521
}
5449
5522
5450
- // Generic environment.
5451
- GenericEnvironment *syntheticEnv = nullptr ;
5452
-
5453
5523
auto trySetOpaqueWitness = [&]{
5454
5524
if (!req)
5455
5525
return ;
5456
-
5457
- // We shouldn't yet need to worry about generic requirements, since
5458
- // an imported ObjC method should never be generic.
5459
- assert (syntheticEnv == nullptr &&
5460
- " opaque witness shouldn't be generic yet. when this is "
5461
- " possible, it should use forwarding substitutions" );
5526
+
5462
5527
conformance->setWitness (req, Witness::forOpaque (req));
5463
5528
};
5464
5529
5465
5530
// Witness substitutions.
5466
- SubstitutionMap witnessSubstitutions = getSubstitutionMap (*rawIDIter++);
5531
+ auto witnessSubstitutions = getSubstitutionMapChecked (*rawIDIter++);
5532
+ if (!witnessSubstitutions) {
5533
+ // Missing module errors are most likely caused by an
5534
+ // implementation-only import hiding types and decls.
5535
+ // rdar://problem/52837313
5536
+ if (witnessSubstitutions.errorIsA <XRefNonLoadedModuleError>()) {
5537
+ consumeError (witnessSubstitutions.takeError ());
5538
+ isOpaque = true ;
5539
+ }
5540
+ else
5541
+ fatal (witnessSubstitutions.takeError ());
5542
+ }
5467
5543
5468
5544
// Handle opaque witnesses that couldn't be deserialized.
5469
5545
if (isOpaque) {
@@ -5472,7 +5548,7 @@ void ModuleFile::finishNormalConformance(NormalProtocolConformance *conformance,
5472
5548
}
5473
5549
5474
5550
// Set the witness.
5475
- trySetWitness (Witness::forDeserialized (witness, witnessSubstitutions));
5551
+ trySetWitness (Witness::forDeserialized (witness, witnessSubstitutions. get () ));
5476
5552
}
5477
5553
assert (rawIDIter <= rawIDs.end () && " read too much" );
5478
5554
0 commit comments