checker: fix option ptr field assign checking (fix #23879) (#23880)

This commit is contained in:
Felipe Pena 2025-03-08 15:58:03 -03:00 committed by GitHub
parent 6b31c86fea
commit 9ae8cc357f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 2 deletions

View file

@ -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

View file

@ -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
}"
}