From d851ecffb7e74b58fff9821708eb4987d926a83e Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 6 Jul 2023 02:30:26 +0300 Subject: [PATCH] parser: fix anonymous fns parameter checks, behaving differently than named fns (fix #18779) (#18785) --- vlib/v/parser/fn.v | 1 + vlib/v/tests/anon_fn_test.v | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 18c6c995fc..a4aa42b306 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -764,6 +764,7 @@ fn (mut p Parser) anon_fn() ast.AnonFn { name: arg.name typ: arg.typ is_mut: arg.is_mut + is_auto_deref: arg.is_mut || arg.is_auto_rec pos: arg.pos is_used: true is_arg: true diff --git a/vlib/v/tests/anon_fn_test.v b/vlib/v/tests/anon_fn_test.v index 20c22138d7..ba5b4790c6 100644 --- a/vlib/v/tests/anon_fn_test.v +++ b/vlib/v/tests/anon_fn_test.v @@ -21,3 +21,23 @@ fn test_anon_assign_struct() { } assert w.fn_() } + +// + +fn fnormal(mut acc []string, e int) []string { + acc << e.str() + return acc +} + +fn test_anon_fn_returning_a_mut_parameter_should_act_the_same_as_normal_fn_returning_a_mut_parameter() { + fanon := fn (mut acc []string, e int) []string { + acc << e.str() + return acc + } + assert '${fanon}' == '${fnormal}' + mut a := ['a', 'b', 'c'] + mut b := a.clone() + x := fanon(mut a, 123) + y := fnormal(mut b, 123) + assert a == b +}