zig icon indicating copy to clipboard operation
zig copied to clipboard

Trace/breakpoint trap compiling `@shlExact`

Open amp-59 opened this issue 2 years ago • 6 comments

Zig Version

0.12.0-dev.654+599641357

Steps to Reproduce and Observed Behavior

Compile example program with zig build-obj shl_error.zig shl_error.zig:

comptime {
    _ = @shlExact(1, 1);
}

Actual result:

zig build-obj shl_error.zig
Trace/breakpoint trap

Expected Behavior

amp-59 avatar Sep 29 '23 12:09 amp-59

debug stack trace in 0.12.0-dev.6294+cbf2b1fea

Analyzing test.zig: test.zig:comptime_0
      %2 = typeof_log2_int_type(@one) node_offset:2:19 to :2:20
      %3 = as_shift_operand(%2, @one) node_offset:2:22 to :2:23
    > %4 = shl_exact(@one, %3) node_offset:2:9 to :2:24
      %5 = ensure_result_non_error(%4) node_offset:2:9 to :2:24
      %6 = break_inline(%1, @void_value)
    For full context, use the command
      zig ast-check -t test.zig


/home/wrongnull/projects/zig/src/type.zig:2171:33: 0xd4c659 in intInfo (zig)
                .simple_type => unreachable, // handled via Index enum tag above
                                ^
/home/wrongnull/projects/zig/src/value.zig:2959:32: 0x199ad8f in shlWithOverflowScalar (zig)
        const info = ty.intInfo(mod);
                               ^
/home/wrongnull/projects/zig/src/value.zig:2949:37: 0x199b36a in shlWithOverflow (zig)
        return shlWithOverflowScalar(lhs, rhs, ty, allocator, mod);
                                    ^
/home/wrongnull/projects/zig/src/Sema.zig:13219:60: 0x13d3239 in zirShl (zig)
                const shifted = try lhs_val.shlWithOverflow(rhs_val, lhs_ty, sema.arena, mod);
                                                           ^
/home/wrongnull/projects/zig/src/Sema.zig:1202:42: 0xff0241 in analyzeBodyInner (zig)
            .shl_exact => try sema.zirShl(block, inst, .shl_exact),
                                         ^
/home/wrongnull/projects/zig/src/Sema.zig:933:45: 0xd61d34 in analyzeBodyBreak (zig)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
/home/wrongnull/projects/zig/src/Module.zig:3681:50: 0xd5f27c in semaDecl (zig)
    const result_ref = (try sema.analyzeBodyBreak(&block_scope, @ptrCast(body))).?.operand;
                                                 ^
/home/wrongnull/projects/zig/src/Module.zig:3255:32: 0xac947d in ensureDeclAnalyzed (zig)
        break :blk mod.semaDecl(decl_index) catch |err| switch (err) {
                               ^
/home/wrongnull/projects/zig/src/Compilation.zig:3827:38: 0xd43dc6 in processOneJob (zig)
            module.ensureDeclAnalyzed(decl_index) catch |err| switch (err) {
                                     ^
/home/wrongnull/projects/zig/src/Compilation.zig:3698:30: 0xb155d8 in performAllTheWork (zig)
            try processOneJob(comp, work_item, main_progress_node);
                             ^
/home/wrongnull/projects/zig/src/Compilation.zig:2505:31: 0xb10e32 in update (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
/home/wrongnull/projects/zig/src/main.zig:4283:24: 0xb41532 in updateModule (zig)
        try comp.update(main_progress_node);
                       ^
/home/wrongnull/projects/zig/src/main.zig:3684:17: 0xb640f2 in buildOutputType (zig)
    updateModule(comp) catch |err| switch (err) {
                ^
/home/wrongnull/projects/zig/src/main.zig:278:31: 0x96d6ef in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .{ .build = .Exe });
                              ^
/home/wrongnull/projects/zig/src/main.zig:222:20: 0x96a975 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/home/wrongnull/projects/zig/lib/std/start.zig:585:37: 0x96a3f6 in main (zig)
            const result = root.main() catch |err| {
                                    ^
../sysdeps/nptl/libc_start_call_main.h:58:16: 0x7f26f84890cf in __libc_start_call_main (../sysdeps/x86/libc-start.c)
../csu/libc-start.c:360:3: 0x7f26f8489188 in __libc_start_main_impl (../sysdeps/x86/libc-start.c)
???:?:?: 0x96a044 in ??? (???)
???:?:?: 0x0 in ??? (???)
Aborted

wrongnull avatar Dec 22 '23 07:12 wrongnull

should this be a compile error since bit count is undefined for comptime_int ?

nektro avatar Dec 22 '23 08:12 nektro

should this be a compile error since bit count is undefined for comptime_int ?

It will break code like

fn shlExact(any: anytype, shift_amt: anytype) @TypeOf(any) {
    return @shlExact(any, shift_amt);
}

wrongnull avatar Dec 22 '23 09:12 wrongnull

~~yes. btw i'm only referring to the first argument so you'd have to do something like @shlExact(@as(u32, 1), 1) ?~~

nektro avatar Dec 22 '23 18:12 nektro

~yes. btw i'm only referring to the first argument so you'd have to do something like @shlExact(@as(u32, 1), 1) ?~

The original issue is rather trivial to fix for me. The question is: should I attach a proposal issue to pull request. CC @Vexu

wrongnull avatar Dec 24 '23 18:12 wrongnull

This test used to pass with stage1:

comptime {
    if (@shlExact(1, 1) != 2) @compileError("should be 2");
}

Vexu avatar Dec 25 '23 22:12 Vexu