From fb4cfa72cdddcddbe1e13a1909c16651b51c07e4 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 14 Apr 2020 01:18:22 +0800 Subject: [PATCH] cgen: fix str() for arrays --- vlib/v/gen/cgen.v | 39 ++++++++++++++++++----------- vlib/v/tests/array_to_string_test.v | 7 ++++++ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 962c24eae2..3c12c81abf 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -2405,18 +2405,11 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) { verror('only V strings can be formatted with a ${sfmt} format') } g.write('%' + sfmt[1..]) - } else if node.expr_types[i] in [table.string_type, table.bool_type] || sym.kind == - .enum_ { + } else if node.expr_types[i] in [table.string_type, table.bool_type, table.f32_type, + table.f64_type] || sym.kind in [.enum_, .array, .array_fixed] { g.write('%.*s') } else { - match node.exprs[i] { - ast.EnumVal { - g.write('%.*s') - } - else { - g.write('%d') - } - } + g.write('%d') } } g.write('", ') @@ -2428,21 +2421,25 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) { if fspec == `s` && node.expr_types[i] == table.string_type { g.expr(expr) g.write('.str') - } else { + } + else { g.expr(expr) } - } else if node.expr_types[i] == table.string_type { + } + else if node.expr_types[i] == table.string_type { // `name.str, name.len,` g.expr(expr) g.write('.len, ') g.expr(expr) g.write('.str') - } else if node.expr_types[i] == table.bool_type { + } + else if node.expr_types[i] == table.bool_type { g.expr(expr) g.write(' ? 4 : 5, ') g.expr(expr) g.write(' ? "true" : "false"') - } else { + } + else { sym := g.table.get_type_symbol(node.expr_types[i]) if sym.kind == .enum_ { is_var := match node.exprs[i] { @@ -2475,7 +2472,19 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) { g.enum_expr(expr) g.write('"') } - } else { + } + else if node.expr_types[i] in [table.f32_type, table.f64_type, table.array_type, + table.map_type] || sym.kind in [.array, .array_fixed] { + styp := g.typ(node.expr_types[i]) + g.write('${styp}_str(') + g.expr(expr) + g.write(')') + g.write('.len, ') + g.write('${styp}_str(') + g.expr(expr) + g.write(').str') + } + else { g.expr(expr) } } diff --git a/vlib/v/tests/array_to_string_test.v b/vlib/v/tests/array_to_string_test.v index 3c83e68976..3993f9687d 100644 --- a/vlib/v/tests/array_to_string_test.v +++ b/vlib/v/tests/array_to_string_test.v @@ -3,3 +3,10 @@ fn test_array_to_string_conversion() { arr := ['1', '2', '3', '4'] assert arr.str() == expected } + +fn test_interpolation_array_to_string() { + a := ['1', '2', '3'] + assert '$a' == '["1", "2", "3"]' + b := ['a', 'b'] + assert '$b' == '["a", "b"]' +}