c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Union is not properly zero-initialized

Open cbuttner opened this issue 1 year ago • 4 comments

union Rect {
  struct { float[<2>] min, max; }
  // More stuff here...
}

fn void main() {
  Rect rect = {.max = {1, 2});
  assert(rect.min == {}); // Assert will fail: rect.min is not zero (in general)
}

cbuttner avatar May 15 '24 21:05 cbuttner

Ooooh, interesting!

lerno avatar May 16 '24 07:05 lerno

I can't reproduce this and the LLVM output looks fine, is there some variant of this that you used?

lerno avatar May 16 '24 07:05 lerno

Ah right, my example was actually working. This is what I tested with:

union Rect {
  struct { float[<2>] min, max; }
}

fn void test_rect(float[<2>] max) {
  Rect rect = {.max = max};
  assert(rect.min == {});
}

fn void main() {
  test_rect({1,2});
}

My full union looks like this by the way. I don't think it's required to reproduce the issue, but you can check codegen for this too.

union Rect {
  struct { float min_x, min_y, max_x, max_y; }
  struct { float left, top, right, bottom; }
  struct { Vec2f min, max; }
  Vec4f v;
  Vec2f[2] p;
}

cbuttner avatar May 16 '24 08:05 cbuttner

Should work fine now.

lerno avatar May 16 '24 09:05 lerno

This fixed it for me, thanks.

cbuttner avatar Jun 06 '24 20:06 cbuttner