Skip to content

Instantly share code, notes, and snippets.

@matu3ba
Last active April 19, 2022 22:54
Show Gist options
  • Save matu3ba/5ff2bb84507055974f889ba61eba20f9 to your computer and use it in GitHub Desktop.
Save matu3ba/5ff2bb84507055974f889ba61eba20f9 to your computer and use it in GitHub Desktop.
access of inactive union field
!zig test union.zig
1/1 test "testme123"... thread 571747 panic: access of inactive union field
/home/user/dev/git/zig/tryzig/union.zig:40:27: 0x208295 in TcpIo.setup (test)
tcpio.ctrl.client = try tcp.Client.init(.ip, .{ .close_on_exec = true });
^
/home/user/dev/git/zig/tryzig/union.zig:51:28: 0x2078ec in test "testme123" (test)
var tcpio = TcpIo.setup(state) catch unreachable;
^
/home/user/dev/git/zig/zig/master/lib/std/special/test_runner.zig:79:28: 0x2286de in std.special.main (test)
} else test_fn.func();
^
/home/user/dev/git/zig/zig/master/lib/std/start.zig:573:22: 0x2225ac in std.start.callMain (test)
root.main();
^
/home/user/dev/git/zig/zig/master/lib/std/start.zig:517:12: 0x2097fe in std.start.callMainWithArgs (test)
return @call(.{ .modifier = .always_inline }, callMain, .{});
^
/home/user/dev/git/zig/zig/master/lib/std/start.zig:427:17: 0x208716 in std.start.posixCallMainAndExit (test)
std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));
^
/home/user/dev/git/zig/zig/master/lib/std/start.zig:340:5: 0x208522 in std.start._start (test)
@call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
^
error: the following test command crashed:
zig-cache/o/05c10336f90993bb6e9db887e76ac102/test /home/user/dev/git/zig/zig/master/build/zig
Shell beendet 1
const std = @import("std");
const builtin = @import("builtin");
const net = std.x.net;
const os = std.x.os;
const ip = net.ip;
const tcp = net.tcp;
const State = enum {
Control,
Worker,
};
const TcpIo = struct {
state: State,
// ip always localhost (ie 127.0.0.1)
port_ctrl: u16 = 9084,
port_data: u16 = 9085,
ctrl: union {
listener: tcp.Listener,
client: tcp.Client,
},
data: union {
listener: tcp.Listener,
client: tcp.Client,
},
fn setup(state: State) !TcpIo {
var tcpio = TcpIo{
.state = state,
.ctrl = undefined,
.data = undefined,
};
switch (state) {
State.Control => {
tcpio.ctrl.listener = try tcp.Listener.init(.ip, .{ .close_on_exec = true });
tcpio.data.listener = try tcp.Listener.init(.ip, .{ .close_on_exec = true });
},
State.Worker => {
tcpio.ctrl.client = try tcp.Client.init(.ip, .{ .close_on_exec = true });
tcpio.data.client = try tcp.Client.init(.ip, .{ .close_on_exec = true });
},
}
return tcpio;
}
};
test "testme123" {
//const state = State.Control;
const state = State.Worker;
var tcpio = TcpIo.setup(state) catch unreachable;
_ = tcpio;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment