authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-18 17:47:21-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-18 17:47:21-05:00
log4b64c777ee465abb1f4a6bf2d31ba39805d5fa54
tree2295fd5b511f1fe46b69905fa383c9389044572a
parent0fc645ab7084c1812d367583b37218359b21c02c

add compile error for shifting by negative comptime integer

closes #698

2 files changed, 14 insertions(+), 1 deletions(-)

src/ir.cpp+7
......@@ -8816,6 +8816,13 @@ static TypeTableEntry *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *
88168816 if (op_id == IrBinOpBitShiftLeftLossy) {
88178817 op_id = IrBinOpBitShiftLeftExact;
88188818 }
8819
8820 if (casted_op2->value.data.x_bigint.is_negative) {
8821 Buf *val_buf = buf_alloc();
8822 bigint_append_buf(val_buf, &casted_op2->value.data.x_bigint, 10);
8823 ir_add_error(ira, casted_op2, buf_sprintf("shift by negative value %s", buf_ptr(val_buf)));
8824 return ira->codegen->builtin_types.entry_invalid;
8825 }
88198826 } else {
88208827 TypeTableEntry *shift_amt_type = get_smallest_unsigned_int_type(ira->codegen,
88218828 op1->value.type->data.integral.bit_count - 1);
test/compile_errors.zig+7-1
......@@ -1,13 +1,19 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: &tests.CompileErrorContext) {
4 cases.add("shift by negative comptime integer",
5 \\comptime {
6 \\ var a = 1 >> -1;
7 \\}
8 , ".tmp_source.zig:2:18: error: shift by negative value -1");
9
410 cases.add("@panic called at compile time",
511 \\export fn entry() {
612 \\ comptime {
713 \\ @panic("aoeu");
814 \\ }
915 \\}
10 , "error: encountered @panic at compile-time");
16 , ".tmp_source.zig:3:9: error: encountered @panic at compile-time");
1117
1218 cases.add("wrong return type for main",
1319 \\pub fn main() -> f32 { }