cgen: fix auto str for sumtype with alias (#10599)

pull/10600/head weekly.2021.26
yuyi 2021-06-28 19:17:00 +08:00 committed by GitHub
parent 003f60fc1a
commit 0ac0ab6b4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 6 deletions

View File

@ -221,12 +221,7 @@ fn (mut g Gen) gen_str_for_option(typ ast.Type, styp string, str_fn_name string)
} }
fn (mut g Gen) gen_str_for_alias(info ast.Alias, styp string, str_fn_name string) { fn (mut g Gen) gen_str_for_alias(info ast.Alias, styp string, str_fn_name string) {
sym := g.table.get_type_symbol(info.parent_type) parent_str_fn_name := g.gen_str_for_type(info.parent_type)
sym_has_str_method, _, _ := sym.str_method_info()
mut parent_str_fn_name := styp_to_str_fn_name(sym.name.replace('.', '__'))
if !sym_has_str_method {
parent_str_fn_name = g.gen_str_for_type(info.parent_type)
}
mut clean_type_v_type_name := util.strip_main_name(styp.replace('__', '.')) mut clean_type_v_type_name := util.strip_main_name(styp.replace('__', '.'))
g.type_definitions.writeln('static string ${str_fn_name}($styp it); // auto') g.type_definitions.writeln('static string ${str_fn_name}($styp it); // auto')
g.auto_str_funcs.writeln('static string ${str_fn_name}($styp it) { return indent_${str_fn_name}(it, 0); }') g.auto_str_funcs.writeln('static string ${str_fn_name}($styp it) { return indent_${str_fn_name}(it, 0); }')

View File

@ -0,0 +1,9 @@
type Ints = []int
type Strings = []string
type Result = Ints | Strings
fn test_string_interpolation_sumtype() {
res := Result(Ints([1, 2, 3]))
println(res)
assert '$res' == 'Result(Ints([1, 2, 3]))'
}