opensourcerouting / c-capnproto

C library/compiler for the Cap'n Proto serialization/RPC protocol
MIT License
118 stars 40 forks source link

Nested unions generate incorrect code #43

Closed detly closed 3 years ago

detly commented 3 years ago

For this schema:

struct A {
    union {
        zero @0 :Void;
        one :union {
            first @1 :Void;
            second @2 :UInt16;
        }
    }
}

I get this C code:

struct A {
    enum A_which which;
    capnp_nowarn union {
        enum A_one_which one_which;
        capnp_nowarn union {
            uint16_t second;
        } one;
    };
};

The one_which member is in a union alongside one, which means assigning to one.second will overwrite one_which.

detly commented 3 years ago

I have a PR coming that turns the generated C code into:

struct A {
    enum A_which which;
    capnp_nowarn union {
        capnp_nowarn struct {
            enum A_one_which which;
            capnp_nowarn union {
                uint16_t second;
            };
        } one;
    };
};