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

anon union initialization at runtime reaches unreachable code #19966

Closed
lun-4 opened this issue May 14, 2024 · 3 comments
Closed

anon union initialization at runtime reaches unreachable code #19966

lun-4 opened this issue May 14, 2024 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@lun-4
Copy link
Contributor

lun-4 commented May 14, 2024

Zig Version

0.13.0-dev.207+e2ec54bb3

Steps to Reproduce and Observed Behavior

The original code that caused this crash is larger, but I reduced it to this which reproduces the same panic stack trace:

var cached_graphics_magick: union(enum) {
    not_found: void,
} = undefined;

pub fn main() void {
    cached_graphics_magick = .{ .not_found = {} }; // offending line, comment it to compile
}

Leads to this crash:

$ /home.orig/luna/other.git/zig/stage3/bin/zig run test62.zig
thread 13102 panic: reached unreachable code
Analyzing test62.zig: test62.zig:main
      %10 = dbg_stmt(2, 5)
      %11 = decl_ref("cached_graphics_magick") token_offset:8:5 to :8:27
      %12 = opt_eu_base_ptr_init(%11) node_offset:8:30 to :8:50
    > %13 = struct_init_field_ptr(%12, "not_found") node_offset:8:46 to :8:48
      %14 = block({
        %15 = restore_err_ret_index_unconditional(%14) node_offset:8:46 to :8:48
        %16 = break(%14, @void_value)
      }) node_offset:8:46 to :8:48
      %17 = store_node(%13, @void_value) node_offset:8:46 to :8:48
      %18 = validate_ptr_struct_init({
        %13 = struct_init_field_ptr(%12, "not_found") node_offset:8:46 to :8:48
      }) node_offset:8:30 to :8:50
      %19 = dbg_stmt(3, 5)
      %20 = decl_ref("std") token_offset:9:5 to :9:8
      %21 = dbg_stmt(3, 8)
      %22 = field_ptr(%20, "debug") node_offset:9:5 to :9:14
      %23 = dbg_stmt(3, 20)
      %24 = field_call(nodiscard .auto, %22, "print", [
        {
          %25 = str("{}")
          %26 = break_inline(%24, %25)
        },
        {
          %27 = validate_array_init_result_ty(%24, 1) node_offset:9:27 to :9:52
          %28 = array_init_elem_type(%24, 0)
          %29 = decl_val("cached_graphics_magick") token_offset:9:29 to :9:51
          %30 = array_init(%24{%29}) node_offset:9:27 to :9:52
          %31 = break_inline(%24, %30)
        },
      ]) node_offset:9:5 to :9:53
      %32 = restore_err_ret_index_unconditional(.none) node_offset:7:1 to :7:7
      %33 = ret_implicit(@void_value) token_offset:10:1 to :10:1
    For full context, use the command
      zig ast-check -t test62.zig


/home.orig/luna/other.git/zig/src/Sema.zig:30994:27: 0xadf2e20 in storePtrVal (zig)
        .runtime_store => unreachable, // use sites check this
                          ^
/home.orig/luna/other.git/zig/src/Sema.zig:28290:37: 0xaddfc99 in unionFieldPtr (zig)
                try sema.storePtrVal(block, src, union_ptr_val, new_union_val, union_ty);
                                    ^
/home.orig/luna/other.git/zig/src/Sema.zig:27711:53: 0xa8284ce in fieldPtr (zig)
            const field_ptr = try sema.unionFieldPtr(block, src, inner_ptr, field_name, field_name_src, inner_ty, initializing);
                                                    ^
/home.orig/luna/other.git/zig/src/Sema.zig:10381:33: 0xad576af in zirStructInitFieldPtr (zig)
            return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src, true);
                                ^
/home.orig/luna/other.git/zig/src/Sema.zig:1096:76: 0xa818e8e in analyzeBodyInner (zig)
            .struct_init_field_ptr        => try sema.zirStructInitFieldPtr(block, inst),
                                                                           ^
