From e6a67e7172cd975d58e4f30d0d4dd509dd07981a Mon Sep 17 00:00:00 2001 From: zakuro Date: Fri, 9 Apr 2021 16:54:03 +0900 Subject: [PATCH] parser: fix error of `-foo.bar()` (#9646) --- vlib/v/parser/pratt.v | 6 +----- vlib/v/tests/prefix_expr_test.v | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 vlib/v/tests/prefix_expr_test.v diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index 0a7ec07de8..268f232a61 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -507,11 +507,7 @@ fn (mut p Parser) prefix_expr() ast.PrefixExpr { // p.warn('unsafe') // } p.next() - mut right := if op == .minus { - p.expr(int(token.Precedence.call)) - } else { - p.expr(int(token.Precedence.prefix)) - } + mut right := p.expr(int(token.Precedence.prefix)) p.is_amp = false if mut right is ast.CastExpr { right.in_prexpr = true diff --git a/vlib/v/tests/prefix_expr_test.v b/vlib/v/tests/prefix_expr_test.v new file mode 100644 index 0000000000..2e3aae3d80 --- /dev/null +++ b/vlib/v/tests/prefix_expr_test.v @@ -0,0 +1,29 @@ +fn value(n int) int { + return n +} + +struct Foo { + n int +} + +fn (foo Foo) value() int { + return foo.n +} + +fn test_negative() { + one := 1 + negative_one := -1 + assert -one == negative_one + assert one == -negative_one + + assert -value(1) == -1 + + // issue #9643 + foo := Foo{1} + assert -foo.value() == -1 + assert -(foo.value()) == -1 + + arr := [1, 2, 3] + assert -arr[0] == -1 + assert -arr[1] == -2 +}