From 88ec9c235dfbb2a446e84b50cddcc0b7adc8ccbd Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 12 Dec 2019 13:51:05 +0300 Subject: [PATCH] sort structs: handle fixed size arrays --- vlib/builtin/string_test.v | 6 ++++++ vlib/compiler/cgen.v | 11 +++++++---- vlib/compiler/tests/struct_test.v | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index d16aceb112..1c7b0336b5 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -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() { diff --git a/vlib/compiler/cgen.v b/vlib/compiler/cgen.v index 3be78539a2..541680be5c 100644 --- a/vlib/compiler/cgen.v +++ b/vlib/compiler/cgen.v @@ -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) diff --git a/vlib/compiler/tests/struct_test.v b/vlib/compiler/tests/struct_test.v index c9cb622347..0086d5567a 100644 --- a/vlib/compiler/tests/struct_test.v +++ b/vlib/compiler/tests/struct_test.v @@ -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 + //} +} +*/ +