Files
swift-mirror/test/Interop/Cxx/class/for-each-field.swift
susmonteiro d81d6547ba [cxx-interop] Fix metadata mismatch regarding fields of structs
In https://github.com/swiftlang/swift/pull/78467 and https://github.com/swiftlang/swift/pull/78961, we stopped emitting metadata for private C++ fields. However, this created a mismatch between the fields emitted and the number of fields + their offsets in the StructDescriptor.

rdar://147263490
(cherry picked from commit 72b13b3b48)
2025-05-23 11:16:09 +01:00

77 lines
1.7 KiB
Swift

// RUN: %target-run-simple-swift(-cxx-interoperability-mode=default -Xfrontend -disable-availability-checking -I %S/Inputs)
// REQUIRES: executable_test
// REQUIRES: reflection
@_spi(Reflection) import Swift
import SimpleStructs
import StdlibUnittest
func checkFieldsWithKeyPath<T>(
of type: T.Type,
options: _EachFieldOptions = [],
fields: [String: PartialKeyPath<T>]
) {
var count = 0
_forEachFieldWithKeyPath(of: T.self, options: options) {
charPtr, keyPath in
count += 1
let fieldName = String(cString: charPtr)
if fieldName == "" {
expectTrue(false, "Empty field name")
return true
}
guard let checkKeyPath = fields[fieldName] else {
expectTrue(false, "Unexpected field '\(fieldName)'")
return true
}
expectTrue(checkKeyPath == keyPath)
return true
}
expectEqual(fields.count, count)
}
var ForEachFieldTestSuite = TestSuite("ForEachField")
ForEachFieldTestSuite.test("HasPrivateFieldsOnly") {
checkFieldsWithKeyPath(
of: HasPrivateFieldsOnly.self,
fields: [:]
)
}
ForEachFieldTestSuite.test("HasPublicFieldsOnly") {
checkFieldsWithKeyPath(
of: HasPublicFieldsOnly.self,
fields: [
"publ1": \HasPublicFieldsOnly.publ1,
"publ2": \HasPublicFieldsOnly.publ2
]
)
}
ForEachFieldTestSuite.test("HasPrivatePublicProtectedFields") {
checkFieldsWithKeyPath(
of: HasPrivatePublicProtectedFields.self,
fields: [
"publ1": \HasPrivatePublicProtectedFields.publ1,
"publ2": \HasPrivatePublicProtectedFields.publ2
]
)
}
ForEachFieldTestSuite.test("Outer") {
checkFieldsWithKeyPath(
of: Outer.self,
fields: [
"publStruct": \Outer.publStruct
]
)
}
runAllTests()