cgen: fix nested option selector unwrapping (fix #23500) (#23497)

This commit is contained in:
Felipe Pena 2025-01-17 15:14:51 -03:00 committed by GitHub
parent c98295b294
commit 496451ecbb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 3 deletions

View File

@ -3995,13 +3995,17 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
// check first if field is sum type because scope searching is expensive
scope := g.file.scope.innermost(node.pos.pos)
if field := scope.find_struct_field(node.expr.str(), node.expr_type, node.field_name) {
nested_unwrap := is_option && field.smartcasts.len > 1
is_option_unwrap = is_option && field.smartcasts.len > 0
&& field.typ.clear_flag(.option) == field.smartcasts.last()
if field.orig_type.is_ptr() {
sum_type_dot = '->'
}
if nested_unwrap && field_sym.kind == .sum_type {
g.write('*(')
}
for i, typ in field.smartcasts {
if i == 0 && is_option_unwrap {
if i == 0 && (is_option_unwrap || nested_unwrap) {
deref := if g.inside_selector {
'*'.repeat(field.smartcasts.last().nr_muls() + 1)
} else {
@ -4009,7 +4013,9 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
}
g.write('(${deref}(${g.styp(typ)}*)')
}
if i == 0 || !nested_unwrap {
g.write('(')
}
if field_sym.kind == .sum_type && !is_option {
g.write('*')
}
@ -4028,6 +4034,9 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
if cast_sym.info is ast.Aggregate {
agg_sym := g.table.sym(cast_sym.info.types[g.aggregate_type_idx])
sum_type_deref_field += '_${agg_sym.cname}'
} else {
if i == 0 && nested_unwrap {
sum_type_deref_field += 'data)'
} else {
sum_type_deref_field += '_${cast_sym.cname}'
}
@ -4036,6 +4045,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
}
}
}
}
} else if m := sym.find_method_with_generic_parent(node.field_name) {
mut has_embeds := false
if sym.info in [ast.Struct, ast.Aggregate] {

View File

@ -0,0 +1,22 @@
type Foo = string | int | f32
struct Bar {
log ?Foo
}
fn Bar.init(log ?Foo) {
mut bar := Bar{
log: log
}
if bar.log != none {
if bar.log is string {
assert bar.log == 'foobar'
return
}
}
assert false
}
fn test_main() {
Bar.init('foobar')
}