From b0f66a4e0545e6496c696be45fad297a2effa4ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Tue, 2 Jun 2020 17:00:14 +0200 Subject: [PATCH] checker: permit int -> f32, int64 -> f64 and similar promotions again --- doc/docs.md | 9 ++++----- vlib/v/checker/check_types.v | 8 ++------ vlib/v/tests/type_promotion_test.v | 13 +++++++++++++ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/doc/docs.md b/doc/docs.md index 8f7b77e9c7..f8e022f3bc 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -292,15 +292,14 @@ Please note that unlike C and Go, `int` is always a 32 bit integer. There is an exceptions to the rule that all operators in V must have values of the same type on both sides. A small primitive type on one side can be automatically promoted if it fits -completely into the data range of the type on the other side, i.e. when -the promotion does not result in any data loss. +completely into the data range of the type on the other side. These are the allowed possibilities: ``` i8 → i16 → int → i64 - ↘ ↘ - f32 → f64 - ↗ ↗ + ↘ ↘ + f32 → f64 + ↗ ↗ byte → u16 → u32 → u64 ⬎ ↘ ↘ ↘ ptr i8 → i16 → int → i64 ⬏ diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 3aec2fbbe8..b87247606d 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -183,17 +183,13 @@ fn (c &Checker) promote_num(left_type, right_type table.Type) table.Type { } } else if type_hi.is_float() { if idx_hi == table.f32_type_idx { - if idx_lo in [table.int_type_idx, table.i64_type_idx, table.u32_type_idx, table.u64_type_idx] { - return table.void_type - } else { - return type_hi - } - } else { // f64, any_flt if idx_lo in [table.i64_type_idx, table.u64_type_idx] { return table.void_type } else { return type_hi } + } else { // f64, any_flt + return type_hi } } else if idx_lo >= table.byte_type_idx { // both operands are unsigned return type_hi diff --git a/vlib/v/tests/type_promotion_test.v b/vlib/v/tests/type_promotion_test.v index c3f00f4deb..c3a0e770ed 100644 --- a/vlib/v/tests/type_promotion_test.v +++ b/vlib/v/tests/type_promotion_test.v @@ -67,3 +67,16 @@ fn test_struct_init_ref_return() { assert fabs(fabs(x.get()) - 3.890949634755863) < 1.e-6 } +fn test_f32_int() { + x := f32(15.25) + y := -3 + assert x + y == 12.25 + assert y + x == 12.25 + a := u32(34) + assert a + x == 49.25 + b := i64(-17) + c := 16.75 + assert c + b == -0.25 + d := u64(300) + assert d + c == 316.75 +}