authorgravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2019-08-28 23:12:42+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-05 13:10:39-04:00
log866c253e0ee9dd666ba715ebafecb889c8066367
tree4c248f22f342e12a916d3b5db53262bd619a9296
parent8e3c56b912b7eb6ee551b7e427adbaae0bdcd408
signaturelock-open Commit is signed but in an unrecognized format.

Add compile error when shifting amount is not an int type


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

src/ir.cpp+8-1
...@@ -13734,7 +13734,7 @@ static IrInstruction *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *b...@@ -13734,7 +13734,7 @@ static IrInstruction *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *b
13734 return ira->codegen->invalid_instruction;13734 return ira->codegen->invalid_instruction;
1373513735
13736 if (op1->value.type->id != ZigTypeIdInt && op1->value.type->id != ZigTypeIdComptimeInt) {13736 if (op1->value.type->id != ZigTypeIdInt && op1->value.type->id != ZigTypeIdComptimeInt) {
13737 ir_add_error(ira, &bin_op_instruction->base,13737 ir_add_error(ira, bin_op_instruction->op1,
13738 buf_sprintf("bit shifting operation expected integer type, found '%s'",13738 buf_sprintf("bit shifting operation expected integer type, found '%s'",
13739 buf_ptr(&op1->value.type->name)));13739 buf_ptr(&op1->value.type->name)));
13740 return ira->codegen->invalid_instruction;13740 return ira->codegen->invalid_instruction;
...@@ -13744,6 +13744,13 @@ static IrInstruction *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *b...@@ -13744,6 +13744,13 @@ static IrInstruction *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *b
13744 if (type_is_invalid(op2->value.type))13744 if (type_is_invalid(op2->value.type))
13745 return ira->codegen->invalid_instruction;13745 return ira->codegen->invalid_instruction;
1374613746
13747 if (op2->value.type->id != ZigTypeIdInt && op2->value.type->id != ZigTypeIdComptimeInt) {
13748 ir_add_error(ira, bin_op_instruction->op2,
13749 buf_sprintf("shift amount has to be an integer type, but found '%s'",
13750 buf_ptr(&op2->value.type->name)));
13751 return ira->codegen->invalid_instruction;
13752 }
13753
13747 IrInstruction *casted_op2;13754 IrInstruction *casted_op2;
13748 IrBinOp op_id = bin_op_instruction->op_id;13755 IrBinOp op_id = bin_op_instruction->op_id;
13749 if (op1->value.type->id == ZigTypeIdComptimeInt) {13756 if (op1->value.type->id == ZigTypeIdComptimeInt) {
test/compile_errors.zig+20
...@@ -70,6 +70,26 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -70,6 +70,26 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
70 "tmp.zig:6:37: error: expected type '*i32', found 'bool'",70 "tmp.zig:6:37: error: expected type '*i32', found 'bool'",
71 );71 );
7272
73 cases.add(
74 "shift amount has to be an integer type",
75 \\export fn entry() void {
76 \\ const x = 1 << &u8(10);
77 \\}
78 ,
79 "tmp.zig:2:23: error: shift amount has to be an integer type, but found '*u8'",
80 "tmp.zig:2:17: note: referenced here",
81 );
82
83 cases.add(
84 "bit shifting only works on integer types",
85 \\export fn entry() void {
86 \\ const x = &u8(1) << 10;
87 \\}
88 ,
89 "tmp.zig:2:18: error: bit shifting operation expected integer type, found '*u8'",
90 "tmp.zig:2:22: note: referenced here",
91 );
92
73 cases.add(93 cases.add(
74 "struct depends on itself via optional field",94 "struct depends on itself via optional field",
75 \\const LhsExpr = struct {95 \\const LhsExpr = struct {