@@ -4022,23 +4022,30 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
4022
4022
mut sum_type_deref_field := ''
4023
4023
mut sum_type_dot := '.'
4024
4024
mut field_typ := ast.void_type
4025
+ mut is_option_unwrap := false
4025
4026
if f := g.table.find_field_with_embeds (sym, node.field_name) {
4026
4027
field_sym := g.table.sym (f.typ)
4027
4028
field_typ = f.typ
4028
4029
if sym.kind in [.interface , .sum_type] {
4029
4030
g.write ('(*(' )
4030
4031
}
4031
- if field_sym.kind in [.sum_type, .interface ] {
4032
+ is_option := field_typ.has_flag (.option)
4033
+ if field_sym.kind in [.sum_type, .interface ] || is_option {
4032
4034
if ! prevent_sum_type_unwrapping_once {
4033
4035
// check first if field is sum type because scope searching is expensive
4034
4036
scope := g.file.scope.innermost (node.pos.pos)
4035
4037
if field := scope.find_struct_field (node.expr.str (), node.expr_type, node.field_name) {
4038
+ is_option_unwrap = is_option && field.smartcasts.len > 0
4039
+ && field.typ.clear_flag (.option) == field.smartcasts.last ()
4036
4040
if field.orig_type.is_ptr () {
4037
4041
sum_type_dot = '->'
4038
4042
}
4039
4043
for i, typ in field.smartcasts {
4044
+ if i == 0 && is_option_unwrap {
4045
+ g.write ('(*(${g.styp(typ)} *)' )
4046
+ }
4040
4047
g.write ('(' )
4041
- if field_sym.kind == .sum_type {
4048
+ if field_sym.kind == .sum_type && ! is_option {
4042
4049
g.write ('*' )
4043
4050
}
4044
4051
cast_sym := g.table.sym (g.unwrap_generic (typ))
@@ -4047,7 +4054,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
4047
4054
dot := if node.expr_type.is_ptr () { '->' } else { '.' }
4048
4055
g.write ('I_${field_sym.cname} _as_I_${cast_sym.cname} (${ptr}${node.expr}${dot}${node.field_name} ))' )
4049
4056
return
4050
- } else {
4057
+ } else if ! is_option_unwrap {
4051
4058
if i != 0 {
4052
4059
dot := if field.typ.is_ptr () { '->' } else { '.' }
4053
4060
sum_type_deref_field + = ')${dot} '
@@ -4194,6 +4201,9 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
4194
4201
verror ('cgen: SelectorExpr | expr_type: 0 | it.expr: `${node.expr} ` | field: `${node.field_name} ` | file: ${g.file.path} | line: ${node.pos.line_nr} ' )
4195
4202
}
4196
4203
g.write (field_name)
4204
+ if is_option_unwrap {
4205
+ g.write ('.data))' )
4206
+ }
4197
4207
if sum_type_deref_field != '' {
4198
4208
g.write ('${sum_type_dot}${sum_type_deref_field} )' )
4199
4209
}
0 commit comments