From 635f045b1451d32ea4acebddcc1e6ef32975ab49 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 12 Jun 2021 14:35:33 +0300 Subject: [PATCH] ast: make is_int() work with aliases --- vlib/v/ast/types.v | 6 +++++- vlib/v/checker/checker.v | 2 +- vlib/v/tests/type_alias_test.v | 4 ++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index e5d08cb25a..159cde105e 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -635,7 +635,11 @@ pub fn (t &TypeSymbol) is_pointer() bool { [inline] pub fn (t &TypeSymbol) is_int() bool { - return t.kind in [.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .int_literal, .rune] + res := t.kind in [.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .int_literal, .rune] + if !res && t.kind == .alias { + return (t.info as Alias).parent_type.is_number() + } + return res } [inline] diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 596c39daaa..42ea814576 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3432,7 +3432,7 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { ast.PrefixExpr { // Do now allow `*x = y` outside `unsafe` if left.op == .mul { - if !c.inside_unsafe { + if !c.inside_unsafe && !c.pref.translated { c.error('modifying variables via dereferencing can only be done in `unsafe` blocks', node.pos) } else { diff --git a/vlib/v/tests/type_alias_test.v b/vlib/v/tests/type_alias_test.v index bada874624..e5601d215a 100644 --- a/vlib/v/tests/type_alias_test.v +++ b/vlib/v/tests/type_alias_test.v @@ -22,6 +22,10 @@ fn test_type_alias_v2() { assert f + f32(0.6) == f32(8.0) g := Myf64_2(10.4) assert g + 0.5 == 10.9 + // test ++ on an alias + mut x := Myint(10) + x++ + assert x == 11 } struct Mystruct {