Skip to content

Commit

Permalink
checker: fix: missing check when assignment statement lvalue is ParExpr(
Browse files Browse the repository at this point in the history
fix #19819)
  • Loading branch information
shove70 committed Nov 10, 2023
1 parent 11bc808 commit 17b2045
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 7 deletions.
3 changes: 3 additions & 0 deletions vlib/v/checker/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
}
}
node.left_types << left_type
for left is ast.ParExpr {
left = (left as ast.ParExpr).expr
}
match mut left {
ast.Ident {
if (is_decl || left.kind == .blank_ident) && left_type.is_ptr()
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/checker/tests/invalid_literal_assign_err.out
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ vlib/v/checker/tests/invalid_literal_assign_err.vv:11:2: error: non-name literal
| ~~~~~
12 | (3 + 3.5) = 4 + 6.4
13 | }
vlib/v/checker/tests/invalid_literal_assign_err.vv:12:2: error: non-name literal value `(3 + 3.5)` on left side of `=`
vlib/v/checker/tests/invalid_literal_assign_err.vv:12:3: error: non-name literal value `3 + 3.5` on left side of `=`
10 | true = false
11 | 3 + 5 = 3 & 4
12 | (3 + 3.5) = 4 + 6.4
| ~~~~~~~~~
| ~~~~~~~
13 | }
10 changes: 8 additions & 2 deletions vlib/v/checker/tests/prefix_expr_decl_assign_err.out
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ vlib/v/checker/tests/prefix_expr_decl_assign_err.vv:2:5: error: non-name on the
| ^
3 | (*d) := 14
4 | }
vlib/v/checker/tests/prefix_expr_decl_assign_err.vv:3:5: error: non-name `(*d)` on left side of `:=`
vlib/v/checker/tests/prefix_expr_decl_assign_err.vv:3:10: error: modifying variables via dereferencing can only be done in `unsafe` blocks
1 | fn main() {
2 | &a := 12
3 | (*d) := 14
| ~~~~
| ~~
4 | }
vlib/v/checker/tests/prefix_expr_decl_assign_err.vv:3:6: error: non-name on the left side of `:=`
1 | fn main() {
2 | &a := 12
3 | (*d) := 14
| ^
4 | }
11 changes: 11 additions & 0 deletions vlib/v/checker/tests/unsafe_deref_assign_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
vlib/v/checker/tests/unsafe_deref_assign_err.vv:4:7: error: modifying variables via dereferencing can only be done in `unsafe` blocks
2 | cref := &c
3 |
4 | *cref = 1
| ^
5 | (*cref) = 1
vlib/v/checker/tests/unsafe_deref_assign_err.vv:5:9: error: modifying variables via dereferencing can only be done in `unsafe` blocks
3 |
4 | *cref = 1
5 | (*cref) = 1
| ^
5 changes: 5 additions & 0 deletions vlib/v/checker/tests/unsafe_deref_assign_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
c := 0
cref := &c

*cref = 1
(*cref) = 1
6 changes: 4 additions & 2 deletions vlib/v/tests/assign_bitops_with_type_aliases_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const (
type SteamId = u64

fn (mut s SteamId) set_id(i u32) {
(*s) &= ~steamid_id_mask
(*s) |= ((u64(i) << steamid_id_shift) & steamid_id_mask)
unsafe {
(*s) &= ~steamid_id_mask
(*s) |= ((u64(i) << steamid_id_shift) & steamid_id_mask)
}
}

fn test_bitops_work_with_type_aliases() {
Expand Down
4 changes: 3 additions & 1 deletion vlib/v/tests/mut_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ fn test_mut() {
n := 1
mut b := (&n)
//
(*b) = 10
unsafe {
(*b) = 10
}
// mut b := mut a
// b = 10
}
Expand Down

0 comments on commit 17b2045

Please sign in to comment.