From 76ed8e3750406ad703ed075f6b5f50919c5126ad Mon Sep 17 00:00:00 2001 From: spaceface777 Date: Fri, 4 Dec 2020 21:27:38 +0100 Subject: [PATCH] checker: don't disallow method call chains (#7128) --- vlib/v/checker/checker.v | 2 -- vlib/v/tests/method_call_chain_test.v | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/method_call_chain_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index ed93403072..8822d85fd0 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -994,8 +994,6 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) { // No automatic lock for array slicing (yet(?)) explicit_lock_needed = true } - } else { - c.error('cannot use function call as mut', expr.pos) } } ast.ArrayInit { diff --git a/vlib/v/tests/method_call_chain_test.v b/vlib/v/tests/method_call_chain_test.v new file mode 100644 index 0000000000..90c055d6c3 --- /dev/null +++ b/vlib/v/tests/method_call_chain_test.v @@ -0,0 +1,16 @@ +struct Test { +mut: + val int +} + +// this must return a reference, or else you'll get a C error +// TODO: add a proper checker check for that case +fn new(x int) &Test { return &Test{ x } } +fn (mut t Test) inc() &Test { t.val++ return t } +fn (mut t Test) add(x int) &Test { t.val += x return t } +fn (mut t Test) div(x int) &Test { t.val /= x return t } + +fn test_method_call_chains() { + mut x := new(4).inc().inc().inc().inc().add(4).div(2).inc() + assert x.val == 7 +}