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

Sat shl neg rhs #9949

Merged
merged 2 commits into from Oct 15, 2021
Merged

Sat shl neg rhs #9949

merged 2 commits into from Oct 15, 2021

Conversation

travisstaloch
Copy link
Sponsor Contributor

@travisstaloch travisstaloch commented Oct 15, 2021

This pr adds a check for negative rhs to saturating shl and expected compile errors test cases.

This is a follow up to my saturating arithmetic pr and was discovered by @Snektron as demonstrated in the following:

pub fn main() void {
    _ = @as(i32, 1) <<| @as(i32, -2);
}

This pr prevents the compiler bug and emits a compile error:

$ ./zig run tmp.zig 
./tmp.zig:6:25: error: shift by negative value -2
    _ = @as(i32, 1) <<| @as(i32, -2);
                        ^
./tmp.zig:6:21: note: referenced here
    _ = @as(i32, 1) <<| @as(i32, -2);
                    ^

@travisstaloch
Copy link
Sponsor Contributor Author

I forgot to mention that the compiler bug without this patch is:

$ ../build/zig run tmp.zig
Assertion failed at ../src/stage1/bigint.cpp:1432 in bigint_shl. This is a bug in the Zig compiler.thread 26254 panic: 
/media/data/Users/Travis/Documents/Code/zig/zig/src/stage1.zig:177:5: 0x559a5da669a7 in stage2_panic (zig1)
    @panic(ptr[0..len]);
    ^
../src/stage1/util.cpp:20:17: 0x559a5e9222d2 in zig_panic (../src/stage1/util.cpp)
    stage2_panic("", 0);
                ^
../src/stage1/util_base.hpp:53:18: 0x559a5ea5aaaa in zig_assert (../src/stage1/bigint.cpp)
        zig_panic("Assertion failed at %s:%d in %s. This is a bug in the Zig compiler.", file, line, func);
                 ^
../src/stage1/bigint.cpp:1432:5: 0x559a5ea5f2f1 in bigint_shl (../src/stage1/bigint.cpp)
    assert(!op2->is_negative);
    ^
../src/stage1/bigint.cpp:545:15: 0x559a5ea5c51b in bigint_shl_sat (../src/stage1/bigint.cpp)
    bigint_shl(dest, op1, op2);
              ^
../src/stage1/ir.cpp:9846:31: 0x559a5e99400f in ir_eval_math_op_scalar (../src/stage1/ir.cpp)
                bigint_shl_sat(&out_val->data.x_bigint, &op1_val->data.x_bigint, &op2_val->data.x_bigint, type_entry->data.integral.bit_count, type_entry->data.integral.is_signed);
                              ^
../src/stage1/ir.cpp:9896:35: 0x559a5e994331 in ir_analyze_math_op (../src/stage1/ir.cpp)
        if (ir_eval_math_op_scalar(ira, scope, source_node, type_entry, op1_val, op_id, op2_val, out_val) != nullptr) {
                                  ^
../src/stage1/ir.cpp:10412:34: 0x559a5e995f00 in ir_analyze_bin_op_math (../src/stage1/ir.cpp)
        return ir_analyze_math_op(ira, instruction->base.scope, instruction->base.source_node, resolved_type, op1_val, op_id, op2_val);
                                 ^
../src/stage1/ir.cpp:11053:42: 0x559a5e998895 in ir_analyze_instruction_bin_op (../src/stage1/ir.cpp)
            return ir_analyze_bin_op_math(ira, bin_op_instruction);
                                         ^
...

@andrewrk andrewrk merged commit 16ac034 into ziglang:master Oct 15, 2021
@travisstaloch travisstaloch deleted the sat-shl-neg-rhs branch October 15, 2021 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants