From 7fd9b62b343cfd74848d61c333c2ee0f6b4447c5 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 15 Jan 2023 03:38:46 +0800 Subject: [PATCH] parser: fix type alias of fn with mut argument (#16974) --- vlib/v/parser/fn.v | 6 +++--- vlib/v/tests/type_alias_of_fn_with_mut_args_test.v | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/type_alias_of_fn_with_mut_args_test.v diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index ead322928f..d7feceb18b 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -804,9 +804,9 @@ fn (mut p Parser) fn_args() ([]ast.Param, bool, bool) { types_only := p.tok.kind in [.amp, .ellipsis, .key_fn, .lsbr] || (p.peek_tok.kind == .comma && (p.table.known_type(argname) || is_generic_type)) || p.peek_tok.kind == .dot || p.peek_tok.kind == .rpar || p.fn_language == .c - || (p.tok.kind == .key_mut && (p.peek_token(2).kind == .comma - || p.peek_token(2).kind == .rpar || (p.peek_tok.kind == .name - && p.peek_token(2).kind == .dot))) + || (p.tok.kind == .key_mut && (p.peek_tok.kind in [.amp, .ellipsis, .key_fn, .lsbr] + || p.peek_token(2).kind == .comma || p.peek_token(2).kind == .rpar + || (p.peek_tok.kind == .name && p.peek_token(2).kind == .dot))) // TODO copy paste, merge 2 branches if types_only { mut arg_no := 1 diff --git a/vlib/v/tests/type_alias_of_fn_with_mut_args_test.v b/vlib/v/tests/type_alias_of_fn_with_mut_args_test.v new file mode 100644 index 0000000000..767293a4ac --- /dev/null +++ b/vlib/v/tests/type_alias_of_fn_with_mut_args_test.v @@ -0,0 +1,13 @@ +type MutCallback = fn (mut []string) + +fn mutate(mut ss []string, cb MutCallback) { + cb(mut ss) +} + +fn test_type_alias_of_fn_with_mut_args() { + mut s := ['a'] + mutate(mut s, fn (mut ss []string) { + ss << 'b' + }) + assert s == ['a', 'b'] +}