Skip to content

stage2: panic when instantiating @TypeOf of anonymous struct #12030

@topolarity

Description

@topolarity

Zig Version

0.10.0-dev.2855+aab1284e1

Steps to Reproduce

test {
    var x: u32 = 3;
    const T = @TypeOf(.{ .foo = x });
    const a: T = .{ .foo = 1 }; // Works on stage1, panics on stage2
    _ = a;

    const U = @TypeOf(.{ .foo = 2 });
    const b: U = .{}; // Fails on stage1 with "error: cannot assign to constant", panics on stage2
    _ = b;
}

Expected Behavior

Test should compile and pass

Actual Behavior

thread 1716 panic: attempt to use null value
Analyzing test.zig: test.zig:test_0
      %2 = dbg_block_begin())
      %3 = dbg_stmt(2, 5)
      %4 = alloc_mut(@Zir.Inst.Ref.u32_type) node_offset:2:5
      %5 = int(3)
      %6 = store_node(%4, %5) node_offset:2:18
      %7 = dbg_var_ptr(%4, "x")
      %8 = dbg_stmt(3, 5)
      %9 = typeof_builtin({
        %10 = load(%4) node_offset:3:33
        %11 = struct_init_anon([foo=%10]) node_offset:3:24
        %12 = break_inline(%9, %11)
      }) node_offset:3:15
      %13 = dbg_var_val(%9, "T")
      %14 = dbg_stmt(4, 5)
      %15 = alloc(%9) node_offset:4:5
      %16 = field_base_ptr(%15) node_offset:4:19
      %17 = field_ptr(%16, "foo") node_offset:4:28
      %18 = store_node(%17, @Zir.Inst.Ref.one) node_offset:4:28
    > %19 = validate_struct_init({
        %17 = field_ptr(%16, "foo") node_offset:4:28
      }) node_offset:4:19
      %20 = make_ptr_const(%15) node_offset:4:5
      %21 = dbg_var_ptr(%20, "a")
      %22 = dbg_stmt(5, 5)
      %23 = load(%20) node_offset:5:9
      %24 = ensure_result_non_error(%23) node_offset:5:9
      %25 = dbg_stmt(7, 5)
      %26 = typeof_builtin({
        %27 = int(2)
        %28 = struct_init_anon([foo=%27]) node_offset:7:24
        %29 = break_inline(%26, %28)
      }) node_offset:7:15
      %30 = dbg_var_val(%26, "U")
      %31 = dbg_stmt(8, 5)
      %34 = as(%26, @Zir.Inst.Ref.empty_struct)
      %35 = dbg_var_val(%34, "b")
      %36 = dbg_stmt(9, 5)
      %38 = dbg_block_end())
      %37 = ensure_result_non_error(%34) node_offset:9:9
      %39 = ret_tok(@Zir.Inst.Ref.void_value) token_offset:10:1
    For full context, use the command
      zig ast-check -t test.zig


/home/topolarity/repos/zig/src/Sema.zig:3318:39: 0x346cfb6 in Sema.zirValidateStructInit (zig)
            agg_ty.castTag(.@"struct").?.data,
                                      ^
/home/topolarity/repos/zig/src/Sema.zig:1064:47: 0x32841c1 in Sema.analyzeBodyInner (zig)
                try sema.zirValidateStructInit(block, inst, false);
                                              ^
/home/topolarity/repos/zig/src/Sema.zig:598:30: 0x326c60a in Sema.analyzeBody (zig)
    _ = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                             ^
/home/topolarity/repos/zig/src/Module.zig:5049:21: 0x309e306 in Module.analyzeFnBody (zig)
    sema.analyzeBody(&inner_block, fn_info.body) catch |err| switch (err) {
                    ^
/home/topolarity/repos/zig/src/Module.zig:3777:40: 0x30811e0 in Module.ensureFuncBodyAnalyzed (zig)
            var air = mod.analyzeFnBody(func, sema_arena) catch |err| switch (err) {
                                       ^
/home/topolarity/repos/zig/src/Compilation.zig:2937:42: 0x2dfe72a in Compilation.processOneJob (zig)
            module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
                                         ^
/home/topolarity/repos/zig/src/Compilation.zig:2869:30: 0x2deb27d in Compilation.performAllTheWork (zig)
            try processOneJob(comp, work_item);
                             ^
/home/topolarity/repos/zig/src/Compilation.zig:2218:31: 0x2de3902 in Compilation.update (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
/home/topolarity/repos/zig/src/main.zig:3245:20: 0x2d4eebf in updateModule (zig)
    try comp.update();
                   ^
/home/topolarity/repos/zig/src/main.zig:2934:17: 0x2d1703a in buildOutputType (zig)
    updateModule(gpa, comp, hook) catch |err| switch (err) {
                ^
/home/topolarity/repos/zig/src/main.zig:225:31: 0x2cfb966 in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .zig_test);
                              ^
/home/topolarity/repos/zig/src/main.zig:174:20: 0x2cfa9b4 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/home/topolarity/repos/zig/lib/std/start.zig:581:37: 0x3220167 in std.start.callMain (zig)
            const result = root.main() catch |err| {
                                    ^
/home/topolarity/repos/zig/lib/std/start.zig:515:12: 0x2cfd407 in std.start.callMainWithArgs (zig)
    return @call(.{ .modifier = .always_inline }, callMain, .{});
           ^
/home/topolarity/repos/zig/lib/std/start.zig:480:12: 0x2cfd1b2 in std.start.main (zig)
    return @call(.{ .modifier = .always_inline }, callMainWithArgs, .{ @intCast(usize, c_argc), c_argv, envp });
           ^
???:?:?: 0x7faa7fb977fc in ??? (???)
[1]    1716 IOT instruction  ./stage2/bin/zig test test.zig

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