@@ -49,7 +49,6 @@ static unsigned getElementCountRec(SILModule &Module, SILType T,
49
49
if (IsSelfOfNonDelegatingInitializer) {
50
50
// Protocols never have a stored properties.
51
51
if (auto *NTD = T.getNominalOrBoundGenericNominal ()) {
52
-
53
52
unsigned NumElements = 0 ;
54
53
for (auto *VD : NTD->getStoredProperties ())
55
54
NumElements +=
@@ -137,6 +136,8 @@ static SILType getElementTypeRec(SILModule &Module, SILType T, unsigned EltNo,
137
136
return getElementTypeRec (Module, FieldType, EltNo, false );
138
137
EltNo -= NumFieldElements;
139
138
}
139
+ // This can only happen if we look at a symbolic element number of an empty
140
+ // tuple.
140
141
llvm::report_fatal_error (" invalid element number" );
141
142
}
142
143
@@ -145,14 +146,22 @@ static SILType getElementTypeRec(SILModule &Module, SILType T, unsigned EltNo,
145
146
// for each of the tuple members.
146
147
if (IsSelfOfNonDelegatingInitializer) {
147
148
if (auto *NTD = T.getNominalOrBoundGenericNominal ()) {
149
+ bool HasStoredProperties = false ;
148
150
for (auto *VD : NTD->getStoredProperties ()) {
151
+ HasStoredProperties = true ;
149
152
auto FieldType = T.getFieldType (VD, Module);
150
153
unsigned NumFieldElements =
151
154
getElementCountRec (Module, FieldType, false );
152
155
if (EltNo < NumFieldElements)
153
156
return getElementTypeRec (Module, FieldType, EltNo, false );
154
157
EltNo -= NumFieldElements;
155
158
}
159
+
160
+ // If we do not have any stored properties and were passed an EltNo of 0,
161
+ // just return self.
162
+ if (!HasStoredProperties && EltNo == 0 ) {
163
+ return T;
164
+ }
156
165
llvm::report_fatal_error (" invalid element number" );
157
166
}
158
167
}
@@ -206,22 +215,26 @@ SILValue DIMemoryObjectInfo::emitElementAddress(
206
215
// lifetimes for each of the tuple members.
207
216
if (IsSelf) {
208
217
if (auto *NTD = PointeeType.getNominalOrBoundGenericNominal ()) {
209
- // If we have a class, we can use a borrow directly and avoid ref count
210
- // traffic.
211
- if (isa<ClassDecl>(NTD) && Ptr->getType ().isAddress ()) {
212
- SILValue Original = Ptr;
213
- SILValue Borrowed = Ptr = B.createLoadBorrow (Loc, Ptr);
214
- EndBorrowList.emplace_back (Borrowed, Original);
215
- }
216
-
218
+ bool HasStoredProperties = false ;
217
219
for (auto *VD : NTD->getStoredProperties ()) {
220
+ if (!HasStoredProperties) {
221
+ HasStoredProperties = true ;
222
+ // If we have a class, we can use a borrow directly and avoid ref
223
+ // count traffic.
224
+ if (isa<ClassDecl>(NTD) && Ptr->getType ().isAddress ()) {
225
+ SILValue Original = Ptr;
226
+ SILValue Borrowed = Ptr = B.createLoadBorrow (Loc, Ptr);
227
+ EndBorrowList.emplace_back (Borrowed, Original);
228
+ }
229
+ }
230
+
218
231
auto FieldType = PointeeType.getFieldType (VD, Module);
219
232
unsigned NumFieldElements =
220
233
getElementCountRec (Module, FieldType, false );
221
234
if (EltNo < NumFieldElements) {
222
- if (isa<StructDecl>(NTD))
235
+ if (isa<StructDecl>(NTD)) {
223
236
Ptr = B.createStructElementAddr (Loc, Ptr, VD);
224
- else {
237
+ } else {
225
238
assert (isa<ClassDecl>(NTD));
226
239
SILValue Original, Borrowed;
227
240
if (Ptr.getOwnershipKind () != ValueOwnershipKind::Guaranteed) {
@@ -241,6 +254,12 @@ SILValue DIMemoryObjectInfo::emitElementAddress(
241
254
}
242
255
EltNo -= NumFieldElements;
243
256
}
257
+
258
+ if (!HasStoredProperties) {
259
+ assert (EltNo == 0 && " Element count problem" );
260
+ return Ptr;
261
+ }
262
+
244
263
continue ;
245
264
}
246
265
}
@@ -301,7 +320,9 @@ DIMemoryObjectInfo::getPathStringToElement(unsigned Element,
301
320
// If this is indexing into a field of 'self', look it up.
302
321
if (isNonDelegatingInit () && !isDerivedClassSelfOnly ()) {
303
322
if (auto *NTD = MemorySILType.getNominalOrBoundGenericNominal ()) {
323
+ bool HasStoredProperty = false ;
304
324
for (auto *VD : NTD->getStoredProperties ()) {
325
+ HasStoredProperty = true ;
305
326
auto FieldType = MemorySILType.getFieldType (VD, Module);
306
327
unsigned NumFieldElements =
307
328
getElementCountRec (Module, FieldType, false );
@@ -313,6 +334,10 @@ DIMemoryObjectInfo::getPathStringToElement(unsigned Element,
313
334
}
314
335
Element -= NumFieldElements;
315
336
}
337
+
338
+ // If we do not have any stored properties, we have nothing of interest.
339
+ if (!HasStoredProperty)
340
+ return nullptr ;
316
341
}
317
342
}
318
343
0 commit comments