diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 2680811569..8bf191a5f4 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -433,8 +433,10 @@ mut: pub struct AsCast { pub: - expr Expr - typ table.Type + expr Expr + typ table.Type +mut: + expr_type table.Type } // e.g. `[unsafe_fn]` diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 216071e560..7b1270a1f6 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -567,8 +567,7 @@ fn (c mut Checker) stmt(node ast.Stmt) { sym.map_info().key_type } else { - table.int_type - } + table.int_type} } scope.override_var(ast.Var{ name: it.key_var @@ -618,6 +617,7 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type { return c.array_init(mut it) } ast.AsCast { + it.expr_type = c.expr(it.expr) return it.typ } ast.AssignExpr { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index e1b9562a52..9719874a9e 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -562,7 +562,17 @@ fn (g mut Gen) expr(node ast.Expr) { else {} } ast.AsCast { - g.write('/* as */') + styp := g.typ(it.typ) + expr_type_sym := g.table.get_type_symbol(it.expr_type) + if expr_type_sym.kind == .sum_type { + g.write('/* as */ *($styp*)') + g.expr(it.expr) + g.write('.obj') + } + else { + g.write('/* as */ ($styp)') + g.expr(it.expr) + } } ast.AssignExpr { if ast.expr_is_blank_ident(it.left) {