# Swift compiler crashes when accessing from Swift code a C++ union member that is a struct with user defined copy constructor

**URL:** <https://forums.swift.org/t/swift-compiler-crashes-when-accessing-from-swift-code-a-c-union-member-that-is-a-struct-with-user-defined-copy-constructor/76250>\
**Category:** C++ interoperability\
**Tags:** cxx-interop\
**Created:** [November 27, 2024, 5:08pm UTC](https://forums.swift.org/t/swift-compiler-crashes-when-accessing-from-swift-code-a-c-union-member-that-is-a-struct-with-user-defined-copy-constructor/76250 "2024-11-27T17:08:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![coconut](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/coconut/32/42276_2.png) [@coconut](https://forums.swift.org/u/coconut)\
**Post date:** [November 27, 2024, 5:08pm UTC](https://forums.swift.org/t/swift-compiler-crashes-when-accessing-from-swift-code-a-c-union-member-that-is-a-struct-with-user-defined-copy-constructor/76250/1 "2024-11-27T17:08:09Z")

</div>

Hello ladies and gentlemen. First of all, I would like to thank everyone who made it possible to interoperate Swift and C++ code - it's a gigantic work and you did it great. I'm using this feature since day zero and I was able to build my Swift 6 Frameworks the way that the client code won't notice if the api comes from a C++ or Swift code. I even made a Swift global actor with custom executor that schedules jobs on my C++ task manager's specific queue and it works ✨ fabulous ✨ .

Even though there are still some issues, for the most of them I've written workarounds, and it's still way better than not having the Swift-C++ interoperability feature at all. There is one issue that recently bothered me the most. Let's suppose I have a 3d vector struct:

```cxx
struct vector3f {
    union {
        float contents[3];

        struct {
            float x;
            float y;
            float z;
        };
    };

    vector3f(): x(0), y(0), z(0) { }
};

```

And I have a 3d box struct:

```cxx
struct box3f {
    union {
        struct {
            vector3f start;
            vector3f end;
        };
        
        struct {
            float left;
            float bottom;
            float back;
            
            float right;
            float top;
            float front;
        };
    };
    
    box3f(): start(), end() { }
};

```

The code is simplified for example purposes. You could notice that I use unions to conveniently access vector's and boxes members. In practice, I have unit tests, paddings and many other things to make sure that nothing is misaligned and properly accessed.

If I access box's members from the Swift code, everything compiles as expected:

```swift
let box = box3f()
let left = box.left

```

Then I noticed while working with vector3f that the vector is not copied correctly - in my case, the x value of copy became 0, y became original's x and z became original's y. I didn't think about that much and decided to write my own copy constructor for vector3f:

```cxx
struct vector3f {
    // ...
    vector3f(const vector3f& other): x(other.x), y(other.y), z(other.z) { }
};

```

Now every instance of vector3f copies correctly, but then I have a problem with box3f:

> Record 'box3f' is not automatically available: does not have a copy constructor or destructor; does this type have reference semantics?

So I've also implemented a copy constructor for box3f:

```cxx
struct box3f {
    // ...
    box3f(const box3f& other): start(other.start), end(other.end) { }
};

```

And now I get a slap in the face from the Swift compiler if I try to access box's left or right property from Swift:

> Command SwiftCompile failed with a nonzero exit code

Looks like Swift compiler takes user defined copy constructors too personal and just crashes. The only solution I see right now is to write getter and setter functions from C++ code. Does anyone have the similar issue?
