cgen: always unwrap generic type. fix `x := &T{}`

pull/5516/head
joe-conigliaro 2020-06-27 00:31:36 +10:00
parent 951f30853a
commit 2440ffd013
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
3 changed files with 8 additions and 14 deletions

View File

@ -449,15 +449,11 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type {
} }
c.expected_type = table.void_type c.expected_type = table.void_type
mut left_type := c.expr(infix_expr.left) mut left_type := c.expr(infix_expr.left)
// if false && left_type == table.t_type { // left_type = c.unwrap_genric(c.expr(infix_expr.left))
// left_type = c.cur_generic_type
// }
infix_expr.left_type = left_type infix_expr.left_type = left_type
c.expected_type = left_type c.expected_type = left_type
mut right_type := c.expr(infix_expr.right) right_type := c.expr(infix_expr.right)
if false && right_type == table.t_type { // right_type = c.unwrap_genric(c.expr(infix_expr.right))
right_type = c.cur_generic_type
}
infix_expr.right_type = right_type infix_expr.right_type = right_type
right := c.table.get_type_symbol(right_type) right := c.table.get_type_symbol(right_type)
left := c.table.get_type_symbol(left_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 c.expected_type = table.void_type
} }
[inline]
pub fn (c &Checker) unwrap_generic(typ table.Type) table.Type { pub fn (c &Checker) unwrap_generic(typ table.Type) table.Type {
if typ.idx() == table.t_type_idx { if typ.idx() == table.t_type_idx {
// return c.cur_generic_type // return c.cur_generic_type

View File

@ -310,11 +310,7 @@ pub fn (mut g Gen) write_typeof_functions() {
// V type to C type // V type to C type
fn (g &Gen) typ(t table.Type) string { fn (g &Gen) typ(t table.Type) string {
mut styp := g.base_type(t) 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)
}
if t.has_flag(.optional) { if t.has_flag(.optional) {
// Register an optional if it's not registered yet // Register an optional if it's not registered yet
return g.register_optional(t) 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*, // cc_type returns the Cleaned Concrete Type name, *without ptr*,
// i.e. it's always just Cat, not Cat_ptr: // i.e. it's always just Cat, not Cat_ptr:
fn (g &Gen) cc_type(t table.Type) string { 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('.', '__') mut styp := sym.name.replace('.', '__')
if styp.starts_with('C__') { if styp.starts_with('C__') {
styp = styp[3..] styp = styp[3..]
@ -1696,7 +1692,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
// } // }
// string + string, string == string etc // string + string, string == string etc
// g.infix_op = node.op // 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) 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 } 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] { if node.op in [.key_is, .not_is] {

View File

@ -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 { pub fn (g &Gen) unwrap_generic(typ table.Type) table.Type {
if typ.idx() == table.t_type_idx { if typ.idx() == table.t_type_idx {
// return g.cur_generic_type // return g.cur_generic_type