test: fix alias type errors and type_test.v
parent
bd85d2fd2a
commit
74686d0ec4
|
@ -9,7 +9,6 @@ const (
|
||||||
'vlib/v/tests/enum_bitfield_test.v',
|
'vlib/v/tests/enum_bitfield_test.v',
|
||||||
'vlib/v/tests/num_lit_call_method_test.v',
|
'vlib/v/tests/num_lit_call_method_test.v',
|
||||||
'vlib/v/tests/pointers_test.v',
|
'vlib/v/tests/pointers_test.v',
|
||||||
'vlib/v/tests/type_test.v',
|
|
||||||
'vlib/v/tests/pointers_str_test.v',
|
'vlib/v/tests/pointers_str_test.v',
|
||||||
'vlib/arrays/arrays_test.v',
|
'vlib/arrays/arrays_test.v',
|
||||||
'vlib/net/http/http_httpbin_test.v',
|
'vlib/net/http/http_httpbin_test.v',
|
||||||
|
|
|
@ -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)
|
c.error('unknown struct: $type_sym.name', struct_init.pos)
|
||||||
}
|
}
|
||||||
// string & array are also structs but .kind of string/array
|
// string & array are also structs but .kind of string/array
|
||||||
.struct_, .string, .array {
|
.struct_, .string, .array, .alias {
|
||||||
info := type_sym.info as table.Struct
|
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 {
|
if struct_init.is_short && struct_init.fields.len > info.fields.len {
|
||||||
c.error('too many fields', struct_init.pos)
|
c.error('too many fields', struct_init.pos)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1412,7 +1412,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
|
||||||
parent_idx: pid
|
parent_idx: pid
|
||||||
mod: p.mod
|
mod: p.mod
|
||||||
info: table.Alias{
|
info: table.Alias{
|
||||||
foo: ''
|
parent_typ: parent_type
|
||||||
is_c: parent_name.len > 2 && parent_name[0] == `C` && parent_name[1] == `.`
|
is_c: parent_name.len > 2 && parent_name[0] == `C` && parent_name[1] == `.`
|
||||||
}
|
}
|
||||||
is_public: is_pub
|
is_public: is_pub
|
||||||
|
|
|
@ -574,8 +574,8 @@ pub:
|
||||||
|
|
||||||
pub struct Alias {
|
pub struct Alias {
|
||||||
pub:
|
pub:
|
||||||
foo string
|
parent_typ Type
|
||||||
is_c bool
|
is_c bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NB: FExpr here is a actually an ast.Expr .
|
// NB: FExpr here is a actually an ast.Expr .
|
||||||
|
|
|
@ -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 {
|
struct Foo {
|
||||||
}
|
}
|
||||||
|
|
|
@ -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'
|
||||||
|
}
|
Loading…
Reference in New Issue