1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser, checker: fix postfix expr check (fix #14852) (#14857)

This commit is contained in:
yuyi 2022-06-27 09:28:24 +08:00 committed by GitHub
parent 5a79a54fe4
commit ef7f9779a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 22 deletions

View File

@ -628,6 +628,9 @@ fn (mut c Checker) fail_if_immutable(expr_ ast.Expr) (string, token.Pos) {
ast.PrefixExpr {
to_lock, pos = c.fail_if_immutable(expr.right)
}
ast.PostfixExpr {
to_lock, pos = c.fail_if_immutable(expr.expr)
}
ast.SelectorExpr {
if expr.expr_type == 0 {
return '', pos

View File

@ -457,7 +457,7 @@ pub fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_iden
if p.peek_tok.kind in [.rpar, .rsbr] {
if !p.inside_ct_if_expr {
p.warn_with_pos('`$p.tok.kind` operator can only be used as a statement',
p.peek_tok.pos())
p.tok.pos())
}
}
if p.tok.kind in [.inc, .dec] && p.prev_tok.line_nr != p.tok.line_nr {

View File

@ -1,21 +0,0 @@
vlib/v/parser/tests/postfix_err.vv:5:10: warning: `++` operator can only be used as a statement
3 | fn test_postfix() {
4 | mut x := 1
5 | _ = (x++)
| ^
6 | x--, x-- // OK
7 | f(x++)
vlib/v/parser/tests/postfix_err.vv:7:7: warning: `++` operator can only be used as a statement
5 | _ = (x++)
6 | x--, x-- // OK
7 | f(x++)
| ^
8 | a := [x]
9 | _ = a[x--]
vlib/v/parser/tests/postfix_err.vv:9:11: warning: `--` operator can only be used as a statement
7 | f(x++)
8 | a := [x]
9 | _ = a[x--]
| ^
10 | }
11 |

View File

@ -0,0 +1,21 @@
vlib/v/parser/tests/postfix_err_a.vv:5:8: warning: `++` operator can only be used as a statement
3 | fn test_postfix() {
4 | mut x := 1
5 | _ = (x++)
| ~~
6 | x--, x-- // OK
7 | f(x++)
vlib/v/parser/tests/postfix_err_a.vv:7:5: warning: `++` operator can only be used as a statement
5 | _ = (x++)
6 | x--, x-- // OK
7 | f(x++)
| ~~
8 | a := [x]
9 | _ = a[x--]
vlib/v/parser/tests/postfix_err_a.vv:9:9: warning: `--` operator can only be used as a statement
7 | f(x++)
8 | a := [x]
9 | _ = a[x--]
| ~~
10 | }
11 |

View File

@ -0,0 +1,14 @@
vlib/v/parser/tests/postfix_err_b.vv:7:14: warning: `++` operator can only be used as a statement
5 | for _ in 0..3 {
6 | unsafe {
7 | *(arrayptr++) = 0
| ~~
8 | }
9 | }
vlib/v/parser/tests/postfix_err_b.vv:3:18: warning: cannot cast a fixed array (use e.g. `&arr[0]` instead)
1 | fn main(){
2 | mut array := [3]int{}
3 | mut arrayptr := &int(array)
| ~~~~~~~~~~~
4 |
5 | for _ in 0..3 {

View File

@ -0,0 +1,10 @@
fn main(){
mut array := [3]int{}
mut arrayptr := &int(array)
for _ in 0..3 {
unsafe {
*(arrayptr++) = 0
}
}
}