@@ -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
@@ -4759,9 +4828,13 @@ class swift::TypeDeserializer {
4759
4828
4760
4829
decls_block::DependentMemberTypeLayout::readRecord (scratch, baseID,
4761
4830
assocTypeID);
4831
+ auto assocType = MF.getDeclChecked (assocTypeID);
4832
+ if (!assocType)
4833
+ return assocType.takeError ();
4834
+
4762
4835
return DependentMemberType::get (
4763
4836
MF.getType (baseID),
4764
- cast<AssociatedTypeDecl>(MF. getDecl (assocTypeID )));
4837
+ cast<AssociatedTypeDecl>(assocType. get ( )));
4765
4838
}
4766
4839
4767
4840
Expected<Type> deserializeBoundGenericType (ArrayRef<uint64_t > scratch,
@@ -5436,23 +5509,26 @@ void ModuleFile::finishNormalConformance(NormalProtocolConformance *conformance,
5436
5509
continue ;
5437
5510
}
5438
5511
5439
- // Generic environment.
5440
- GenericEnvironment *syntheticEnv = nullptr ;
5441
-
5442
5512
auto trySetOpaqueWitness = [&]{
5443
5513
if (!req)
5444
5514
return ;
5445
-
5446
- // We shouldn't yet need to worry about generic requirements, since
5447
- // an imported ObjC method should never be generic.
5448
- assert (syntheticEnv == nullptr &&
5449
- " opaque witness shouldn't be generic yet. when this is "
5450
- " possible, it should use forwarding substitutions" );
5515
+
5451
5516
conformance->setWitness (req, Witness::forOpaque (req));
5452
5517
};
5453
5518
5454
5519
// Witness substitutions.
5455
- SubstitutionMap witnessSubstitutions = getSubstitutionMap (*rawIDIter++);
5520
+ auto witnessSubstitutions = getSubstitutionMapChecked (*rawIDIter++);
5521
+ if (!witnessSubstitutions) {
5522
+ // Missing module errors are most likely caused by an
5523
+ // implementation-only import hiding types and decls.
5524
+ // rdar://problem/52837313
5525
+ if (witnessSubstitutions.errorIsA <XRefNonLoadedModuleError>()) {
5526
+ consumeError (witnessSubstitutions.takeError ());
5527
+ isOpaque = true ;
5528
+ }
5529
+ else
5530
+ fatal (witnessSubstitutions.takeError ());
5531
+ }
5456
5532
5457
5533
// Handle opaque witnesses that couldn't be deserialized.
5458
5534
if (isOpaque) {
@@ -5461,7 +5537,7 @@ void ModuleFile::finishNormalConformance(NormalProtocolConformance *conformance,
5461
5537
}
5462
5538
5463
5539
// Set the witness.
5464
- trySetWitness (Witness::forDeserialized (witness, witnessSubstitutions));
5540
+ trySetWitness (Witness::forDeserialized (witness, witnessSubstitutions. get () ));
5465
5541
}
5466
5542
assert (rawIDIter <= rawIDs.end () && " read too much" );
5467
5543
0 commit comments