#[solved] Is it possible to set an union(enum) to a different branch after creation?

1 messages · Page 1 of 1 (latest)

weak latch
#
thread 292702 panic: access of union field 'any' while field 'void' is active
/ssd/dev/gd/tools/minim/src/minim/gen.zig:21:52: 0x1044993 in func (M)
    .retT= slate.C.Ident.Type{ .name= N.Proc.retT.?.any.name, .type= .i32 },
                                                   ^
/ssd/dev/gd/tools/minim/src/minim/gen.zig:35:44: 0x1044f35 in C (M)
    .Proc => try result.append(try Gen.func(ast, N)),
                                           ^
/ssd/dev/gd/tools/minim/src/M.zig:22:27: 0x10451a5 in main (M)
  const code = try M.Gen.C(&ast);
                          ^
/ssd/dev/gd/tools/.zvm/0.13.0/lib/std/start.zig:524:37: 0x103c685 in posixCallMainAndExit (M)
            const result = root.main() catch |err| {
                                    ^
/ssd/dev/gd/tools/.zvm/0.13.0/lib/std/start.zig:266:5: 0x103c1a1 in _start (M)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)
Aborted (core dumped)
```I'm getting this error when trying to read a tagged union
The value initially starts as this `void` branch... but then it is set to `any` as expected
But still, this error shows up
frank spear
#
const Foo = union(enum) { a: usize, b: bool };

var foo: Foo = .{ .a = 0 };
foo = .{ .b = false };
#

retroactively changing the question :O

weak latch
#

I didn't know how to explain it, was thinking about it

frank spear
#

you're trying to access a value of one variant while another was active

#

this is an error, which is caught on safe builds

weak latch
#

but I am setting the value to any

#

and its erroring as if void was active, while it shouldn't be

frank spear
#

well, the active variant was indeed void, so, you know...

#

you're probably overriding the value somewhere without noticing?

weak latch
#
pub fn newEmpty () Proc {
  return Proc{
    // ...
    .retT = Ident.Type.Void.new(),
    // ...
  };
}

// Start of parsing
pub fn parse (P:*Par) !void {
  var result = M.Ast.Proc.newEmpty();
  // ...
}
```I do have this at the start, which is why I worded the question that way
#

but then those two links above happen... so... that's why I asked if its not possible to change the branch

weak latch
#

I think it is being freed with the defer after the parser function ends

weak latch
#

Solved by not accidentally freeing the memory of the object when returning the parsed data from a nested function ✍️