@@ -458,6 +458,15 @@ SILLayout *ModuleFile::readSILLayout(llvm::BitstreamCursor &Cursor) {
458
458
ProtocolConformanceRef ModuleFile::readConformance (
459
459
llvm::BitstreamCursor &Cursor,
460
460
GenericEnvironment *genericEnv) {
461
+ auto conformance = readConformanceChecked (Cursor, genericEnv);
462
+ if (!conformance)
463
+ fatal (conformance.takeError ());
464
+ return conformance.get ();
465
+ }
466
+
467
+ Expected<ProtocolConformanceRef>
468
+ ModuleFile::readConformanceChecked (llvm::BitstreamCursor &Cursor,
469
+ GenericEnvironment *genericEnv) {
461
470
using namespace decls_block ;
462
471
463
472
SmallVector<uint64_t , 16 > scratch;
@@ -477,14 +486,24 @@ ProtocolConformanceRef ModuleFile::readConformance(
477
486
case ABSTRACT_PROTOCOL_CONFORMANCE: {
478
487
DeclID protoID;
479
488
AbstractProtocolConformanceLayout::readRecord (scratch, protoID);
480
- auto proto = cast<ProtocolDecl>(getDecl (protoID));
489
+
490
+ auto decl = getDeclChecked (protoID);
491
+ if (!decl)
492
+ return decl.takeError ();
493
+
494
+ auto proto = cast<ProtocolDecl>(decl.get ());
481
495
return ProtocolConformanceRef (proto);
482
496
}
483
497
484
498
case SELF_PROTOCOL_CONFORMANCE: {
485
499
DeclID protoID;
486
500
SelfProtocolConformanceLayout::readRecord (scratch, protoID);
487
- auto proto = cast<ProtocolDecl>(getDecl (protoID));
501
+
502
+ auto decl = getDeclChecked (protoID);
503
+ if (!decl)
504
+ return decl.takeError ();
505
+
506
+ auto proto = cast<ProtocolDecl>(decl.get ());
488
507
auto conformance = getContext ().getSelfConformance (proto);
489
508
return ProtocolConformanceRef (conformance);
490
509
}
@@ -696,6 +715,14 @@ GenericParamList *ModuleFile::maybeReadGenericParams(DeclContext *DC) {
696
715
void ModuleFile::readGenericRequirements (
697
716
SmallVectorImpl<Requirement> &requirements,
698
717
llvm::BitstreamCursor &Cursor) {
718
+ auto error = readGenericRequirementsChecked (requirements, Cursor);
719
+ if (error)
720
+ fatal (std::move (error));
721
+ }
722
+
723
+ llvm::Error ModuleFile::readGenericRequirementsChecked (
724
+ SmallVectorImpl<Requirement> &requirements,
725
+ llvm::BitstreamCursor &Cursor) {
699
726
using namespace decls_block ;
700
727
701
728
BCOffsetRAII lastRecordOffset (Cursor);
@@ -721,27 +748,42 @@ void ModuleFile::readGenericRequirements(
721
748
722
749
switch (rawKind) {
723
750
case GenericRequirementKind::Conformance: {
724
- auto subject = getType (rawTypeIDs[0 ]);
725
- auto constraint = getType (rawTypeIDs[1 ]);
751
+ auto subject = getTypeChecked (rawTypeIDs[0 ]);
752
+ if (!subject)
753
+ return subject.takeError ();
754
+
755
+ auto constraint = getTypeChecked (rawTypeIDs[1 ]);
756
+ if (!constraint)
757
+ return constraint.takeError ();
726
758
727
759
requirements.push_back (Requirement (RequirementKind::Conformance,
728
- subject, constraint));
760
+ subject. get () , constraint. get () ));
729
761
break ;
730
762
}
731
763
case GenericRequirementKind::Superclass: {
732
- auto subject = getType (rawTypeIDs[0 ]);
733
- auto constraint = getType (rawTypeIDs[1 ]);
764
+ auto subject = getTypeChecked (rawTypeIDs[0 ]);
765
+ if (!subject)
766
+ return subject.takeError ();
767
+
768
+ auto constraint = getTypeChecked (rawTypeIDs[1 ]);
769
+ if (!constraint)
770
+ return constraint.takeError ();
734
771
735
772
requirements.push_back (Requirement (RequirementKind::Superclass,
736
- subject, constraint));
773
+ subject. get () , constraint. get () ));
737
774
break ;
738
775
}
739
776
case GenericRequirementKind::SameType: {
740
- auto first = getType (rawTypeIDs[0 ]);
741
- auto second = getType (rawTypeIDs[1 ]);
777
+ auto first = getTypeChecked (rawTypeIDs[0 ]);
778
+ if (!first)
779
+ return first.takeError ();
780
+
781
+ auto second = getTypeChecked (rawTypeIDs[1 ]);
782
+ if (!second)
783
+ return second.takeError ();
742
784
743
785
requirements.push_back (Requirement (RequirementKind::SameType,
744
- first, second));
786
+ first. get () , second. get () ));
745
787
break ;
746
788
}
747
789
default :
@@ -759,7 +801,10 @@ void ModuleFile::readGenericRequirements(
759
801
LayoutRequirementLayout::readRecord (scratch, rawKind, rawTypeID,
760
802
size, alignment);
761
803
762
- auto first = getType (rawTypeID);
804
+ auto first = getTypeChecked (rawTypeID);
805
+ if (!first)
806
+ return first.takeError ();
807
+
763
808
LayoutConstraint layout;
764
809
LayoutConstraintKind kind = LayoutConstraintKind::UnknownLayout;
765
810
switch (rawKind) {
@@ -803,7 +848,7 @@ void ModuleFile::readGenericRequirements(
803
848
LayoutConstraint::getLayoutConstraint (kind, size, alignment, ctx);
804
849
805
850
requirements.push_back (
806
- Requirement (RequirementKind::Layout, first, layout));
851
+ Requirement (RequirementKind::Layout, first. get () , layout));
807
852
break ;
808
853
}
809
854
default :
@@ -815,6 +860,8 @@ void ModuleFile::readGenericRequirements(
815
860
if (!shouldContinue)
816
861
break ;
817
862
}
863
+
864
+ return llvm::Error::success ();
818
865
}
819
866
820
867
// / Advances past any records that might be part of a requirement signature.
@@ -866,7 +913,15 @@ void ModuleFile::configureGenericEnvironment(
866
913
}
867
914
868
915
GenericSignature *ModuleFile::getGenericSignature (
869
- serialization::GenericSignatureID ID) {
916
+ serialization::GenericSignatureID ID) {
917
+ auto signature = getGenericSignatureChecked (ID);
918
+ if (!signature)
919
+ fatal (signature.takeError ());
920
+ return signature.get ();
921
+ }
922
+
923
+ Expected<GenericSignature *>
924
+ ModuleFile::getGenericSignatureChecked (serialization::GenericSignatureID ID) {
870
925
using namespace decls_block ;
871
926
872
927
// Zero is a sentinel for having no generic signature.
@@ -912,7 +967,9 @@ GenericSignature *ModuleFile::getGenericSignature(
912
967
913
968
// Read the generic requirements.
914
969
SmallVector<Requirement, 4 > requirements;
915
- readGenericRequirements (requirements, DeclTypeCursor);
970
+ auto error = readGenericRequirementsChecked (requirements, DeclTypeCursor);
971
+ if (error)
972
+ return std::move (error);
916
973
917
974
// Construct the generic signature from the loaded parameters and
918
975
// requirements.
@@ -1040,6 +1097,12 @@ ModuleFile::getGenericSignatureOrEnvironment(
1040
1097
auto genericEnv = signature->createGenericEnvironment ();
1041
1098
envOrOffset = genericEnv;
1042
1099
1100
+ // // Read the generic requirements.
1101
+ // SmallVector<Requirement, 4> requirements;
1102
+ // auto error = readGenericRequirementsChecked(requirements, DeclTypeCursor);
1103
+ // if (error)
1104
+ // return std::move(error);
1105
+
1043
1106
return genericEnv;
1044
1107
}
1045
1108
@@ -1051,6 +1114,14 @@ GenericEnvironment *ModuleFile::getGenericEnvironment(
1051
1114
1052
1115
SubstitutionMap ModuleFile::getSubstitutionMap (
1053
1116
serialization::SubstitutionMapID id) {
1117
+ auto map = getSubstitutionMapChecked (id);
1118
+ if (!map)
1119
+ fatal (map.takeError ());
1120
+ return map.get ();
1121
+ }
1122
+
1123
+ Expected<SubstitutionMap>
1124
+ ModuleFile::getSubstitutionMapChecked (serialization::SubstitutionMapID id) {
1054
1125
using namespace decls_block ;
1055
1126
1056
1127
// Zero is a sentinel for having an empty substitution map.
@@ -1091,7 +1162,11 @@ SubstitutionMap ModuleFile::getSubstitutionMap(
1091
1162
replacementTypeIDs);
1092
1163
1093
1164
// Generic signature.
1094
- auto genericSig = getGenericSignature (genericSigID);
1165
+ auto genericSigOrError = getGenericSignatureChecked (genericSigID);
1166
+ if (!genericSigOrError)
1167
+ return genericSigOrError.takeError ();
1168
+
1169
+ auto genericSig = genericSigOrError.get ();
1095
1170
if (!genericSig) {
1096
1171
error ();
1097
1172
return SubstitutionMap ();
@@ -5037,9 +5112,13 @@ class swift::TypeDeserializer {
5037
5112
5038
5113
decls_block::DependentMemberTypeLayout::readRecord (scratch, baseID,
5039
5114
assocTypeID);
5115
+ auto assocType = MF.getDeclChecked (assocTypeID);
5116
+ if (!assocType)
5117
+ return assocType.takeError ();
5118
+
5040
5119
return DependentMemberType::get (
5041
5120
MF.getType (baseID),
5042
- cast<AssociatedTypeDecl>(MF. getDecl (assocTypeID )));
5121
+ cast<AssociatedTypeDecl>(assocType. get ( )));
5043
5122
}
5044
5123
5045
5124
Expected<Type> deserializeBoundGenericType (ArrayRef<uint64_t > scratch,
@@ -5749,23 +5828,26 @@ void ModuleFile::finishNormalConformance(NormalProtocolConformance *conformance,
5749
5828
continue ;
5750
5829
}
5751
5830
5752
- // Generic environment.
5753
- GenericEnvironment *syntheticEnv = nullptr ;
5754
-
5755
5831
auto trySetOpaqueWitness = [&]{
5756
5832
if (!req)
5757
5833
return ;
5758
-
5759
- // We shouldn't yet need to worry about generic requirements, since
5760
- // an imported ObjC method should never be generic.
5761
- assert (syntheticEnv == nullptr &&
5762
- " opaque witness shouldn't be generic yet. when this is "
5763
- " possible, it should use forwarding substitutions" );
5834
+
5764
5835
conformance->setWitness (req, Witness::forOpaque (req));
5765
5836
};
5766
5837
5767
5838
// Witness substitutions.
5768
- SubstitutionMap witnessSubstitutions = getSubstitutionMap (*rawIDIter++);
5839
+ auto witnessSubstitutions = getSubstitutionMapChecked (*rawIDIter++);
5840
+ if (!witnessSubstitutions) {
5841
+ // Missing module errors are most likely caused by an
5842
+ // implementation-only import hiding types and decls.
5843
+ // rdar://problem/52837313
5844
+ if (witnessSubstitutions.errorIsA <XRefNonLoadedModuleError>()) {
5845
+ consumeError (witnessSubstitutions.takeError ());
5846
+ isOpaque = true ;
5847
+ }
5848
+ else
5849
+ fatal (witnessSubstitutions.takeError ());
5850
+ }
5769
5851
5770
5852
// Handle opaque witnesses that couldn't be deserialized.
5771
5853
if (isOpaque) {
@@ -5774,7 +5856,7 @@ void ModuleFile::finishNormalConformance(NormalProtocolConformance *conformance,
5774
5856
}
5775
5857
5776
5858
// Set the witness.
5777
- trySetWitness (Witness::forDeserialized (witness, witnessSubstitutions));
5859
+ trySetWitness (Witness::forDeserialized (witness, witnessSubstitutions. get () ));
5778
5860
}
5779
5861
assert (rawIDIter <= rawIDs.end () && " read too much" );
5780
5862
0 commit comments