zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 866c253e0ee9dd666ba715ebafecb889c8066367 (tree)
parent 8e3c56b912b7eb6ee551b7e427adbaae0bdcd408
Author: Timon Kruiper <timonkruiper@gmail.com>
Date:   Wed, 28 Aug 2019 23:12:42 +0200

Add compile error when shifting amount is not an int type

Diffstat:
Msrc/ir.cpp | 9++++++++-
Mtest/compile_errors.zig | 20++++++++++++++++++++
2 files changed, 28 insertions(+), 1 deletion(-)

diff --git 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 @@ -71,6 +71,26 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { ); 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 { \\ rhsExpr: ?AstObject,