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 *...@@ -8816,6 +8816,13 @@ static TypeTableEntry *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *
8816 if (op_id == IrBinOpBitShiftLeftLossy) {8816 if (op_id == IrBinOpBitShiftLeftLossy) {
8817 op_id = IrBinOpBitShiftLeftExact;8817 op_id = IrBinOpBitShiftLeftExact;
8818 }8818 }
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 }
8819 } else {8826 } else {
8820 TypeTableEntry *shift_amt_type = get_smallest_unsigned_int_type(ira->codegen,8827 TypeTableEntry *shift_amt_type = get_smallest_unsigned_int_type(ira->codegen,
8821 op1->value.type->data.integral.bit_count - 1);8828 op1->value.type->data.integral.bit_count - 1);
test/compile_errors.zig+7-1
...@@ -1,13 +1,19 @@...@@ -1,13 +1,19 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: &tests.CompileErrorContext) {3pub 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
4 cases.add("@panic called at compile time",10 cases.add("@panic called at compile time",
5 \\export fn entry() {11 \\export fn entry() {
6 \\ comptime {12 \\ comptime {
7 \\ @panic("aoeu");13 \\ @panic("aoeu");
8 \\ }14 \\ }
9 \\}15 \\}
10 , "error: encountered @panic at compile-time");16 , ".tmp_source.zig:3:9: error: encountered @panic at compile-time");
1117
12 cases.add("wrong return type for main",18 cases.add("wrong return type for main",
13 \\pub fn main() -> f32 { }19 \\pub fn main() -> f32 { }