Skip to content

Commit

Permalink
checker: fix missing check for taking address of literal value member (
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Jun 27, 2023
1 parent 3558e05 commit d523bb0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions vlib/v/checker/checker.v
Expand Up @@ -3906,6 +3906,9 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
c.error('unexpected `&`, expecting expression', node.right.pos)
}
} else if mut node.right is ast.SelectorExpr {
if node.right.expr.is_literal() {
c.error('cannot take the address of a literal value', node.pos.extend(node.right.pos))
}
right_sym := c.table.sym(right_type)
expr_sym := c.table.sym(node.right.expr_type)
if expr_sym.kind == .struct_ && (expr_sym.info as ast.Struct).is_minify
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/checker/tests/prefix_addr_err.out
@@ -0,0 +1,10 @@
vlib/v/checker/tests/prefix_addr_err.vv:2:2: warning: unused variable: `b`
1 | fn main() {
2 | b := &'foo'.len
| ^
3 | }
vlib/v/checker/tests/prefix_addr_err.vv:2:7: error: cannot take the address of a literal value
1 | fn main() {
2 | b := &'foo'.len
| ~~~~~~~~~~
3 | }
3 changes: 3 additions & 0 deletions vlib/v/checker/tests/prefix_addr_err.vv
@@ -0,0 +1,3 @@
fn main() {
b := &'foo'.len
}

0 comments on commit d523bb0

Please sign in to comment.