authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-06 19:49:28-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-06 19:49:28-07:00
log271a37b418828c8ee79402f97016be42c5fa2884
tree4834923022eddbc1e314a0d4d2844d15c0829574
parent6131b3716322d60cf26f7aad1a654dc6c4414051

implicit wrap widening cast on integer peer types

closes #46

3 files changed, 38 insertions(+), 1 deletions(-)

src/analyze.cpp+14-1
......@@ -1892,12 +1892,25 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa
18921892 continue;
18931893 } else if (prev_type->id == TypeTableEntryIdInt &&
18941894 cur_type->id == TypeTableEntryIdInt &&
1895 prev_type->data.integral.is_signed == cur_type->data.integral.is_signed)
1895 prev_type->data.integral.is_signed == cur_type->data.integral.is_signed &&
1896 (cur_type->data.integral.bit_count >= prev_type->data.integral.bit_count &&
1897 (cur_type->data.integral.is_wrapping || !prev_type->data.integral.is_wrapping)))
18961898 {
18971899 if (cur_type->data.integral.bit_count > prev_type->data.integral.bit_count) {
18981900 prev_type = cur_type;
18991901 prev_node = cur_node;
1902 } else if (cur_type->data.integral.is_wrapping && !prev_type->data.integral.is_wrapping) {
1903 prev_type = cur_type;
1904 prev_node = cur_node;
19001905 }
1906 continue;
1907 } else if (prev_type->id == TypeTableEntryIdInt &&
1908 cur_type->id == TypeTableEntryIdInt &&
1909 prev_type->data.integral.is_signed == cur_type->data.integral.is_signed &&
1910 (prev_type->data.integral.bit_count >= cur_type->data.integral.bit_count &&
1911 (prev_type->data.integral.is_wrapping || !cur_type->data.integral.is_wrapping)))
1912 {
1913 continue;
19011914 } else if (prev_type->id == TypeTableEntryIdFloat &&
19021915 cur_type->id == TypeTableEntryIdFloat)
19031916 {
test/run_tests.cpp+7
......@@ -1354,6 +1354,13 @@ fn mul(a: u16, b: u16) -> u16 {
13541354 ".tmp_source.zig:5:1: error: function evaluation caused overflow",
13551355 ".tmp_source.zig:3:18: note: called from here",
13561356 ".tmp_source.zig:6:7: note: overflow occurred here");
1357
1358 add_compile_fail_case("add incompatible int types", R"SOURCE(
1359fn add(x: i8w, y: i32) {
1360 const z = x + y;
1361}
1362 )SOURCE", 1, ".tmp_source.zig:3:17: error: incompatible types: 'i8w' and 'i32'");
1363
13571364}
13581365
13591366//////////////////////////////////////////////////////////////////////////////
test/self_hosted.zig+17
......@@ -1534,3 +1534,20 @@ fn shl_with_overflow() {
15341534 assert(!@shl_with_overflow(u16, 0b0010111111111111, 2, &result));
15351535 assert(result == 0b1011111111111100);
15361536}
1537
1538#attribute("test")
1539fn combine_non_wrap_with_wrap() {
1540 const x: i32 = 123;
1541 const y: i32w = 456;
1542 const z = x + y;
1543 const z2 = y + x;
1544 assert(@typeof(z) == i32w);
1545 assert(@typeof(z2) == i32w);
1546
1547 const a: i8 = 123;
1548 const b: i32w = 456;
1549 const c = b + a;
1550 const d = a + b;
1551 assert(@typeof(c) == i32w);
1552 assert(@typeof(d) == i32w);
1553}