Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: fix missing check for taking address of literal value member #18570

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
b := &'foo'.len
}