parser: fix error of `-foo.bar()` (#9646)

pull/9647/head
zakuro 2021-04-09 16:54:03 +09:00 committed by GitHub
parent 67ec33218e
commit e6a67e7172
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 5 deletions

View File

@ -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

View File

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