1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

type alias check

This commit is contained in:
BigBlack 2019-12-15 03:01:20 +08:00 committed by Alexander Medvednikov
parent 2e23592264
commit fc33f9d49c
3 changed files with 30 additions and 4 deletions

View File

@ -49,11 +49,12 @@ fn (p mut Parser) bterm() string {
p.expected_type = typ p.expected_type = typ
is_str := typ=='string' && !p.is_sql is_str := typ=='string' && !p.is_sql
is_ustr := typ=='ustring' 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 in ['f64_abs', 'f32_abs']) &&
p.cur_fn.name != 'eq' p.cur_fn.name != 'eq'
is_array := typ.starts_with('array_') is_array := typ.starts_with('array_')
expr_type := typ expr_type := base
tok := p.tok tok := p.tok
/* /*
if tok == .assign { if tok == .assign {
@ -472,7 +473,7 @@ fn (p mut Parser) expression() string {
if typ == 'bool' { if typ == 'bool' {
p.error('operator ${p.tok.str()} not defined on 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) p.check_space(p.tok)
if is_str && tok_op == .plus && !p.is_js { if is_str && tok_op == .plus && !p.is_js {
p.is_alloc = true p.is_alloc = true

View File

@ -601,7 +601,9 @@ fn (p mut Parser) check_types2(got_, expected_ string, throw bool) bool {
p.cur_fn.typ = got p.cur_fn.typ = got
return true return true
} }
if throw && p.base_type(got) == p.base_type(expected) {
return true
}
// variadic // variadic
if expected.starts_with('varg_') { if expected.starts_with('varg_') {
expected = expected[5..] expected = expected[5..]
@ -728,6 +730,14 @@ fn (p mut Parser) check_types2(got_, expected_ string, throw bool) bool {
return true 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 // throw by default
fn (p mut Parser) check_types(got, expected string) bool { fn (p mut Parser) check_types(got, expected string) bool {
if p.first_pass() { return true } if p.first_pass() { return true }

View File

@ -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
}