From 436ef127307e036285c01ca723e666de881ec4e3 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 3 Apr 2020 18:49:12 +0200 Subject: [PATCH] cgen: str(): handle empty structs --- vlib/v/gen/cgen.v | 12 +++++++----- vlib/v/tests/struct_test.v | 4 ++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index f65cabf0f7..d63baf476a 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -49,7 +49,7 @@ mut: assign_op token.Kind // *=, =, etc (for array_set) defer_stmts []ast.DeferStmt defer_ifdef string - str_types []int // types that need automatic str() generation + str_types []string// types that need automatic str() generation threaded_fns []string // for generating unique wrapper types and fns for `go xxx()` } @@ -2423,12 +2423,12 @@ fn (g mut Gen) fn_call(node ast.CallExpr) { typ := node.args[0].typ mut styp := g.typ(typ) sym := g.table.get_type_symbol(typ) - if !sym.has_method('str') && !(int(typ) in g.str_types) { + if !sym.has_method('str') && !(styp in g.str_types) { // Generate an automatic str() method if this type doesn't have it already if table.type_is_ptr(typ) { styp = styp.replace('*', '') } - g.str_types << typ + g.str_types << styp g.gen_str_for_type(sym, styp) } if g.autofree && !table.type_is_optional(typ) { @@ -2882,9 +2882,11 @@ fn (g mut Gen) gen_str_for_type(sym table.TypeSymbol, styp string) { for field in info.fields { fmt := type_to_fmt(field.typ) g.definitions.write('\t$field.name: $fmt\\n') - } - g.definitions.write('\\n}", ') + g.definitions.write('\\n}"') + if info.fields.len > 0 { + g.definitions.write(', ') + } for i, field in info.fields { g.definitions.write('a.' + field.name) if field.typ == table.string_type { diff --git a/vlib/v/tests/struct_test.v b/vlib/v/tests/struct_test.v index 312e79038a..8ce1d08ee1 100644 --- a/vlib/v/tests/struct_test.v +++ b/vlib/v/tests/struct_test.v @@ -59,7 +59,11 @@ struct ReservedKeywords { fn test_empty_struct() { d := &Empty{} + d2 := Empty{} + println('&empty:') println(d) // != voidptr(0) + println('empty:') + println(d2) // empty struct print println(sizeof(Empty)) // == 0 }