Skip to content

Commit ea4fe32

Browse files
committed
Reflection: Give up instead of producing an empty field list if a field type does not demangle
Better to fail instead of producing an invalid layout.
1 parent d07a3f8 commit ea4fe32

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ class TypeRefBuilder {
314314
const FieldDescriptor *getFieldTypeInfo(const TypeRef *TR);
315315

316316
/// Get the parsed and substituted field types for a nominal type.
317-
std::vector<FieldTypeInfo>
318-
getFieldTypeRefs(const TypeRef *TR, const FieldDescriptor *FD);
317+
bool getFieldTypeRefs(const TypeRef *TR, const FieldDescriptor *FD,
318+
std::vector<FieldTypeInfo> &Fields);
319319

320320
/// Get the primitive type lowering for a builtin type.
321321
const BuiltinTypeDescriptor *getBuiltinTypeInfo(const TypeRef *TR);

stdlib/public/Reflection/TypeLowering.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,13 @@ class EnumTypeInfoBuilder {
878878
unsigned NoPayloadCases = 0;
879879
std::vector<FieldTypeInfo> PayloadCases;
880880

881-
for (auto Case : TC.getBuilder().getFieldTypeRefs(TR, FD)) {
881+
std::vector<FieldTypeInfo> Fields;
882+
if (!TC.getBuilder().getFieldTypeRefs(TR, FD, Fields)) {
883+
Invalid = true;
884+
return nullptr;
885+
}
886+
887+
for (auto Case : Fields) {
882888
if (Case.TR == nullptr) {
883889
NoPayloadCases++;
884890
continue;
@@ -1024,7 +1030,12 @@ class LowerType
10241030
// Lower the struct's fields using substitutions from the
10251031
// TypeRef to make field types concrete.
10261032
RecordTypeInfoBuilder builder(TC, RecordKind::Struct);
1027-
for (auto Field : TC.getBuilder().getFieldTypeRefs(TR, FD))
1033+
1034+
std::vector<FieldTypeInfo> Fields;
1035+
if (!TC.getBuilder().getFieldTypeRefs(TR, FD, Fields))
1036+
return nullptr;
1037+
1038+
for (auto Field : Fields)
10281039
builder.addField(Field.Name, Field.TR);
10291040
return builder.build();
10301041
}
@@ -1266,11 +1277,15 @@ const TypeInfo *TypeConverter::getClassInstanceTypeInfo(const TypeRef *TR,
12661277
// TypeRef to make field types concrete.
12671278
RecordTypeInfoBuilder builder(*this, RecordKind::ClassInstance);
12681279

1280+
std::vector<FieldTypeInfo> Fields;
1281+
if (!getBuilder().getFieldTypeRefs(TR, FD, Fields))
1282+
return nullptr;
1283+
12691284
// Start layout from the given instance start offset. This should
12701285
// be the superclass instance size.
12711286
builder.addField(start, 1, /*numExtraInhabitants=*/0);
12721287

1273-
for (auto Field : getBuilder().getFieldTypeRefs(TR, FD))
1288+
for (auto Field : Fields)
12741289
builder.addField(Field.Name, Field.TR);
12751290
return builder.build();
12761291
}

stdlib/public/Reflection/TypeRefBuilder.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,16 @@ TypeRefBuilder::getFieldTypeInfo(const TypeRef *TR) {
124124
return nullptr;
125125
}
126126

127-
std::vector<FieldTypeInfo>
128-
TypeRefBuilder::getFieldTypeRefs(const TypeRef *TR, const FieldDescriptor *FD) {
127+
128+
bool TypeRefBuilder::getFieldTypeRefs(const TypeRef *TR,
129+
const FieldDescriptor *FD,
130+
std::vector<FieldTypeInfo> &Fields) {
129131
if (FD == nullptr)
130-
return {};
132+
return false;
131133

132134
auto Subs = TR->getSubstMap();
133135

134136
Demangle::Demangler Dem;
135-
std::vector<FieldTypeInfo> Fields;
136137
for (auto &Field : *FD) {
137138
auto FieldName = Field.getFieldName();
138139

@@ -145,7 +146,7 @@ TypeRefBuilder::getFieldTypeRefs(const TypeRef *TR, const FieldDescriptor *FD) {
145146
auto Demangled = Dem.demangleType(Field.getMangledTypeName());
146147
auto Unsubstituted = swift::remote::decodeMangledType(*this, Demangled);
147148
if (!Unsubstituted)
148-
return {};
149+
return false;
149150

150151
auto Substituted = Unsubstituted->subst(*this, Subs);
151152

@@ -156,7 +157,7 @@ TypeRefBuilder::getFieldTypeRefs(const TypeRef *TR, const FieldDescriptor *FD) {
156157

157158
Fields.push_back(FieldTypeInfo::forField(FieldName, Substituted));
158159
}
159-
return Fields;
160+
return true;
160161
}
161162

162163
const BuiltinTypeDescriptor *

0 commit comments

Comments
 (0)