Skip to content

Commit 646f16d

Browse files
authored
Merge pull request #65551 from mikeash/fix-foreachfield-no-reflection-names
[Reflection] Handle -disable-reflection-names in _forEachField.
2 parents 922f884 + a6bab73 commit 646f16d

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

stdlib/public/core/ReflectionMirror.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,14 @@ public func _forEachField(
284284
defer { field.freeFunc?(field.name) }
285285
let kind = _MetadataKind(childType)
286286

287-
if !body(field.name!, offset, childType, kind) {
288-
return false
287+
if let name = field.name {
288+
if !body(name, offset, childType, kind) {
289+
return false
290+
}
291+
} else {
292+
if !body("", offset, childType, kind) {
293+
return false
294+
}
289295
}
290296
}
291297

@@ -362,8 +368,14 @@ public func _forEachFieldWithKeyPath<Root>(
362368
endOfReferencePrefix: false)
363369
}
364370

365-
if !body(field.name!, partialKeyPath) {
366-
return false
371+
if let name = field.name {
372+
if !body(name, partialKeyPath) {
373+
return false
374+
}
375+
} else {
376+
if !body("", partialKeyPath) {
377+
return false
378+
}
367379
}
368380
}
369381

test/stdlib/ForEachField.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
// RUN: %target-run-simple-swift
14+
// RUN: %target-run-simple-swift(-Xfrontend -disable-reflection-names -DNO_FIELD_NAMES)
1415
// REQUIRES: executable_test
1516
// REQUIRES: reflection
1617

@@ -150,6 +151,14 @@ func checkFields<T>(
150151
count += 1
151152

152153
let fieldName = String(cString: charPtr)
154+
if fieldName == "" {
155+
#if NO_FIELD_NAMES
156+
expectTrue(fields.values.contains{ $0 == offset && $1 == type })
157+
#else
158+
expectTrue(false, "Empty field name")
159+
#endif
160+
return true
161+
}
153162
guard let (checkOffset, checkType) = fields[fieldName] else {
154163
expectTrue(false, "Unexpected field '\(fieldName)'")
155164
return true
@@ -176,6 +185,14 @@ func checkFieldsWithKeyPath<T>(
176185
count += 1
177186

178187
let fieldName = String(cString: charPtr)
188+
if fieldName == "" {
189+
#if NO_FIELD_NAMES
190+
expectTrue(fields.values.contains{ $0 == keyPath })
191+
#else
192+
expectTrue(false, "Empty field name")
193+
#endif
194+
return true
195+
}
179196
guard let checkKeyPath = fields[fieldName] else {
180197
expectTrue(false, "Unexpected field '\(fieldName)'")
181198
return true
@@ -450,8 +467,13 @@ if #available(SwiftStdlib 5.2, *) {
450467
charPtr, _, type, _ in
451468

452469
let fieldName = String(cString: charPtr)
470+
#if NO_FIELD_NAMES
471+
return type == (Double, Double).self
472+
&& fieldName == ""
473+
#else
453474
return type == (Double, Double).self
454475
&& fieldName == "point"
476+
#endif
455477
})
456478

457479
expectTrue(_forEachField(of: EmptyNSObject.self, options: .classType) {

0 commit comments

Comments
 (0)