@@ -5587,28 +5587,39 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) {
55875587 } else if sym.info is ast.Alias && sym.info.parent_type.has_flag (.option) {
55885588 g.expr_with_opt (node.expr, expr_type, sym.info.parent_type)
55895589 } else {
5590- g.write ('(${cast_label }(' )
5591- if node.expr is ast.Ident {
5592- if ! node.typ.is_ptr () && node.expr_type.is_ptr () && node.expr.obj is ast.Var
5593- && node.expr.obj.smartcasts.len > 0 {
5594- g.write ('*' .repeat (node.expr_type.nr_muls ()))
5590+ g.write ('(${cast_label }' )
5591+ expr_typ := ast.mktyp (node.expr_type)
5592+ alias_to_sumtype := sym.info is ast.Alias
5593+ && g.table.sumtype_has_variant (sym.info.parent_type, expr_typ, false )
5594+ if alias_to_sumtype {
5595+ expr_styp := g.styp (expr_typ)
5596+ g.write ('{._${g .table .sym (expr_typ ).cname }=memdup(ADDR(${expr_styp }, ' )
5597+ g.expr (node.expr)
5598+ g.write ('), sizeof(${expr_styp })),._typ=${int (expr_typ )}})' )
5599+ } else {
5600+ g.write ('(' )
5601+ if node.expr is ast.Ident {
5602+ if ! node.typ.is_ptr () && node.expr_type.is_ptr () && node.expr.obj is ast.Var
5603+ && node.expr.obj.smartcasts.len > 0 {
5604+ g.write ('*' .repeat (node.expr_type.nr_muls ()))
5605+ }
55955606 }
5596- }
5597- if sym. kind == .alias && g.table. final_sym ( node.typ). kind == .string {
5598- ptr_cnt := node.typ. nr_muls () - expr_type. nr_muls ()
5599- if ptr_cnt > 0 {
5600- g. write ( '&' . repeat (ptr_cnt))
5607+ if sym. kind == .alias && g.table. final_sym (node.typ). kind == .string {
5608+ ptr_cnt := node.typ. nr_muls () - expr_type. nr_muls ()
5609+ if ptr_cnt > 0 {
5610+ g. write ( '&' . repeat ( ptr_cnt))
5611+ }
56015612 }
5602- }
5603- g. expr ( node.expr)
5604- if node.expr is ast.IntegerLiteral {
5605- if node_typ in [ast.u 64_ type, ast.u 32_ type, ast.u 16_ type] {
5606- if ! node.expr.val. starts_with ( '-' ) {
5607- g. write ( 'U' )
5613+ g. expr (node.expr)
5614+ if node.expr is ast.IntegerLiteral {
5615+ if node_typ in [ast.u 64_ type, ast.u 32_ type, ast.u 16_ type] {
5616+ if ! node.expr.val. starts_with ( '-' ) {
5617+ g. write ( 'U' )
5618+ }
56085619 }
56095620 }
5621+ g.write ('))' )
56105622 }
5611- g.write ('))' )
56125623 }
56135624 }
56145625}
0 commit comments