compiler: fix generation of default .str() methods in interpolation

This commit is contained in:
Delyan Angelov 2020-03-07 15:13:53 +02:00 committed by GitHub
parent 09d9dd2607
commit d2ab9d3e77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 136 additions and 14 deletions

View file

@ -114,15 +114,10 @@ fn (p mut Parser) string_expr() {
else {
f := p.typ_to_fmt(typ, 0)
if f == '' {
is_array := typ.starts_with('array_')
typ2 := p.table.find_type(typ)
has_str_method := p.table.type_has_method(typ2, 'str')
if is_array || has_str_method {
if is_array && !has_str_method {
p.gen_array_str(typ2)
}
has_str_method, styp := p.gen_default_str_method_if_missing( typ )
if has_str_method {
tmp_var := p.get_tmp()
p.cgen.insert_before('string $tmp_var = ${typ}_str(${val});')
p.cgen.insert_before('string $tmp_var = ${styp}_str(${val});')
args = args.all_before_last(val) + '${tmp_var}.len, ${tmp_var}.str'
format += '%.*s '
}
@ -137,7 +132,7 @@ fn (p mut Parser) string_expr() {
if complex_inter {
p.fgen('}')
}
// p.fgen('\'')
// println("hello %d", num) optimization.
if p.cgen.nogen {
@ -165,4 +160,3 @@ fn (p mut Parser) string_expr() {
p.gen('_STR($format$args)')
}
}