cgen: fix match sumtype print var aggregate error (#12667)

pull/12714/head
yuyi 2021-12-04 02:00:00 +08:00 committed by GitHub
parent 4624de6cb5
commit 365b46cad3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 4 deletions

View File

@ -832,11 +832,20 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
return return
} }
} }
} else if node.left is ast.Ident && g.comptime_var_type_map.len > 0 { } else if node.left is ast.Ident {
if node.left.obj is ast.Var { if node.left.obj is ast.Var {
rec_type = node.left.obj.typ if g.comptime_var_type_map.len > 0 {
g.gen_expr_to_string(node.left, rec_type) rec_type = node.left.obj.typ
return g.gen_expr_to_string(node.left, rec_type)
return
} else if node.left.obj.smartcasts.len > 0 {
cast_sym := g.table.get_type_symbol(node.left.obj.smartcasts.last())
if cast_sym.info is ast.Aggregate {
rec_type = cast_sym.info.types[g.aggregate_type_idx]
g.gen_expr_to_string(node.left, rec_type)
return
}
}
} }
} }
g.get_str_fn(rec_type) g.get_str_fn(rec_type)
@ -1175,6 +1184,12 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
} else if expr is ast.Ident { } else if expr is ast.Ident {
if expr.obj is ast.Var { if expr.obj is ast.Var {
typ = expr.obj.typ typ = expr.obj.typ
if expr.obj.smartcasts.len > 0 {
cast_sym := g.table.get_type_symbol(expr.obj.smartcasts.last())
if cast_sym.info is ast.Aggregate {
typ = cast_sym.info.types[g.aggregate_type_idx]
}
}
} }
} }
g.gen_expr_to_string(expr, typ) g.gen_expr_to_string(expr, typ)

View File

@ -0,0 +1,13 @@
type Bug = i64 | u64
fn test_match_sumtype_var_aggregate_print_var() {
f := Bug(i64(-17))
ret := match f {
u64, i64 {
println(f)
println(f.str())
f.str()
}
}
assert ret == '-17'
}