From 3fa9351c3ea0cacf445bb39315cd130d7530cd30 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sat, 9 Apr 2022 12:57:09 +0100 Subject: [PATCH] don't error for types with the same size --- vlib/v/ast/types.v | 17 +++++++++++++++++ vlib/v/ast/types_test.v | 6 ++++++ vlib/v/checker/checker.v | 3 ++- vlib/x/ttf/render_bmp.v | 2 +- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index 00369c12f7..9b48ea7a9e 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -374,6 +374,23 @@ pub fn (typ Type) is_unsigned() bool { return typ.idx() in ast.unsigned_integer_type_idxs } +pub fn (typ Type) flip_signedness() Type { + r := match typ { + i8_type {byte_type} + i16_type {u16_type} + int_type {u32_type} + isize_type {usize_type} + i64_type {u64_type} + byte_type {i8_type} + u16_type {i16_type} + u32_type {int_type} + usize_type {isize_type} + u64_type {i64_type} + else {typ} + } + return r +} + [inline] pub fn (typ Type) is_int_literal() bool { return int(typ) == ast.int_literal_type_idx diff --git a/vlib/v/ast/types_test.v b/vlib/v/ast/types_test.v index fd98e33c8c..67e29459b4 100644 --- a/vlib/v/ast/types_test.v +++ b/vlib/v/ast/types_test.v @@ -79,3 +79,9 @@ fn test_derive() { assert t2.has_flag(ast.TypeFlag.generic) == true assert t2.nr_muls() == 10 } + +fn test_flip_signedness() { + assert ast.i8_type.flip_signedness() == ast.byte_type + assert ast.u16_type.flip_signedness() == ast.i16_type + assert ast.isize_type.flip_signedness() == ast.usize_type +} diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 148fa8a9a7..4379c0ec60 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -636,7 +636,8 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { rt := c.table.sym(right_type).name c.error('negative value cannot be compared with `$rt`', node.left.pos) } - } else if is_left_type_signed != is_right_type_signed { + } else if is_left_type_signed != is_right_type_signed && + left_type.flip_signedness() != right_type { // prevent e.g. `u16(-1) == int(-1)` which is false in C if (is_right_type_signed && left_type in ast.int_promoted_type_idxs) || (is_left_type_signed && right_type in ast.int_promoted_type_idxs) { diff --git a/vlib/x/ttf/render_bmp.v b/vlib/x/ttf/render_bmp.v index d7a74f79d3..4539617ca2 100644 --- a/vlib/x/ttf/render_bmp.v +++ b/vlib/x/ttf/render_bmp.v @@ -778,7 +778,7 @@ pub fn (mut bmp BitMap) draw_glyph(index u16) (int, int) { } } - if count == glyph.contour_ends[c] { + if count == int(glyph.contour_ends[c]) { // dprintln("count == glyph.contour_ends[count]") if s == 2 { // final point was off-curve. connect to start