cgen: fix string_interpolation_struct_test.v error

pull/4460/head
yuyi 2020-04-17 07:45:42 +08:00 committed by GitHub
parent 7bfc3ef767
commit 8bb11d9035
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 4 deletions

View File

@ -29,7 +29,6 @@ const (
'vlib/v/tests/pointers_test.v',
'vlib/v/tests/repl/repl_test.v',
'vlib/v/tests/string_interpolation_array_of_structs_test.v',
'vlib/v/tests/string_interpolation_struct_test.v',
'vlib/v/tests/string_interpolation_variadic_test.v',
'vlib/v/tests/type_test.v',
'vlib/v/tests/valgrind/valgrind_test.v', // ubuntu-musl only

View File

@ -2253,6 +2253,8 @@ fn (var g Gen) string_inter_literal(node ast.StringInterLiteral) {
g.write('%.*s')
} else if node.expr_types[i] in [table.f32_type, table.f64_type] {
g.write('%g')
} else if sym.kind == .struct_ && !sym.has_method('str') {
g.write('%.*s')
} else {
g.write('%d')
}
@ -2324,6 +2326,16 @@ fn (var g Gen) string_inter_literal(node ast.StringInterLiteral) {
g.write('${styp}_str(')
g.expr(expr)
g.write(').str')
} else if sym.kind == .struct_ && !sym.has_method('str') {
styp := g.typ(node.expr_types[i])
g.gen_str_for_type(sym, styp)
g.write('${styp}_str(')
g.expr(expr)
g.write(',0)')
g.write('.len, ')
g.write('${styp}_str(')
g.expr(expr)
g.write(',0).str')
} else {
g.expr(expr)
}
@ -2936,7 +2948,7 @@ fn (var g Gen) gen_str_for_struct(info table.Struct, styp string) {
g.definitions.write(', ')
for i, field in info.fields {
sym := g.table.get_type_symbol(field.typ)
if sym.kind == .struct_ {
if sym.kind in [.struct_, .array, .array_fixed] {
field_styp := g.typ(field.typ)
second_str_param := if sym.has_method('str') { '' } else { ', indent_count + 1' }
g.definitions.write('indents.len, indents.str, ${field_styp}_str(it.$field.name$second_str_param).len, ${field_styp}_str(it.$field.name$second_str_param).str')
@ -2958,7 +2970,7 @@ fn (var g Gen) gen_str_for_struct(info table.Struct, styp string) {
fn (g Gen) type_to_fmt(typ table.Type) string {
sym := g.table.get_type_symbol(typ)
if sym.kind == .struct_ {
if sym.kind in [.struct_, .array, .array_fixed] {
return '%.*s'
} else if typ == table.string_type {
return "\'%.*s\'"

View File

@ -11,7 +11,7 @@ fn test_default_struct_string_interpolation() {
superman := Man{'Superman', 30, ['flying', 'fighting evil', 'being nice']}
s := '$superman'
assert s.contains('Man {')
assert s.contains('name: Superman')
assert s.contains('name: \'Superman\'')
assert s.contains('age: 30')
assert s.contains('interests: [')
assert s.contains('"being nice"')