Skip to content

Commit 3af7209

Browse files
authored
checker: fix selector with prefixed & structinit (#22689)
1 parent 54a6915 commit 3af7209

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

vlib/v/checker/checker.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4498,6 +4498,9 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
44984498
} else if mut node.right is ast.SelectorExpr {
44994499
if node.right.expr.is_literal() {
45004500
c.error('cannot take the address of a literal value', node.pos.extend(node.right.pos))
4501+
} else if node.right.expr is ast.StructInit {
4502+
c.error('should not create object instance on the heap to simply access a member',
4503+
node.pos.extend(node.right.pos))
45014504
}
45024505
right_sym := c.table.sym(right_type)
45034506
expr_sym := c.table.sym(node.right.expr_type)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/selector_struct_init_amp_err.vv:6:11: error: should not create object instance on the heap to simply access a member
2+
4 |
3+
5 | fn test_main() {
4+
6 | field := &Object{}.a
5+
| ~~~~~~~~~~~
6+
7 | assert field == 123
7+
8 | }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct Object {
2+
a int = 123
3+
}
4+
5+
fn test_main() {
6+
field := &Object{}.a
7+
assert field == 123
8+
}

0 commit comments

Comments
 (0)