zig

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

commit 271a37b418828c8ee79402f97016be42c5fa2884 (tree)
parent 6131b3716322d60cf26f7aad1a654dc6c4414051
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Fri,  6 May 2016 19:49:28 -0700

implicit wrap widening cast on integer peer types

closes #46

Diffstat:
Msrc/analyze.cpp | 15++++++++++++++-
Mtest/run_tests.cpp | 7+++++++
Mtest/self_hosted.zig | 17+++++++++++++++++
3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/src/analyze.cpp b/src/analyze.cpp @@ -1892,12 +1892,25 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa continue; } else if (prev_type->id == TypeTableEntryIdInt && cur_type->id == TypeTableEntryIdInt && - prev_type->data.integral.is_signed == cur_type->data.integral.is_signed) + prev_type->data.integral.is_signed == cur_type->data.integral.is_signed && + (cur_type->data.integral.bit_count >= prev_type->data.integral.bit_count && + (cur_type->data.integral.is_wrapping || !prev_type->data.integral.is_wrapping))) { if (cur_type->data.integral.bit_count > prev_type->data.integral.bit_count) { prev_type = cur_type; prev_node = cur_node; + } else if (cur_type->data.integral.is_wrapping && !prev_type->data.integral.is_wrapping) { + prev_type = cur_type; + prev_node = cur_node; } + continue; + } else if (prev_type->id == TypeTableEntryIdInt && + cur_type->id == TypeTableEntryIdInt && + prev_type->data.integral.is_signed == cur_type->data.integral.is_signed && + (prev_type->data.integral.bit_count >= cur_type->data.integral.bit_count && + (prev_type->data.integral.is_wrapping || !cur_type->data.integral.is_wrapping))) + { + continue; } else if (prev_type->id == TypeTableEntryIdFloat && cur_type->id == TypeTableEntryIdFloat) { diff --git a/test/run_tests.cpp b/test/run_tests.cpp @@ -1354,6 +1354,13 @@ fn mul(a: u16, b: u16) -> u16 { ".tmp_source.zig:5:1: error: function evaluation caused overflow", ".tmp_source.zig:3:18: note: called from here", ".tmp_source.zig:6:7: note: overflow occurred here"); + + add_compile_fail_case("add incompatible int types", R"SOURCE( +fn add(x: i8w, y: i32) { + const z = x + y; +} + )SOURCE", 1, ".tmp_source.zig:3:17: error: incompatible types: 'i8w' and 'i32'"); + } ////////////////////////////////////////////////////////////////////////////// diff --git a/test/self_hosted.zig b/test/self_hosted.zig @@ -1534,3 +1534,20 @@ fn shl_with_overflow() { assert(!@shl_with_overflow(u16, 0b0010111111111111, 2, &result)); assert(result == 0b1011111111111100); } + +#attribute("test") +fn combine_non_wrap_with_wrap() { + const x: i32 = 123; + const y: i32w = 456; + const z = x + y; + const z2 = y + x; + assert(@typeof(z) == i32w); + assert(@typeof(z2) == i32w); + + const a: i8 = 123; + const b: i32w = 456; + const c = b + a; + const d = a + b; + assert(@typeof(c) == i32w); + assert(@typeof(d) == i32w); +}