mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
In C++, a primary base class that is placed in the beginning of the type's memory layout isn't always the type that is the first in the list of bases – the base types might be laid out in memory in a different order. This makes sure that IRGen handles base types of C++ structs in the correct order. This fixes an assertion in asserts-enabled compilers, and an out-of-memory error in asserts-disabled compilers. The issue was happening for both value types and foreign reference types. This change also includes a small refactoring to reuse the logic between the two code paths. rdar://140848603
135 lines
3.6 KiB
Swift
135 lines
3.6 KiB
Swift
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop)
|
|
//
|
|
// REQUIRES: executable_test
|
|
|
|
import StdlibUnittest
|
|
import Fields
|
|
|
|
var FieldsTestSuite = TestSuite("Getting and setting fields in base classes")
|
|
|
|
struct SwiftStructWrapper {
|
|
let value: DerivedFromOneField
|
|
}
|
|
|
|
func optionalDerivedFromAll() -> DerivedFromAll? { DerivedFromAll() }
|
|
|
|
FieldsTestSuite.test("Fields from derived from all") {
|
|
let derived = DerivedFromAll()
|
|
expectEqual(derived.a, 1)
|
|
expectEqual(derived.b, 2)
|
|
expectEqual(derived.c, 3)
|
|
expectEqual(derived.d, 4)
|
|
expectEqual(derived.e, 5)
|
|
expectEqual(derived.f, 6)
|
|
|
|
var mutable = DerivedFromAll()
|
|
mutable.a = 42
|
|
mutable.d = 44
|
|
mutable.e = 46
|
|
mutable.f = 48
|
|
|
|
expectEqual(mutable.a, 42)
|
|
expectEqual(mutable.d, 44)
|
|
expectEqual(mutable.e, 46)
|
|
expectEqual(mutable.f, 48)
|
|
}
|
|
|
|
FieldsTestSuite.test("Optional") {
|
|
let derived = optionalDerivedFromAll()
|
|
expectEqual(derived!.a, 1)
|
|
expectEqual(derived!.b, 2)
|
|
expectEqual(derived!.c, 3)
|
|
expectEqual(derived!.d, 4)
|
|
expectEqual(derived!.e, 5)
|
|
expectEqual(derived!.f, 6)
|
|
}
|
|
|
|
FieldsTestSuite.test("Struct holding derived from one field") {
|
|
let derived = DerivedFromOneField()
|
|
let s = SwiftStructWrapper(value: derived)
|
|
|
|
expectEqual(s.value.value, 42)
|
|
}
|
|
|
|
FieldsTestSuite.test("Fields from derived from non trivial") {
|
|
let derived = NonTrivialDerivedFromAll()
|
|
expectEqual(derived.a, 1)
|
|
expectEqual(derived.b, 2)
|
|
expectEqual(derived.c, 3)
|
|
expectEqual(derived.d, 4)
|
|
expectEqual(derived.e, 5)
|
|
expectEqual(derived.f, 6)
|
|
|
|
var mutable = NonTrivialDerivedFromAll()
|
|
mutable.a = 42
|
|
mutable.d = 44
|
|
mutable.e = 46
|
|
mutable.f = 48
|
|
|
|
expectEqual(mutable.a, 42)
|
|
expectEqual(mutable.d, 44)
|
|
expectEqual(mutable.e, 46)
|
|
expectEqual(mutable.f, 48)
|
|
}
|
|
|
|
FieldsTestSuite.test("Derived from class template") {
|
|
var derived = DerivedFromClassTemplate()
|
|
derived.value = 42
|
|
expectEqual(derived.value, 42)
|
|
}
|
|
|
|
FieldsTestSuite.test("Same field from derived") {
|
|
var derived = DerivedWithSameField()
|
|
derived.a = 42
|
|
expectEqual(derived.a, 42)
|
|
}
|
|
|
|
extension CopyTrackedBaseClass {
|
|
var swiftProp: CInt {
|
|
return x
|
|
}
|
|
}
|
|
|
|
FieldsTestSuite.test("Get field without copying base in the getter accessor") {
|
|
let base = CopyTrackedBaseClass(0)
|
|
var copyCounter = getCopyCounter().pointee
|
|
expectEqual(base.swiftProp, 0)
|
|
// Measure copy counter of a regular
|
|
// property access for a C++ type to compare
|
|
// it to see if any additional copies are
|
|
// needed to access the property from the base class.
|
|
let expectedCopyCountDiff = getCopyCounter().pointee - copyCounter
|
|
|
|
let derived = CopyTrackedDerivedClass(234)
|
|
copyCounter = getCopyCounter().pointee
|
|
expectEqual(derived.x, 234)
|
|
expectEqual(copyCounter, getCopyCounter().pointee - expectedCopyCountDiff)
|
|
|
|
let derivedDerived = CopyTrackedDerivedDerivedClass(-11)
|
|
copyCounter = getCopyCounter().pointee
|
|
expectEqual(derivedDerived.x, -11)
|
|
expectEqual(copyCounter, getCopyCounter().pointee - expectedCopyCountDiff)
|
|
}
|
|
|
|
FieldsTestSuite.test("Structs with virtual methods") {
|
|
var derived = InheritFromStructsWithVirtualMethod()
|
|
derived.d = 42
|
|
expectEqual(derived.d, 42)
|
|
}
|
|
|
|
FieldsTestSuite.test("Field in tail padding of base class") {
|
|
let usesBaseTailPadding = DerivedUsesBaseTailPadding()
|
|
expectEqual(usesBaseTailPadding.field2, 789)
|
|
expectEqual(usesBaseTailPadding.field4, 456)
|
|
expectEqual(usesBaseTailPadding.field8, 123)
|
|
}
|
|
|
|
FieldsTestSuite.test("Out-of-order inheritance") {
|
|
let d = DerivedOutOfOrder()
|
|
expectEqual(d.leafField, 789)
|
|
expectEqual(d.derivedField, 456)
|
|
expectEqual(d.baseField, 123)
|
|
}
|
|
|
|
runAllTests()
|