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

sort structs: handle fixed size arrays

This commit is contained in:
Alexander Medvednikov 2019-12-12 13:51:05 +03:00
parent 576618d8cc
commit 88ec9c235d
3 changed files with 36 additions and 4 deletions

View File

@ -14,6 +14,9 @@ fn test_add() {
assert a.ends_with('bbbbb')
a += '123'
assert a.ends_with('3')
mut foo := Foo{0, 'hi'}
foo.str += '!'
assert foo.str == 'hi!'
}
fn test_ends_with() {
@ -418,6 +421,8 @@ fn test_reverse() {
struct Foo {
bar int
mut:
str string
}
fn (f Foo) baz() string {
@ -584,6 +589,7 @@ fn test_inter_before_comp_if() {
$if linux {
println(s)
}
assert s == '123'
}
fn test_double_quote_inter() {

View File

@ -376,19 +376,22 @@ fn sort_structs(types []Type) []Type {
mut dep_graph := new_dep_graph()
// types name list
mut type_names := []string
for t in types {
type_names << t.name
for typ in types {
type_names << typ.name
}
// loop over types
for t in types {
// create list of deps
mut field_deps := []string
for field in t.fields {
// Need to handle fixed size arrays as well (`[10]Point`)
ft := if field.typ.starts_with('[') { field.typ.all_after(']') }
else { field.typ }
// skip if not in types list or already in deps
if !(field.typ in type_names) || field.typ in field_deps {
if !(ft in type_names) || ft in field_deps {
continue
}
field_deps << field.typ
field_deps << ft//field.typ
}
// add type and dependant types to graph
dep_graph.add(t.name, field_deps)

View File

@ -154,3 +154,26 @@ fn test_assoc_with_constants() {
assert again.b == 22
}
/*
[typedef]
struct C.fixed {
points [10]C.point
}
[typedef]
struct C.point {
x int
y int
}
fn test_fixed_field() {
f := &C.fixed{}
p := f.points[0]
//f.nums[0] = 10
//println(f.nums[0])
println(p.x)
//nums: [10]int
//}
}
*/