Skip to content

Commit

Permalink
checker: give an error for free method used by fixed arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Dec 31, 2023
1 parent b87586d commit 95ece12
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1902,6 +1902,9 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
c.warn('manual memory management with `free()` is only allowed in unsafe code',
node.pos)
}
if left_sym.kind == .array_fixed {
c.error('unknown method or field: ${left_sym.name}.free()', node.pos)
}
return ast.void_type
}
// call struct field fn type
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/array_fixed_no_free_method_call_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vlib/v/checker/tests/array_fixed_no_free_method_call_err.vv:5:19: error: unknown method or field: [24]u8.free()
3 | `0`, `:`, `0`, `0`, `.`, `0`, `0`, `0`, `Z`]!
4 | defer {
5 | unsafe { buffer.free() }
| ~~~~~~
6 | }
7 | }
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/array_fixed_no_free_method_call_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
mut buffer := [u8(`0`), `0`, `0`, `0`, `-`, `0`, `0`, `-`, `0`, `0`, `T`, `0`, `0`, `:`, `0`,
`0`, `:`, `0`, `0`, `.`, `0`, `0`, `0`, `Z`]!
defer {
unsafe { buffer.free() }
}
}

0 comments on commit 95ece12

Please sign in to comment.