/home.orig/luna/other.git/zig/src/Sema.zig:892:26: 0xa8147d1 in analyzeFnBody (zig)
    sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                         ^
/home.orig/luna/other.git/zig/src/Module.zig:4633:23: 0xa503245 in analyzeFnBody (zig)
    sema.analyzeFnBody(&inner_block, fn_info.body) catch |err| switch (err) {
                      ^
/home.orig/luna/other.git/zig/src/Module.zig:3143:32: 0xa26c898 in ensureFuncBodyAnalyzed (zig)
    var air = zcu.analyzeFnBody(func_index, sema_arena) catch |err| switch (err) {
                               ^
/home.orig/luna/other.git/zig/src/Compilation.zig:3414:42: 0xa26a3b2 in processOneJob (zig)
            module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
                                         ^
/home.orig/luna/other.git/zig/src/Compilation.zig:3354:30: 0xa09eecf in performAllTheWork (zig)
            try processOneJob(comp, work_item, main_progress_node);
                             ^
/home.orig/luna/other.git/zig/src/Compilation.zig:2132:31: 0xa09a8d3 in update (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
/home.orig/luna/other.git/zig/src/main.zig:4489:24: 0xa0cbbff in updateModule (zig)
        try comp.update(main_progress_node);
                       ^
/home.orig/luna/other.git/zig/src/main.zig:3411:17: 0xa134552 in buildOutputType (zig)
    updateModule(comp, color) catch |err| switch (err) {
                ^
/home.orig/luna/other.git/zig/src/main.zig:270:31: 0x9f7e8d9 in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .run);
                              ^
/home.orig/luna/other.git/zig/src/main.zig:208:20: 0x9f7b575 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/home.orig/luna/other.git/zig/lib/std/start.zig:524:37: 0x9f7b00e in main (zig)
            const result = root.main() catch |err| {
                                    ^
../sysdeps/nptl/libc_start_call_main.h:58:16: 0x7feb41699c4b in __libc_start_call_main (../sysdeps/x86/libc-start.c)
../csu/libc-start.c:360:3: 0x7feb41699d04 in __libc_start_main_impl (../sysdeps/x86/libc-start.c)
../sysdeps/x86_64/start.S:115:0: 0x4b80a20 in _start (../sysdeps/x86_64/start.S)
???:?:?: 0x0 in ??? (???)
fish: Job 1, '/home.orig/luna/other.git/zig/s…' terminated by signal SIGABRT (Abort)

Expected Behavior

This code compiles on zig 0.11, but stopped working on 0.12. These results are using the Release builds of zig, rather than Debug (Observed Behavior section uses 0.13-dev on Debug mode as shown before):

$ ~/.zvm/0.11.0/zig run test62.zig
test62.cached_graphics_magick{ .not_found = void }⏎

$ ~/.zvm/0.12.0/zig run test62.zig
fish: Job 1, '~/.zvm/0.12.0/zig run test62.zig' terminated by signal SIGTRAP (Trace or breakpoint trap)
@lun-4 lun-4 added the bug Observed behavior contradicts documented or intended behavior label May 14, 2024
@lun-4
Copy link
Contributor Author

lun-4 commented May 14, 2024

To show that this is specific to anon initialization, this snippet reproduces the issue, but if you replace the anon init with the reference to the type, it compiles:

const Union = union(enum) {
    not_found: void,
};
var cached_graphics_magick: Union = undefined;

pub fn main() void {
    cached_graphics_magick = .{ .not_found = {} }; // crashes, comment to compile
    cached_graphics_magick = Union{ .not_found = {} }; // compiles without issue
}

@xdBronch
Copy link
Contributor

duplicate of #19832

@lun-4
Copy link
Contributor Author

lun-4 commented May 14, 2024

oops. thanks!

@lun-4 lun-4 closed this as completed May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants