From 2ab6cac59f972e9bc52e79916ea6f9d8f4d9dbba Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 22 Sep 2024 16:49:22 -0300 Subject: [PATCH] fix --- vlib/v/checker/fn.v | 2 +- vlib/v/tests/fns/method_call_chain_mut_test.v | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/fns/method_call_chain_mut_test.v diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index e73f7d0db215c0..772f9ab0817dbb 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -3366,7 +3366,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as fn (mut c Checker) check_for_mut_receiver(mut expr ast.Expr) (string, token.Pos) { to_lock, pos := c.fail_if_immutable(mut expr) - if !expr.is_lvalue() { + if !expr.is_lvalue() && expr !is ast.CallExpr { c.error('cannot pass expression as `mut`', expr.pos()) } return to_lock, pos diff --git a/vlib/v/tests/fns/method_call_chain_mut_test.v b/vlib/v/tests/fns/method_call_chain_mut_test.v new file mode 100644 index 00000000000000..8a9925aceb4ca6 --- /dev/null +++ b/vlib/v/tests/fns/method_call_chain_mut_test.v @@ -0,0 +1,54 @@ +struct App { + name string + version string +mut: + commands []Command + flags []Flag +} + +struct Command { + name string + execute fn (cmd Command) = unsafe { nil } +} + +struct Flag { + name string + abbrev string + required bool +} + +fn (mut app App) command(cmd Command) &App { + app.commands << cmd + return unsafe { app } +} + +fn (mut app App) flag(flag Flag) &App { + app.flags << flag + return unsafe { app } +} + +fn test_main() { + mut app := App{ + name: 'amazing cli' + version: '0.1.0' + commands: [] + flags: [] + } + + w := app.command(Command{ + name: 'test' + execute: fn (cmd Command) { + println('Testing') + } + }).flag(Flag{ + name: 'verbose' + abbrev: 'v' + required: true + }) + + println(app) + assert app.flags.len == 1 + assert app.commands.len == 1 + assert w.flags.len == 1 + assert w.commands.len == 1 +}