From 2440ffd0135d2efdb8884767d6d44409d1e858ae Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Sat, 27 Jun 2020 00:31:36 +1000 Subject: [PATCH] cgen: always unwrap generic type. fix `x := &T{}` --- vlib/v/checker/checker.v | 11 ++++------- vlib/v/gen/cgen.v | 10 +++------- vlib/v/gen/fn.v | 1 + 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index fafcf32d32..7f41800367 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -449,15 +449,11 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type { } c.expected_type = table.void_type mut left_type := c.expr(infix_expr.left) - // if false && left_type == table.t_type { - // left_type = c.cur_generic_type - // } + // left_type = c.unwrap_genric(c.expr(infix_expr.left)) infix_expr.left_type = left_type c.expected_type = left_type - mut right_type := c.expr(infix_expr.right) - if false && right_type == table.t_type { - right_type = c.cur_generic_type - } + right_type := c.expr(infix_expr.right) + // right_type = c.unwrap_genric(c.expr(infix_expr.right)) infix_expr.right_type = right_type right := c.table.get_type_symbol(right_type) left := c.table.get_type_symbol(left_type) @@ -1887,6 +1883,7 @@ fn (mut c Checker) stmts(stmts []ast.Stmt) { c.expected_type = table.void_type } +[inline] pub fn (c &Checker) unwrap_generic(typ table.Type) table.Type { if typ.idx() == table.t_type_idx { // return c.cur_generic_type diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 4e9920a33a..b2ea9e1d9d 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -310,11 +310,7 @@ pub fn (mut g Gen) write_typeof_functions() { // V type to C type fn (g &Gen) typ(t table.Type) string { - mut styp := g.base_type(t) - if styp.len == 1 && t == table.t_type && g.cur_generic_type != 0 { - // T => int etc - return g.typ(g.cur_generic_type) - } + styp := g.base_type(t) if t.has_flag(.optional) { // Register an optional if it's not registered yet return g.register_optional(t) @@ -388,7 +384,7 @@ fn (mut g Gen) register_optional(t table.Type) string { // cc_type returns the Cleaned Concrete Type name, *without ptr*, // i.e. it's always just Cat, not Cat_ptr: fn (g &Gen) cc_type(t table.Type) string { - sym := g.table.get_type_symbol(t) + sym := g.table.get_type_symbol(g.unwrap_generic(t)) mut styp := sym.name.replace('.', '__') if styp.starts_with('C__') { styp = styp[3..] @@ -1696,7 +1692,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) { // } // string + string, string == string etc // g.infix_op = node.op - left_type := if node.left_type == table.t_type { g.cur_generic_type } else { node.left_type } + left_type := g.unwrap_generic(node.left_type) left_sym := g.table.get_type_symbol(left_type) unaliased_left := if left_sym.kind == .alias { (left_sym.info as table.Alias).parent_type } else { left_type } if node.op in [.key_is, .not_is] { diff --git a/vlib/v/gen/fn.v b/vlib/v/gen/fn.v index 8656411958..61aeb14432 100644 --- a/vlib/v/gen/fn.v +++ b/vlib/v/gen/fn.v @@ -337,6 +337,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { } } +[inline] pub fn (g &Gen) unwrap_generic(typ table.Type) table.Type { if typ.idx() == table.t_type_idx { // return g.cur_generic_type