authorgravatar for isaachier@gmail.comisaachier <isaachier@gmail.com> 2018-06-29 14:52:25-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-29 14:52:25-04:00
logf1c56f7f225f2f3054abd8c9e6330a0f1e20b2a6
treed00685dfe1f65219fa15c8fa3a9a2a9010c723db
parent0874a5ba77a1d049a0e9e7f9f249605c109a731c

Clarify reason implicit cast does not work for large RHS (#1168)

* Clarify reason implicit cast does not work for large RHS

2 files changed, 32 insertions(+), 0 deletions(-)

src/ir.cpp+20
...@@ -11432,6 +11432,26 @@ static TypeTableEntry *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *...@@ -11432,6 +11432,26 @@ static TypeTableEntry *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *
11432 } else {11432 } else {
11433 TypeTableEntry *shift_amt_type = get_smallest_unsigned_int_type(ira->codegen,11433 TypeTableEntry *shift_amt_type = get_smallest_unsigned_int_type(ira->codegen,
11434 op1->value.type->data.integral.bit_count - 1);11434 op1->value.type->data.integral.bit_count - 1);
11435 if (bin_op_instruction->op_id == IrBinOpBitShiftLeftLossy &&
11436 op2->value.type->id == TypeTableEntryIdComptimeInt) {
11437 if (!bigint_fits_in_bits(&op2->value.data.x_bigint,
11438 shift_amt_type->data.integral.bit_count,
11439 op2->value.data.x_bigint.is_negative)) {
11440 Buf *val_buf = buf_alloc();
11441 bigint_append_buf(val_buf, &op2->value.data.x_bigint, 10);
11442 ErrorMsg* msg = ir_add_error(ira,
11443 &bin_op_instruction->base,
11444 buf_sprintf("RHS of shift is too large for LHS type"));
11445 add_error_note(
11446 ira->codegen,
11447 msg,
11448 op2->source_node,
11449 buf_sprintf("value %s cannot fit into type %s",
11450 buf_ptr(val_buf),
11451 buf_ptr(&shift_amt_type->name)));
11452 return ira->codegen->builtin_types.entry_invalid;
11453 }
11454 }
1143511455
11436 casted_op2 = ir_implicit_cast(ira, op2, shift_amt_type);11456 casted_op2 = ir_implicit_cast(ira, op2, shift_amt_type);
11437 if (casted_op2 == ira->codegen->invalid_instruction)11457 if (casted_op2 == ira->codegen->invalid_instruction)
test/compile_errors.zig+12
...@@ -1677,6 +1677,18 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -1677,6 +1677,18 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
1677 ".tmp_source.zig:1:16: error: integer value 300 cannot be implicitly casted to type 'u8'",1677 ".tmp_source.zig:1:16: error: integer value 300 cannot be implicitly casted to type 'u8'",
1678 );1678 );
16791679
1680 cases.add(
1681 "invalid shift amount error",
1682 \\const x : u8 = 2;
1683 \\fn f() u16 {
1684 \\ return x << 8;
1685 \\}
1686 \\export fn entry() u16 { return f(); }
1687 ,
1688 ".tmp_source.zig:3:14: error: RHS of shift is too large for LHS type",
1689 ".tmp_source.zig:3:17: note: value 8 cannot fit into type u3",
1690 );
1691
1680 cases.add(1692 cases.add(
1681 "incompatible number literals",1693 "incompatible number literals",
1682 \\const x = 2 == 2.0;1694 \\const x = 2 == 2.0;