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 +}