From 41dca3ef580a7849724564cd62019bcf3e77cf14 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Thu, 4 Jun 2020 19:32:31 +1000 Subject: [PATCH] checker/cgen: fix mutable generic fn args --- vlib/v/checker/checker.v | 2 +- vlib/v/gen/fn.v | 7 ++++--- vlib/v/parser/fn.v | 12 ++++++++---- vlib/v/tests/generics_test.v | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 3500747b8f..b7a64f4086 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1749,7 +1749,7 @@ fn (mut c Checker) stmts(stmts []ast.Stmt) { } pub fn (mut c Checker) unwrap_generic(typ table.Type) table.Type { - if typ == table.t_type { + if typ.idx() == table.t_type_idx { return c.cur_generic_type } return typ diff --git a/vlib/v/gen/fn.v b/vlib/v/gen/fn.v index 0c89ff56b0..5e894c475b 100644 --- a/vlib/v/gen/fn.v +++ b/vlib/v/gen/fn.v @@ -315,8 +315,9 @@ fn (mut g Gen) fn_args(args []table.Arg, is_variadic bool) ([]string, []string) no_names := args.len > 0 && args[0].name == 'arg_1' for i, arg in args { caname := c_name(arg.name) - arg_type_sym := g.table.get_type_symbol(arg.typ) - mut arg_type_name := g.typ(arg.typ) // arg_type_sym.name.replace('.', '__') + typ := g.unwrap_generic(arg.typ).set_nr_muls(arg.typ.nr_muls()) + arg_type_sym := g.table.get_type_symbol(typ) + mut arg_type_name := g.typ(typ) // arg_type_sym.name.replace('.', '__') // if arg.name == 'xxx' { // println('! ' + arg_type_name) // } @@ -400,7 +401,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { } pub fn (mut g Gen) unwrap_generic(typ table.Type) table.Type { - if typ == table.t_type { + if typ.idx() == table.t_type_idx { return g.cur_generic_type } return typ diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index ccd0b3eacc..23e38e6d09 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -371,8 +371,10 @@ fn (mut p Parser) fn_args() ([]table.Arg, bool) { } pos := p.tok.position() mut arg_type := p.parse_type() - if is_mut && arg_type != table.t_type { - p.check_fn_mutable_arguments(arg_type, pos) + if is_mut { + if arg_type != table.t_type { + p.check_fn_mutable_arguments(arg_type, pos) + } // if arg_type.is_ptr() { // p.error('cannot mut') // } @@ -421,8 +423,10 @@ fn (mut p Parser) fn_args() ([]table.Arg, bool) { } pos := p.tok.position() mut typ := p.parse_type() - if is_mut && typ != table.t_type { - p.check_fn_mutable_arguments(typ, pos) + if is_mut { + if typ != table.t_type { + p.check_fn_mutable_arguments(typ, pos) + } typ = typ.set_nr_muls(1) } if is_variadic { diff --git a/vlib/v/tests/generics_test.v b/vlib/v/tests/generics_test.v index a0fb8a162d..982ba32a51 100644 --- a/vlib/v/tests/generics_test.v +++ b/vlib/v/tests/generics_test.v @@ -72,7 +72,7 @@ fn test_create() { create() create() u := User{} - //gen_arg(mut u) + gen_arg(mut u) } /*