From fc33f9d49cce4d8764d2d8bf45ef75936d0dc6a8 Mon Sep 17 00:00:00 2001 From: BigBlack <840206@qq.com> Date: Sun, 15 Dec 2019 03:01:20 +0800 Subject: [PATCH] type alias check --- vlib/compiler/expression.v | 7 ++++--- vlib/compiler/table.v | 12 +++++++++++- vlib/compiler/tests/type_alias_test.v | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 vlib/compiler/tests/type_alias_test.v diff --git a/vlib/compiler/expression.v b/vlib/compiler/expression.v index ad75dbdbc3..655d550abe 100644 --- a/vlib/compiler/expression.v +++ b/vlib/compiler/expression.v @@ -49,11 +49,12 @@ fn (p mut Parser) bterm() string { p.expected_type = typ is_str := typ=='string' && !p.is_sql is_ustr := typ=='ustring' - is_float := typ[0] == `f` && (typ in ['f64', 'f32']) && + base := p.base_type(typ) + is_float := base[0] == `f` && (base in ['f64', 'f32']) && !(p.cur_fn.name in ['f64_abs', 'f32_abs']) && p.cur_fn.name != 'eq' is_array := typ.starts_with('array_') - expr_type := typ + expr_type := base tok := p.tok /* if tok == .assign { @@ -472,7 +473,7 @@ fn (p mut Parser) expression() string { if typ == 'bool' { p.error('operator ${p.tok.str()} not defined on bool ') } - is_num := typ.contains('*') || is_number_type(typ) + is_num := typ.contains('*') || is_number_type(typ) || is_number_type(p.base_type(typ)) p.check_space(p.tok) if is_str && tok_op == .plus && !p.is_js { p.is_alloc = true diff --git a/vlib/compiler/table.v b/vlib/compiler/table.v index fa39d0493e..6ac4a53735 100644 --- a/vlib/compiler/table.v +++ b/vlib/compiler/table.v @@ -601,7 +601,9 @@ fn (p mut Parser) check_types2(got_, expected_ string, throw bool) bool { p.cur_fn.typ = got return true } - + if throw && p.base_type(got) == p.base_type(expected) { + return true + } // variadic if expected.starts_with('varg_') { expected = expected[5..] @@ -728,6 +730,14 @@ fn (p mut Parser) check_types2(got_, expected_ string, throw bool) bool { return true } + fn (p mut Parser) base_type(name string) string { + typ := p.find_type(name) + if typ.parent != '' { + return p.base_type(typ.parent) + } + return name + } + // throw by default fn (p mut Parser) check_types(got, expected string) bool { if p.first_pass() { return true } diff --git a/vlib/compiler/tests/type_alias_test.v b/vlib/compiler/tests/type_alias_test.v new file mode 100644 index 0000000000..c53fff6df8 --- /dev/null +++ b/vlib/compiler/tests/type_alias_test.v @@ -0,0 +1,15 @@ + +type Myint int +type Myf32 f32 +type Myf64 f64 + +fn test_type_alias() { + i := Myint(10) + assert i + 100 == 110 + + f := Myf32(1.0) + assert f + 3.14 == 4.14 + + + +} \ No newline at end of file