Skip to content

Commit 682f162

Browse files
committed
C++ Interop: fix subscript operator tests
C++ structs that define a non-default copy constructor should actually copy their members in the copy constructor: Swift might copy the struct around, and if it does so after the `setValueAtIndex` call, the modification of `NonTrivialIntArrayByVal::values` won't propagate to the copied struct and these tests no longer pass: • `Interop/Cxx/operators/member-inline.swift` • `Interop/Cxx/operators/member-out-of-line.swift` The tests are currently passing on the main branch despite this, but they might fail after a seemingly unrelated change (which is how I discovered this).
1 parent b60c826 commit 682f162

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

test/Interop/Cxx/operators/Inputs/member-inline.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ struct IntArrayByVal {
147147

148148
struct NonTrivialIntArrayByVal {
149149
NonTrivialIntArrayByVal(int first) { values[0] = first; }
150-
NonTrivialIntArrayByVal(const NonTrivialIntArrayByVal &other) {}
150+
NonTrivialIntArrayByVal(const NonTrivialIntArrayByVal &other) {
151+
for (int i = 0; i < 5; i++)
152+
values[i] = other.values[i];
153+
}
154+
151155
int operator[](int x) const { return values[x]; }
152156

153157
// For testing purposes.

test/Interop/Cxx/operators/Inputs/member-out-of-line.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ struct ReadWriteIntArray {
3131

3232
struct NonTrivialIntArrayByVal {
3333
NonTrivialIntArrayByVal(int first) { values[0] = first; }
34-
NonTrivialIntArrayByVal(const NonTrivialIntArrayByVal &other) {}
34+
NonTrivialIntArrayByVal(const NonTrivialIntArrayByVal &other) {
35+
for (int i = 0; i < 5; i++)
36+
values[i] = other.values[i];
37+
}
3538
int operator[](int x);
3639

3740
// For testing purposes.

0 commit comments

Comments
 (0)