#Packed union inside of a packed struct

1 messages · Page 1 of 1 (latest)

vagrant totem
#

I'm doing some bit field work and I've encountered a roadblock.
My application could use a struct with some field that are unions like TestStruct:

const std = @import("std");

const TestStruct = packed struct {
    a: u32,
    b:  packed union {
        b1: u32,
        b2: i32,
    }
};

pub fn main() void{
    const c = TestStruct{.a = 10, .b = .{.b2= -10}};
    std.debug.print("{}\n", .{c.b.b2});
}

Howevere compiling it gives some LLVM errors:
LLVM Emit Object... LLVM ERROR: Unsupported expression in static initializer: zext (i32 bitcast ({ i32 } { i32 -10 } to i32) to i64)

Am I missing something?
Also if I remove packed from the struct it compiles fine.

steep prawn
#

what's your zig version? Btw, just to be clear, that's most definitely a bug of some sort

hoary hornet
#

reproduced
can't reproduce 0.10.0-dev.4253+fa9327ac0
updating zig should fix it

vagrant totem
#

I was using 0.10.0-dev.3880+e2bb92b2e. Updated to 0.10.0-dev.4324+c23b3e6fd and I still get the same error

vagrant totem
#

Ubuntu 22.04

hoary hornet
#

this probably needs a github issue

vagrant totem
#

What platform you are using?

steep prawn
hoary hornet
#

Debian sid 5.19.11-1, Linux 5.19.0-2-amd64

steep prawn
#

I'd recommend trying it out with the nightly before going to make any issues

#

right, just tested it on my machine with latest nightly

#

does appear to happen

#

I'd recommend looking to see if there's an issue open for this

#

if you can't find one, go ahead and post one

hoary hornet
#

Oh

#

-O ReleaseFast somehow fixes it

vagrant totem
hoary hornet
#
$ cat >tf.zig
const std = @import("std");

const TestStruct = packed struct {
    a: u32,
    b:  packed union {
        b1: u32,
        b2: i32,
    }
};

pub fn main() void{
    const c = TestStruct{.a = 10, .b = .{.b2= -10}};
    std.debug.print("{}\n", .{c.b.b2});
}
$ zig build-exe tf.zig
LLVM Emit Object... ./tfLLVM ERROR: Unsupported expression in static initializer: or (i64 shl (i64 zext (i32 bitcast ({ i32 } { i32 -10 } to i32) to i64), i64 32), i64 10)
[1]    10719 IOT instruction  zig build-exe tf.zig
$ zig build-exe tf.zig -O ReleaseFast
$ ./tf
-10
steep prawn
vagrant totem
hoary hornet
#

lol

vagrant totem
#

Gotta go fast