cgen: fix string_interpolation_struct_test.v error
parent
7bfc3ef767
commit
8bb11d9035
|
@ -29,7 +29,6 @@ const (
|
||||||
'vlib/v/tests/pointers_test.v',
|
'vlib/v/tests/pointers_test.v',
|
||||||
'vlib/v/tests/repl/repl_test.v',
|
'vlib/v/tests/repl/repl_test.v',
|
||||||
'vlib/v/tests/string_interpolation_array_of_structs_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/string_interpolation_variadic_test.v',
|
||||||
'vlib/v/tests/type_test.v',
|
'vlib/v/tests/type_test.v',
|
||||||
'vlib/v/tests/valgrind/valgrind_test.v', // ubuntu-musl only
|
'vlib/v/tests/valgrind/valgrind_test.v', // ubuntu-musl only
|
||||||
|
|
|
@ -2253,6 +2253,8 @@ fn (var g Gen) string_inter_literal(node ast.StringInterLiteral) {
|
||||||
g.write('%.*s')
|
g.write('%.*s')
|
||||||
} else if node.expr_types[i] in [table.f32_type, table.f64_type] {
|
} else if node.expr_types[i] in [table.f32_type, table.f64_type] {
|
||||||
g.write('%g')
|
g.write('%g')
|
||||||
|
} else if sym.kind == .struct_ && !sym.has_method('str') {
|
||||||
|
g.write('%.*s')
|
||||||
} else {
|
} else {
|
||||||
g.write('%d')
|
g.write('%d')
|
||||||
}
|
}
|
||||||
|
@ -2324,6 +2326,16 @@ fn (var g Gen) string_inter_literal(node ast.StringInterLiteral) {
|
||||||
g.write('${styp}_str(')
|
g.write('${styp}_str(')
|
||||||
g.expr(expr)
|
g.expr(expr)
|
||||||
g.write(').str')
|
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 {
|
} else {
|
||||||
g.expr(expr)
|
g.expr(expr)
|
||||||
}
|
}
|
||||||
|
@ -2936,7 +2948,7 @@ fn (var g Gen) gen_str_for_struct(info table.Struct, styp string) {
|
||||||
g.definitions.write(', ')
|
g.definitions.write(', ')
|
||||||
for i, field in info.fields {
|
for i, field in info.fields {
|
||||||
sym := g.table.get_type_symbol(field.typ)
|
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)
|
field_styp := g.typ(field.typ)
|
||||||
second_str_param := if sym.has_method('str') { '' } else { ', indent_count + 1' }
|
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')
|
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 {
|
fn (g Gen) type_to_fmt(typ table.Type) string {
|
||||||
sym := g.table.get_type_symbol(typ)
|
sym := g.table.get_type_symbol(typ)
|
||||||
if sym.kind == .struct_ {
|
if sym.kind in [.struct_, .array, .array_fixed] {
|
||||||
return '%.*s'
|
return '%.*s'
|
||||||
} else if typ == table.string_type {
|
} else if typ == table.string_type {
|
||||||
return "\'%.*s\'"
|
return "\'%.*s\'"
|
||||||
|
|
|
@ -11,7 +11,7 @@ fn test_default_struct_string_interpolation() {
|
||||||
superman := Man{'Superman', 30, ['flying', 'fighting evil', 'being nice']}
|
superman := Man{'Superman', 30, ['flying', 'fighting evil', 'being nice']}
|
||||||
s := '$superman'
|
s := '$superman'
|
||||||
assert s.contains('Man {')
|
assert s.contains('Man {')
|
||||||
assert s.contains('name: Superman')
|
assert s.contains('name: \'Superman\'')
|
||||||
assert s.contains('age: 30')
|
assert s.contains('age: 30')
|
||||||
assert s.contains('interests: [')
|
assert s.contains('interests: [')
|
||||||
assert s.contains('"being nice"')
|
assert s.contains('"being nice"')
|
||||||
|
|
Loading…
Reference in New Issue