Skip to content

Commit

Permalink
all: @[freed] attribute for assign statements
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Jun 3, 2024
1 parent 6d2c3a9 commit 193a99d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,7 @@ pub mut:
is_volatile bool // for disabling variable access optimisations (needed for hardware drivers)
is_simple bool // `x+=2` in `for x:=1; ; x+=2`
has_cross_var bool
attr Attr
}

// `expr as Ident`
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/checker/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import v.ast
fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
prev_inside_assign := c.inside_assign
c.inside_assign = true
if node.attr.name != '' {
c.assign_stmt_attr = node.attr.name
} else {
c.assign_stmt_attr = ''
}
c.expected_type = ast.none_type // TODO: a hack to make `x := if ... work`
defer {
c.expected_type = ast.void_type
Expand Down
1 change: 1 addition & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ mut:
orm_table_fields map[string][]ast.StructField // known table structs
//
v_current_commit_hash string // same as old C.V_CURRENT_COMMIT_HASH
assign_stmt_attr string // for `x := [1,2,3] @[freed]`
}

pub fn new_checker(table &ast.Table, pref_ &pref.Preferences) &Checker {
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/checker/errors.v
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ fn (mut c Checker) warn(s string, pos token.Pos) {
}

fn (mut c Checker) warn_alloc(s string, pos token.Pos) {
if c.assign_stmt_attr == 'freed' {
return
}

if !c.is_builtin_mod && c.mod !in ['strings', 'math', 'math.bits', 'builtin', 'strconv'] {
c.warn('allocation (${s})', pos)
}
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,9 @@ pub fn (mut f Fmt) assign_stmt(node ast.AssignStmt) {
f.write(', ')
}
}
if node.attr.name != '' {
f.write(' @[${node.attr.name}]')
}
f.comments(node.end_comments, has_nl: false, same_line: true, level: .keep)
if !f.single_line_if {
f.writeln('')
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/fmt/tests/stmt_attr_keep.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn foo() {
x := [1, 2, 3] @[freed]
unsafe {
x.free()
}
println('x is freed')
}
9 changes: 9 additions & 0 deletions vlib/v/parser/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr) ast.Stmt {
}
}
}
mut attr := ast.Attr{}
// This assign stmt has an attribute e.g. `x := [1,2,3] @[freed]`
if p.tok.kind == .at && p.tok.line_nr == p.prev_tok.line_nr {
p.check(.at)
p.check(.lsbr)
attr = p.parse_attr(true)
p.check(.rsbr)
}
pos.update_last_line(p.prev_tok.line_nr)
p.expr_mod = ''
return ast.AssignStmt{
Expand All @@ -297,5 +305,6 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr) ast.Stmt {
is_simple: p.inside_for && p.tok.kind == .lcbr
is_static: is_static
is_volatile: is_volatile
attr: attr
}
}

0 comments on commit 193a99d

Please sign in to comment.