@@ -5268,14 +5268,12 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
52685268 } else if node.field_name in ['idx' , 'unaliased_typ' ] {
52695269 // `T.idx`, `T.unaliased_typ`, `typeof(expr).idx`, `typeof(expr).unalised_typ`
52705270 mut name_type := node.name_type
5271- mut resolved_via_generic := false
52725271 if node.expr is ast.TypeOf {
52735272 if g.cur_fn != unsafe { nil } && g.cur_concrete_types.len > 0 {
52745273 resolved := g.resolve_typeof_in_generic (node.expr)
52755274 if resolved != 0 {
52765275 name_type = g.type_resolver.typeof_field_type (resolved,
52775276 node.field_name)
5278- resolved_via_generic = true
52795277 } else {
52805278 name_type = g.type_resolver.typeof_field_type (g.type_resolver.typeof_type (node.expr.expr,
52815279 g.resolve_typeof_expr_type (node.expr.expr, name_type)),
@@ -5288,7 +5286,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
52885286 }
52895287 // For mut params (auto_deref), strip pointer so that
52905288 // typeof(mut_param).idx == typeof(val_param).idx
5291- if ! resolved_via_generic && node.expr.expr is ast.Ident {
5289+ if node.expr.expr is ast.Ident {
52925290 if node.expr.expr.obj is ast.Var {
52935291 if node.expr.expr.obj.is_auto_deref && name_type.is_ptr () {
52945292 name_type = name_type.deref ()
@@ -6948,7 +6946,8 @@ fn (mut g Gen) ident(node ast.Ident) {
69486946 } else {
69496947 comptime_type
69506948 }
6951- g.or_block (var_name, node.or_expr, or_return_type)
6949+ g.or_block (var_name, node.or_expr, g.or_type_with_auto_deref (node,
6950+ or_return_type))
69526951 g.writeln (stmt_str)
69536952 }
69546953 return
@@ -7019,7 +7018,7 @@ fn (mut g Gen) ident(node ast.Ident) {
70197018 } else {
70207019 node.info.typ
70217020 }
7022- g.or_block (var_name, node.or_expr, or_return_type)
7021+ g.or_block (var_name, node.or_expr, g. or_type_with_auto_deref (node, or_return_type) )
70237022 g.write (stmt_str)
70247023 }
70257024 if has_resolved_var {
@@ -7295,7 +7294,7 @@ fn (mut g Gen) ident(node ast.Ident) {
72957294 stmt_str := g.go_before_last_stmt ().trim_space ()
72967295 g.empty_line = true
72977296 var_opt := if is_auto_heap { '(*${name })' } else { name }
7298- g.or_block (var_opt, node.or_expr, node.obj.typ)
7297+ g.or_block (var_opt, node.or_expr, g. or_type_with_auto_deref ( node, node .obj.typ) )
72997298 g.write (stmt_str)
73007299 }
73017300}
@@ -9226,6 +9225,16 @@ fn (mut g Gen) gen_or_block_stmts(cvar_name string, cast_typ string, stmts []ast
92269225// If the user is not using the option return value. We need to pass a temp var
92279226// to access its fields (`.ok`, `.error` etc)
92289227// `os.cp(...)` => `Option bool tmp = os__cp(...); if (tmp.state != 0) { ... }`
9228+ // For auto-deref option variables (e.g. `mut t ?&T`), ensure the
9229+ // `option_mut_param_t` flag is set so that `or_block` uses `->` instead of `.`.
9230+ fn (g &Gen) or_type_with_auto_deref (node ast.Ident, typ ast.Type) ast.Type {
9231+ if node.obj is ast.Var && node.obj.is_auto_deref && typ.has_flag (.option)
9232+ && ! typ.has_flag (.option_mut_param_t) {
9233+ return typ.set_flag (.option_mut_param_t)
9234+ }
9235+ return typ
9236+ }
9237+
92299238// Returns the type of the last stmt
92309239fn (mut g Gen) or_block (var_name string , or_block ast.OrExpr, return_type ast.Type) {
92319240 cvar_name := c_name (var_name)
0 commit comments