Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning an error from an error union with a 0 length array fails with -ofmt=c #20881

Open
MasonRemaley opened this issue Jul 31, 2024 · 1 comment
Labels
backend-c The C backend (CBE) outputs C source code. bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@MasonRemaley
Copy link
Contributor

MasonRemaley commented Jul 31, 2024

Zig Version

0.14.0-dev.737+30b4a87db

Steps to Reproduce and Observed Behavior

Minimal repro thanks to @ianprime0509:

test {
    try @import("std").testing.expectError(error.E, f());
}

fn f() error{E}![0]u8 {
    return error.E;
}
Stack trace
build/stage4/bin/zig test lib/std/std.zig -target x86_64-linux -ofmt=c -lc --test-filter "error-union-zero-len-array"
thread 57888 panic: reached unreachable code
/home/mason/Documents/zig/lib/std/debug.zig:404:14: 0x5b4e62c in assert (zig)
    if (!ok) unreachable; // assertion failure
             ^S
/home/mason/Documents/zig/src/codegen/c.zig:8089:15: 0x753666d in freeLocal (zig)
        assert(!f.allocs.contains(local_index));
              ^l.Crc(u32,.{ .polynomial = 7
/home/mason/Documents/zig/src/codegen/c.zig:8066:18: 0x753690d in die (zig)
    try freeLocal(f, inst, local_index, ref_inst);
                 ^n_15613
/home/mason/Documents/zig/src/codegen/c.zig:8105:16: 0x787589d in feed (zig)
        try die(bt.f, bt.inst, op_ref);
               ^ion
/home/mason/Documents/zig/src/codegen/c.zig:7122:24: 0x78ba91a in airAggregateInit (zig)
            try bt.feed(element);
                       ^r
/home/mason/Documents/zig/src/codegen/c.zig:3257:54: 0x753caf0 in genBodyInner (zig)
            .aggregate_init   => try airAggregateInit(f, inst),
                                                     ^
/home/mason/Documents/zig/src/codegen/c.zig:3032:25: 0x753f616 in genBody (zig)
        try genBodyInner(f, body);
                        ^
/home/mason/Documents/zig/src/codegen/c.zig:3073:20: 0x7129613 in genBodyResolveState (zig)
        try genBody(f, body);
                   ^
/home/mason/Documents/zig/src/codegen/c.zig:4941:28: 0x789ba92 in airCondBr (zig)
    try genBodyResolveState(f, inst, liveness_condbr.then_deaths, then_body, false);
                           ^
/home/mason/Documents/zig/src/codegen/c.zig:3232:47: 0x753b78d in genBodyInner (zig)
            .cond_br          => try airCondBr(f, inst),
                                              ^
/home/mason/Documents/zig/src/codegen/c.zig:3071:25: 0x71295dd in genBodyResolveState (zig)
        try genBodyInner(f, body);
                        ^s
/home/mason/Documents/zig/src/codegen/c.zig:4605:28: 0x787f0ee in lowerBlock (zig)
    try genBodyResolveState(f, inst, &.{}, body, true);
                           ^es
/home/mason/Documents/zig/src/codegen/c.zig:4582:22: 0x787f852 in airBlock (zig)
    return lowerBlock(f, inst, @ptrCast(f.air.extra[extra.end..][0..extra.data.body_len]));
                     ^atures
/home/mason/Documents/zig/src/codegen/c.zig:3220:46: 0x753afa4 in genBodyInner (zig)
            .block            => try airBlock(f, inst),
                                             ^
/home/mason/Documents/zig/src/codegen/c.zig:3032:25: 0x753f616 in genBody (zig)
        try genBodyInner(f, body);
                        ^res
/home/mason/Documents/zig/src/codegen/c.zig:3073:20: 0x7129613 in genBodyResolveState (zig)
        try genBody(f, body);ls
                   ^
/home/mason/Documents/zig/src/codegen/c.zig:2833:28: 0x712824e in genFunc (zig)
    try genBodyResolveState(f, undefined, &.{}, main_body, false);
                           ^
/home/mason/Documents/zig/src/link/C.zig:252:20: 0x712a6b7 in updateFunc (zig)
    codegen.genFunc(&function) catch |err| switch (err) {
                   ^.gfx1200
/home/mason/Documents/zig/src/link.zig:426:82: 0x6c75111 in updateFunc (zig)
                return @as(*tag.Type(), @fieldParentPtr("base", base)).updateFunc(pt, func_index, air, liveness);
                                                                                 ^
/home/mason/Documents/zig/src/Zcu/PerThread.zig:826:22: 0x67e3889 in linkerUpdateFunc (zig)
        lf.updateFunc(pt, func_index, air, liveness) catch |err| switch (err) {
                     ^apple_a14
/home/mason/Documents/zig/src/Compilation.zig:4070:36: 0x635dd00 in processOneCodegenJob (zig)
            try pt.linkerUpdateFunc(func.func, func.air);
                                   ^
/home/mason/Documents/zig/src/Compilation.zig:4042:33: 0x635ce3f in codegenThread (zig)
            processOneCodegenJob(tid, comp, codegen_job) catch |job_error| {
                                ^
/home/mason/Documents/zig/lib/std/Thread/Pool.zig:178:50: 0x635cf81 in runFn (zig)
            @call(.auto, func, .{id.?} ++ closure.arguments);
                                                 ^
/home/mason/Documents/zig/lib/std/Thread/Pool.zig:291:32: 0x630b36d in worker (zig)
            run_node.data.runFn(&run_node.data, id);
                               ^58
/home/mason/Documents/zig/lib/std/Thread.zig:409:13: 0x6025b6a in callFn__anon_74738 (zig)
            @call(.auto, f, args);
            ^rc.cpu.ultrasparc
/home/mason/Documents/zig/lib/std/Thread.zig:678:30: 0x5da3612 in entryFn (zig)
                return callFn(f, args_ptr.*);
                             ^
./nptl/pthread_create.c:442:8: 0x7d0974894ac2 in start_thread (pthread_create.c)
../sysdeps/unix/sysv/linux/x86_64/clone3.S:81:0: 0x7d097492684f in ??? (../sysdeps/unix/sysv/linux/x86_64/clone3.S)
???:?:?: 0x0 in ??? (???)
fish: Job 1, 'build/stage4/bin/zig test lib/s…' terminated by signal SIGABRT (Abort)

Expected Behavior

I expected the test to pass.

@MasonRemaley MasonRemaley added the bug Observed behavior contradicts documented or intended behavior label Jul 31, 2024
@MasonRemaley MasonRemaley mentioned this issue Jul 31, 2024
5 tasks
@Vexu Vexu added the backend-c The C backend (CBE) outputs C source code. label Jul 31, 2024
@Vexu Vexu added this to the 0.15.0 milestone Jul 31, 2024
@ianprime0509
Copy link
Contributor

I was able to reduce this further:

test {
    g(error.E, .{});
}

pub fn g(a: anyerror, b: anyerror![0]u8) void {
    if (b) |v| {
        h(.{ a, v });
    } else |err| {
        h(.{ a, err });
    }
}

pub fn h(_: anytype) void {}

Here's the output of zig test -ofmt=c test.zig as of 0.14.0-dev.841+ea847535f:

Stack trace
thread 1527409 panic: reached unreachable code
/var/home/ian/src/zig/lib/std/debug.zig:384:14: 0x17dc92c in assert (zig)
    if (!ok) unreachable; // assertion failure
             ^
/var/home/ian/src/zig/src/codegen/c.zig:8089:15: 0x31c488d in freeLocal (zig)
        assert(!f.allocs.contains(local_index));
              ^
/var/home/ian/src/zig/src/codegen/c.zig:8066:18: 0x31c4b2d in die (zig)
    try freeLocal(f, inst, local_index, ref_inst);
                 ^
/var/home/ian/src/zig/src/codegen/c.zig:8105:16: 0x3503c5d in feed (zig)
        try die(bt.f, bt.inst, op_ref);
               ^
/var/home/ian/src/zig/src/codegen/c.zig:7122:24: 0x3548cda in airAggregateInit (zig)
            try bt.feed(element);
                       ^
/var/home/ian/src/zig/src/codegen/c.zig:3257:54: 0x31cad10 in genBodyInner (zig)
            .aggregate_init   => try airAggregateInit(f, inst),
                                                     ^
/var/home/ian/src/zig/src/codegen/c.zig:3032:25: 0x31cd836 in genBody (zig)
        try genBodyInner(f, body);
                        ^
/var/home/ian/src/zig/src/codegen/c.zig:3073:20: 0x2db31f3 in genBodyResolveState (zig)
        try genBody(f, body);
                   ^
/var/home/ian/src/zig/src/codegen/c.zig:4941:28: 0x3529e52 in airCondBr (zig)
    try genBodyResolveState(f, inst, liveness_condbr.then_deaths, then_body, false);
                           ^
/var/home/ian/src/zig/src/codegen/c.zig:3232:47: 0x31c99ad in genBodyInner (zig)
            .cond_br          => try airCondBr(f, inst),
                                              ^
/var/home/ian/src/zig/src/codegen/c.zig:3071:25: 0x2db31bd in genBodyResolveState (zig)
        try genBodyInner(f, body);
                        ^
/var/home/ian/src/zig/src/codegen/c.zig:4605:28: 0x350d4ae in lowerBlock (zig)
    try genBodyResolveState(f, inst, &.{}, body, true);
                           ^
/var/home/ian/src/zig/src/codegen/c.zig:4582:22: 0x350dc12 in airBlock (zig)
    return lowerBlock(f, inst, @ptrCast(f.air.extra[extra.end..][0..extra.data.body_len]));
                     ^
/var/home/ian/src/zig/src/codegen/c.zig:3220:46: 0x31c91c4 in genBodyInner (zig)
            .block            => try airBlock(f, inst),
                                             ^
/var/home/ian/src/zig/src/codegen/c.zig:3032:25: 0x31cd836 in genBody (zig)
        try genBodyInner(f, body);
                        ^
/var/home/ian/src/zig/src/codegen/c.zig:3073:20: 0x2db31f3 in genBodyResolveState (zig)
        try genBody(f, body);
                   ^
/var/home/ian/src/zig/src/codegen/c.zig:2833:28: 0x2db1e2e in genFunc (zig)
    try genBodyResolveState(f, undefined, &.{}, main_body, false);
                           ^
/var/home/ian/src/zig/src/link/C.zig:252:20: 0x2db4297 in updateFunc (zig)
    codegen.genFunc(&function) catch |err| switch (err) {
                   ^
/var/home/ian/src/zig/src/link.zig:426:82: 0x28fdaa1 in updateFunc (zig)
                return @as(*tag.Type(), @fieldParentPtr("base", base)).updateFunc(pt, func_index, air, liveness);
                                                                                 ^
/var/home/ian/src/zig/src/Zcu/PerThread.zig:826:22: 0x246b7f9 in linkerUpdateFunc (zig)
        lf.updateFunc(pt, func_index, air, liveness) catch |err| switch (err) {
                     ^
/var/home/ian/src/zig/src/Compilation.zig:4070:36: 0x1fe64d0 in processOneCodegenJob (zig)
            try pt.linkerUpdateFunc(func.func, func.air);
                                   ^
/var/home/ian/src/zig/src/Compilation.zig:4042:33: 0x1fe560f in codegenThread (zig)
            processOneCodegenJob(tid, comp, codegen_job) catch |job_error| {
                                ^
/var/home/ian/src/zig/lib/std/Thread/Pool.zig:178:50: 0x1fe5751 in runFn (zig)
            @call(.auto, func, .{id.?} ++ closure.arguments);
                                                 ^
/var/home/ian/src/zig/lib/std/Thread/Pool.zig:291:32: 0x1f93b1d in worker (zig)
            run_node.data.runFn(&run_node.data, id);
                               ^
/var/home/ian/src/zig/lib/std/Thread.zig:409:13: 0x1cb255a in callFn__anon_74768 (zig)
            @call(.auto, f, args);
            ^
/var/home/ian/src/zig/lib/std/Thread.zig:678:30: 0x1a31902 in entryFn (zig)
                return callFn(f, args_ptr.*);
                             ^
???:?:?: 0x7fd4f50fe1b6 in ??? (libc.so.6)
Unwind information for `libc.so.6:0x7fd4f50fe1b6` was not available, trace may be incomplete

???:?:?: 0x7fd4f51803cb in ??? (libc.so.6)
Aborted (core dumped)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-c The C backend (CBE) outputs C source code. bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

3 participants