From fe8286c53cb2de7dd6054d98d5ce4723281ebd0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Sat, 29 Aug 2020 01:59:07 +0200 Subject: [PATCH] checker: implement implicit conversions/promotions to `rune` (#6249) --- vlib/v/checker/check_types.v | 4 +-- vlib/v/table/atypes.v | 44 ++++++++++++------------------ vlib/v/tests/type_promotion_test.v | 18 ++++++++++++ 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index dcae17c3fd..09d2993065 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -217,8 +217,8 @@ fn (c &Checker) promote_num(left_type, right_type table.Type) table.Type { } } else if idx_lo >= table.byte_type_idx { // both operands are unsigned return type_hi - } else if idx_lo >= table.i8_type_idx && idx_hi <= table.i64_type_idx { // both signed - return type_hi + } else if idx_lo >= table.i8_type_idx && (idx_hi <= table.i64_type_idx || idx_hi == table.rune_type_idx) { // both signed + return if idx_lo == table.i64_type_idx { type_lo } else { type_hi } } else if idx_hi - idx_lo < (table.byte_type_idx - table.i8_type_idx) { return type_lo // conversion unsigned -> signed if signed type is larger } else { diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index 06b52bb19c..232f1d816c 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -255,12 +255,11 @@ pub const ( array_type_idx = 20 map_type_idx = 21 chan_type_idx = 22 - any_type_idx = 23 - // t_type_idx = 23 - any_flt_type_idx = 24 - any_int_type_idx = 25 - sizet_type_idx = 26 - rune_type_idx = 27 + sizet_type_idx = 23 + rune_type_idx = 24 + any_type_idx = 25 + any_flt_type_idx = 26 + any_int_type_idx = 27 ) pub const ( @@ -300,11 +299,10 @@ pub const ( array_type = new_type(array_type_idx) map_type = new_type(map_type_idx) chan_type = new_type(chan_type_idx) + rune_type = new_type(rune_type_idx) any_type = new_type(any_type_idx) - // t_type = new_type(t_type_idx) any_flt_type = new_type(any_flt_type_idx) any_int_type = new_type(any_int_type_idx) - rune_type = new_type(rune_type_idx) ) pub const ( @@ -569,18 +567,24 @@ pub fn (mut t Table) register_builtin_type_symbols() { source_name: 'chan' mod: 'builtin' }) + t.register_type_symbol({ + kind: .size_t + name: 'size_t' + source_name: 'size_t' + mod: 'builtin' + }) + t.register_type_symbol({ + kind: .rune + name: 'rune' + source_name: 'rune' + mod: 'builtin' + }) t.register_type_symbol({ kind: .any name: 'any' source_name: 'any' mod: 'builtin' }) - // t.register_type_symbol({ - // kind: .any - // name: 'T' - // mod: 'builtin' - // is_public: true - // }) t.register_type_symbol({ kind: .any_float name: 'any_float' @@ -593,18 +597,6 @@ pub fn (mut t Table) register_builtin_type_symbols() { source_name: 'any_int' mod: 'builtin' }) - t.register_type_symbol({ - kind: .size_t - name: 'size_t' - source_name: 'size_t' - mod: 'builtin' - }) - t.register_type_symbol({ - kind: .size_t - name: 'rune' - source_name: 'rune' - mod: 'builtin' - }) // TODO: remove. for v1 map compatibility map_string_string_idx := t.find_or_register_map(string_type, string_type) map_string_int_idx := t.find_or_register_map(string_type, int_type) diff --git a/vlib/v/tests/type_promotion_test.v b/vlib/v/tests/type_promotion_test.v index c3a0e770ed..bb72b41a46 100644 --- a/vlib/v/tests/type_promotion_test.v +++ b/vlib/v/tests/type_promotion_test.v @@ -80,3 +80,21 @@ fn test_f32_int() { d := u64(300) assert d + c == 316.75 } + +fn test_rune() { + a := 3 + mut b := rune(67) + b = a + assert b == rune(3) + b = rune(67) + mut x := 5 + x = int(b) + assert x == 67 + c := b + a + assert c == rune(70) + assert typeof(c) == 'rune' + d := i64(12) + e := b + d + assert e == i64(79) + assert typeof(e) == 'i64' +}