test: fix alias type errors and type_test.v

pull/4934/head
yuyi 2020-05-19 20:04:51 +08:00 committed by GitHub
parent bd85d2fd2a
commit 74686d0ec4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 37 deletions

View File

@ -9,7 +9,6 @@ const (
'vlib/v/tests/enum_bitfield_test.v',
'vlib/v/tests/num_lit_call_method_test.v',
'vlib/v/tests/pointers_test.v',
'vlib/v/tests/type_test.v',
'vlib/v/tests/pointers_str_test.v',
'vlib/arrays/arrays_test.v',
'vlib/net/http/http_httpbin_test.v',

View File

@ -308,8 +308,19 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type {
c.error('unknown struct: $type_sym.name', struct_init.pos)
}
// string & array are also structs but .kind of string/array
.struct_, .string, .array {
info := type_sym.info as table.Struct
.struct_, .string, .array, .alias {
mut info := table.Struct{}
if type_sym.kind == .alias {
info_t := type_sym.info as table.Alias
sym := c.table.get_type_symbol(info_t.parent_typ)
if sym.kind != .struct_ {
c.error('alias type name: $sym.name is not struct type', struct_init.pos)
}
info = sym.info as table.Struct
} else {
info = type_sym.info as table.Struct
}
if struct_init.is_short && struct_init.fields.len > info.fields.len {
c.error('too many fields', struct_init.pos)
}

View File

@ -318,7 +318,7 @@ fn (g &Gen) base_type(t table.Type) string {
nr_muls := t.nr_muls()
if nr_muls > 0 {
styp += strings.repeat(`*`, nr_muls)
}
}
return styp
}

View File

@ -1412,7 +1412,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
parent_idx: pid
mod: p.mod
info: table.Alias{
foo: ''
parent_typ: parent_type
is_c: parent_name.len > 2 && parent_name[0] == `C` && parent_name[1] == `.`
}
is_public: is_pub

View File

@ -574,8 +574,8 @@ pub:
pub struct Alias {
pub:
foo string
is_c bool
parent_typ Type
is_c bool
}
// NB: FExpr here is a actually an ast.Expr .

View File

@ -1,33 +1,3 @@
struct Human {
name string
}
fn (h Human) str() string {
return 'Human: $h.name'
}
type Person Human
fn test_type_print() {
p := Person{
name: 'Bilbo'
}
println(p)
assert p.str() == 'Human: Bilbo'
}
fn (h Person) str() string {
return 'Person: $h.name'
}
fn test_person_str() {
p := Person{
name: 'Bilbo'
}
println(p)
assert p.str() == 'Person: Bilbo'
}
struct Foo {
}

View File

@ -0,0 +1,29 @@
struct Human {
name string
}
fn (h Human) str() string {
return 'Human: $h.name'
}
type Person Human
fn (h Person) str() string {
return 'Person: $h.name'
}
fn test_type_print() {
p := Human{
name: 'Bilbo'
}
println(p)
assert p.str() == 'Human: Bilbo'
}
fn test_person_str() {
p := Person{
name: 'Bilbo'
}
println(p)
assert p.str() == 'Person: Bilbo'
}