From 866c253e0ee9dd666ba715ebafecb889c8066367 Mon Sep 17 00:00:00 2001 From: Timon Kruiper Date: Wed, 28 Aug 2019 23:12:42 +0200 Subject: [PATCH] Add compile error when shifting amount is not an int type --- src/ir.cpp | 9 ++++++++- test/compile_errors.zig | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/ir.cpp b/src/ir.cpp index 03e084e5ce..7504ed3b44 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -13734,7 +13734,7 @@ static IrInstruction *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *b return ira->codegen->invalid_instruction; if (op1->value.type->id != ZigTypeIdInt && op1->value.type->id != ZigTypeIdComptimeInt) { - ir_add_error(ira, &bin_op_instruction->base, + ir_add_error(ira, bin_op_instruction->op1, buf_sprintf("bit shifting operation expected integer type, found '%s'", buf_ptr(&op1->value.type->name))); return ira->codegen->invalid_instruction; @@ -13744,6 +13744,13 @@ static IrInstruction *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *b if (type_is_invalid(op2->value.type)) return ira->codegen->invalid_instruction; + if (op2->value.type->id != ZigTypeIdInt && op2->value.type->id != ZigTypeIdComptimeInt) { + ir_add_error(ira, bin_op_instruction->op2, + buf_sprintf("shift amount has to be an integer type, but found '%s'", + buf_ptr(&op2->value.type->name))); + return ira->codegen->invalid_instruction; + } + IrInstruction *casted_op2; IrBinOp op_id = bin_op_instruction->op_id; if (op1->value.type->id == ZigTypeIdComptimeInt) { diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 38fca5754d..3534ed2247 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -70,6 +70,26 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:6:37: error: expected type '*i32', found 'bool'", ); + cases.add( + "shift amount has to be an integer type", + \\export fn entry() void { + \\ const x = 1 << &u8(10); + \\} + , + "tmp.zig:2:23: error: shift amount has to be an integer type, but found '*u8'", + "tmp.zig:2:17: note: referenced here", + ); + + cases.add( + "bit shifting only works on integer types", + \\export fn entry() void { + \\ const x = &u8(1) << 10; + \\} + , + "tmp.zig:2:18: error: bit shifting operation expected integer type, found '*u8'", + "tmp.zig:2:22: note: referenced here", + ); + cases.add( "struct depends on itself via optional field", \\const LhsExpr = struct {