Skip to content

stage2: Panic when initializing Tuple with explicit type #11182

@topolarity

Description

@topolarity

Zig Version

0.10.0-dev.1335+c64279b15

Steps to Reproduce

test {
    const T = @TypeOf(.{ @as(i32, 0), @as(u32, 0) });
    var a = T{ 0, 0 };
    _ = a;
}

Expected Behavior

Test should pass.

Test is a minor variant of #11159, but the root cause is quite different. In this case, AstGen and the AIR expect to be able to refer to the "elem_type" of the initialized array.

Tuples are initialized like arrays but their fields are heterogeneous like structs, so this fails in Sema.

Actual Behavior

Test crashes:

thread 16327 panic: reached unreachable code
Analyzing testme.zig: testme.zig:test_0
      %2 = dbg_stmt(2, 5)
      %3 = typeof_builtin({
        %4 = as_node(@Ref.i32_type, @Ref.zero) node_offset:2:35
        %5 = as_node(@Ref.u32_type, @Ref.zero) node_offset:2:48
        %6 = array_init_anon(.{%4, %5}) node_offset:2:24
        %7 = break_inline(%3, %6)
      }) node_offset:2:15
      %8 = dbg_var_val(%3, "T")
      %9 = dbg_stmt(3, 5)
      %10 = alloc_inferred_mut() node_offset:3:5
      %11 = validate_array_init_ty(%3) node_offset:3:14
    > %12 = elem_type(%3) node_offset:3:13
      %13 = coerce_result_ptr(%3, %10)
      %14 = elem_ptr_imm(%13, 0) node_offset:3:16
      %15 = store_node(%14, @Ref.zero) node_offset:3:16
      %16 = elem_ptr_imm(%13, 1) node_offset:3:19
      %17 = store_node(%16, @Ref.zero) node_offset:3:19
      %18 = validate_array_init({
        %14 = elem_ptr_imm(%13, 0) node_offset:3:16
        %16 = elem_ptr_imm(%13, 1) node_offset:3:19
      }) node_offset:3:14
      %19 = resolve_inferred_alloc(%10) node_offset:3:5
      %20 = dbg_var_ptr(%10, "a")
      %21 = dbg_stmt(4, 5)
      %22 = load(%10) node_offset:4:9
      %23 = ensure_result_non_error(%22) node_offset:4:9
      %24 = ret_tok(@Ref.void_value) token_offset:5:1
    For full context, use the command
      zig ast-check -t testme.zig


/home/topolarity/repos/zig/src/type.zig:3374:21: 0x2c85805 in type.Type.childType (zig)
            else => unreachable,
                    ^
/home/topolarity/repos/zig/src/Sema.zig:5204:42: 0x331096b in Sema.zirElemType (zig)
    const elem_type = array_type.elemType();
                                         ^
/home/topolarity/repos/zig/src/Sema.zig:671:66: 0x31cc82c in Sema.analyzeBodyInner (zig)
            .elem_type                    => try sema.zirElemType(block, inst),
                                                                 ^
/home/topolarity/repos/zig/src/Sema.zig:541:30: 0x31c035a in Sema.analyzeBody (zig)
    _ = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
    ```

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions