zig

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

commit ea18f894f524fdc82dc09ea9c6abf8feb93b04e8 (tree)
parent 03113d92467a99658c2606a21821b28b19dfdded
Author: g-w1 <58830309+g-w1@users.noreply.github.com>
Date:   Tue, 22 Dec 2020 17:05:42 -0500

Peer type resolution with unsigned ints and larger signed ints


Diffstat:
Msrc/stage1/ir.cpp | 10++++++----
Mtest/stage1/behavior/cast.zig | 9+++++++++
2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp @@ -12706,11 +12706,13 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT } if (prev_type->id == ZigTypeIdInt && - cur_type->id == ZigTypeIdInt && - prev_type->data.integral.is_signed == cur_type->data.integral.is_signed) + cur_type->id == ZigTypeIdInt) { - if (cur_type->data.integral.bit_count > prev_type->data.integral.bit_count) { - prev_inst = cur_inst; + if ((prev_type->data.integral.is_signed == cur_type->data.integral.is_signed) || + (cur_type->data.integral.is_signed && !prev_type->data.integral.is_signed)) { + if (cur_type->data.integral.bit_count > prev_type->data.integral.bit_count) { + prev_inst = cur_inst; + } } continue; } diff --git a/test/stage1/behavior/cast.zig b/test/stage1/behavior/cast.zig @@ -869,6 +869,15 @@ test "peer type resolve string lit with sentinel-terminated mutable slice" { comptime expect(@TypeOf("hi", slice) == [:0]const u8); } +test "peer type unsigned int to signed" { + var w: u31 = 5; + var x: u8 = 7; + var y: i32 = -5; + var a = w + y + x; + comptime expect(@TypeOf(a) == i32); + expect(a == 7); +} + test "peer type resolve array pointers, one of them const" { var array1: [4]u8 = undefined; const array2: [5]u8 = undefined;