cgen: fix if mut var != none { for optional interface values (fix #24351) (#24410)

This commit is contained in:
Felipe Pena 2025-05-05 13:50:33 -03:00 committed by GitHub
parent 0c1a02f910
commit d9afebc277
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 2 deletions

View File

@ -5269,8 +5269,17 @@ fn (mut g Gen) ident(node ast.Ident) {
is_option_unwrap := is_option && typ == node.obj.typ.clear_flag(.option) is_option_unwrap := is_option && typ == node.obj.typ.clear_flag(.option)
cast_sym := g.table.sym(g.unwrap_generic(typ)) cast_sym := g.table.sym(g.unwrap_generic(typ))
if obj_sym.kind == .interface && cast_sym.kind == .interface { if obj_sym.kind == .interface && cast_sym.kind == .interface {
ptr := '*'.repeat(node.obj.typ.nr_muls()) if cast_sym.cname != obj_sym.cname {
g.write('I_${obj_sym.cname}_as_I_${cast_sym.cname}(${ptr}${node.name})') ptr := '*'.repeat(node.obj.typ.nr_muls())
g.write('I_${obj_sym.cname}_as_I_${cast_sym.cname}(${ptr}${node.name})')
} else {
ptr := if is_option {
''
} else {
'*'.repeat(node.obj.typ.nr_muls())
}
g.write('${ptr}${node.name}')
}
} else { } else {
mut is_ptr := false mut is_ptr := false
if i == 0 { if i == 0 {

View File

@ -0,0 +1,42 @@
module main
@[heap]
interface IGameObject {
mut:
name string
parent ?&IGameObject
next ?&IGameObject
child ?&IGameObject
last_child ?&IGameObject
}
@[heap]
struct GameObject implements IGameObject {
mut:
name string
parent ?&IGameObject
next ?&IGameObject
child ?&IGameObject
last_child ?&IGameObject
}
fn test_main() {
mut v1 := &GameObject{
name: 'v1'
}
v1.next = &GameObject{
name: 'v2'
}
mut next := v1.next
for {
if mut next != none {
eprintln(next.name)
assert next.name == 'v2'
next = next.next
} else {
break
}
}
assert next == none
}