Skip to content

Commit

Permalink
checker: fix option ptr field assign checking (fix #23879) (#23880)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Mar 8, 2025
1 parent 6b31c86 commit 9ae8cc3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
3 changes: 1 addition & 2 deletions vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,7 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
if got_type.has_flag(.result) {
c.check_expr_option_or_result_call(init_field.expr, init_field.typ)
}
if exp_type_is_option && got_type.is_ptr() && !(exp_type.is_ptr()
&& exp_type_sym.kind == .struct) {
if exp_type_is_option && got_type.is_ptr() && !exp_type.is_ptr() {
c.error('cannot assign a pointer to option struct field', init_field.pos)
}
if exp_type_sym.kind == .voidptr && got_type_sym.kind == .struct
Expand Down
40 changes: 40 additions & 0 deletions vlib/v/tests/options/option_ptr_field_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module main

@[heap]
interface IGameObject {
mut:
name string
parent ?&IGameObject
}

@[heap]
struct GameObject implements IGameObject {
mut:
name string
parent ?&IGameObject
}

struct Ship implements IGameObject {
GameObject
speed f32
}

fn test_main() {
mut world := &GameObject{
name: 'world'
}
mut ship := &Ship{
name: 'ship'
parent: world
}
assert '${ship}' == "&Ship{
GameObject: GameObject{
name: 'ship'
parent: &Option(IGameObject(GameObject{
name: 'world'
parent: &Option(none)
}))
}
speed: 0.0
}"
}

0 comments on commit 9ae8cc3

Please sign in to comment.