Skip to content

Commit f1c56f7

Browse files
isaachierandrewrk
authored andcommittedJun 29, 2018
Clarify reason implicit cast does not work for large RHS (#1168)
* Clarify reason implicit cast does not work for large RHS
·
0.15.20.3.0
1 parent 0874a5b commit f1c56f7

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
 

‎src/ir.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11432,6 +11432,26 @@ static TypeTableEntry *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *
1143211432
} else {
1143311433
TypeTableEntry *shift_amt_type = get_smallest_unsigned_int_type(ira->codegen,
1143411434
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

1143611456
casted_op2 = ir_implicit_cast(ira, op2, shift_amt_type);
1143711457
if (casted_op2 == ira->codegen->invalid_instruction)

‎test/compile_errors.zig‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,18 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
16771677
".tmp_source.zig:1:16: error: integer value 300 cannot be implicitly casted to type 'u8'",
16781678
);
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+
16801692
cases.add(
16811693
"incompatible number literals",
16821694
\\const x = 2 == 2.0;

0 commit comments

Comments
 (0)
Please sign in to comment.