8

I have code that fails to compile with Visual Studio 2015 Community Edition with the following error:

fatal error C1002: compiler is out of heap space in pass 2

The Code

struct Int { int i; };

struct B {
    union {
        struct { int x; };
        struct { Int y; };
    };

    constexpr B() : x(1) {}
};

struct A { static B b; };

B A::b;

int main() {
    return 0;
}

This is the simplest I've been able to boil down the fail state through trial and error, but there's still quite a bit going on.

What has me dumbfounded is that each of the following changes cause it to compile just fine...

Removing constexpr from B's constructor makes it work:

struct Int { int i; };

struct B {
    union {
        struct { int x; };
        struct { Int y; };
    };

    B() : x(1) {} // <---<<    ( constexpr B() : x(1) {} )
};

struct A { static B b; };

B A::b;

int main() {
    return 0;
}

Changing A's variable to be not static makes it work:

struct Int { int i; };

struct B {
    union {
        struct { int x; };
        struct { Int y; };
    };

    constexpr B() : x(1) {}
};

struct A { B b; }; // <---<<    ( struct A { static B b; }; B A::b; )

int main() {
    return 0;
}

Using a plain int in B's union's second struct instead of an int wrapper makes it work:

struct Int { int i; };

struct B {
    union {
        struct { int x; };
        struct { int y; }; // <---<<    ( struct { Int y; }; )
    };

    constexpr B() : x(1) {}
};

struct A { static B b; };

B A::b;

int main() {
    return 0;
}

Default-initializing x in B's constructor rather than passing 1 makes it work:

struct Int { int i; };

struct B {
    union {
        struct { int x; };
        struct { Int y; };
    };

    constexpr B() : x() {} // <---<<    ( constexpr B() : x(1) {} )
};

struct A { static B b; };

B A::b;

int main() {
    return 0;
}

Finally, taking B's union's Int member out of structs makes it work:

struct Int { int i; };

struct B {
    union {
        struct { int x; };
        Int y; // <---<<    ( struct { Int y; }; )
    };

    constexpr B() : x(1) {}
};

struct A { static B b; };

B A::b;

int main() {
    return 0;
}

Suffice to say, I'm at a complete loss. I would greatly appreciate any assistance from someone who understands the compiler better than I do.

timrau
  • 22,578
  • 4
  • 51
  • 64
Daskie
  • 435
  • 3
  • 11
  • 7
    There's nothing to understand. You found a compiler bug. Congratulations. File a bug report with Microsoft. – Sam Varshavchik Sep 05 '16 at 00:20
  • 2
    That's just MSVC being stupid. Works on gcc and clang. File a bug report if you have the time and motivation, you have the MCVE down already it seems. (Good job on that btw!) – Baum mit Augen Sep 05 '16 at 00:23
  • note: Anonymous structs are not permitted in Standard C++ – M.M Dec 28 '16 at 21:34

0 Answers0