Skip to content

Commit 9ae8cc3

Browse files
authored
checker: fix option ptr field assign checking (fix #23879) (#23880)
1 parent 6b31c86 commit 9ae8cc3

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

vlib/v/checker/struct.v

+1-2
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,7 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
713713
if got_type.has_flag(.result) {
714714
c.check_expr_option_or_result_call(init_field.expr, init_field.typ)
715715
}
716-
if exp_type_is_option && got_type.is_ptr() && !(exp_type.is_ptr()
717-
&& exp_type_sym.kind == .struct) {
716+
if exp_type_is_option && got_type.is_ptr() && !exp_type.is_ptr() {
718717
c.error('cannot assign a pointer to option struct field', init_field.pos)
719718
}
720719
if exp_type_sym.kind == .voidptr && got_type_sym.kind == .struct
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module main
2+
3+
@[heap]
4+
interface IGameObject {
5+
mut:
6+
name string
7+
parent ?&IGameObject
8+
}
9+
10+
@[heap]
11+
struct GameObject implements IGameObject {
12+
mut:
13+
name string
14+
parent ?&IGameObject
15+
}
16+
17+
struct Ship implements IGameObject {
18+
GameObject
19+
speed f32
20+
}
21+
22+
fn test_main() {
23+
mut world := &GameObject{
24+
name: 'world'
25+
}
26+
mut ship := &Ship{
27+
name: 'ship'
28+
parent: world
29+
}
30+
assert '${ship}' == "&Ship{
31+
GameObject: GameObject{
32+
name: 'ship'
33+
parent: &Option(IGameObject(GameObject{
34+
name: 'world'
35+
parent: &Option(none)
36+
}))
37+
}
38+
speed: 0.0
39+
}"
40+
}

0 commit comments

Comments
 (0)