Skip to content

Commit

Permalink
add compile error for shifting by negative comptime integer
Browse files Browse the repository at this point in the history
closes #698
  • Loading branch information
andrewrk committed Jan 18, 2018
1 parent 0fc645a commit 4b64c77
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/ir.cpp
Expand Up @@ -8816,6 +8816,13 @@ static TypeTableEntry *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *
if (op_id == IrBinOpBitShiftLeftLossy) {
op_id = IrBinOpBitShiftLeftExact;
}

if (casted_op2->value.data.x_bigint.is_negative) {
Buf *val_buf = buf_alloc();
bigint_append_buf(val_buf, &casted_op2->value.data.x_bigint, 10);
ir_add_error(ira, casted_op2, buf_sprintf("shift by negative value %s", buf_ptr(val_buf)));
return ira->codegen->builtin_types.entry_invalid;
}
} else {
TypeTableEntry *shift_amt_type = get_smallest_unsigned_int_type(ira->codegen,
op1->value.type->data.integral.bit_count - 1);
Expand Down
8 changes: 7 additions & 1 deletion test/compile_errors.zig
@@ -1,13 +1,19 @@
const tests = @import("tests.zig");

pub fn addCases(cases: &tests.CompileErrorContext) {
cases.add("shift by negative comptime integer",
\\comptime {
\\ var a = 1 >> -1;
\\}
, ".tmp_source.zig:2:18: error: shift by negative value -1");

cases.add("@panic called at compile time",
\\export fn entry() {
\\ comptime {
\\ @panic("aoeu");
\\ }
\\}
, "error: encountered @panic at compile-time");
, ".tmp_source.zig:3:9: error: encountered @panic at compile-time");

cases.add("wrong return type for main",
\\pub fn main() -> f32 { }
Expand Down

0 comments on commit 4b64c77

Please sign in to comment.