@@ -119,7 +119,26 @@ fn (mut g Gen) final_gen_str(typ StrType) {
119119 }
120120 if typ.typ.has_flag (.option) {
121121 opt_typ := if typ.typ.has_flag (.option_mut_param_t) { styp.replace ('*' , '' ) } else { styp }
122- g.gen_str_for_option (typ.typ, opt_typ, str_fn_name)
122+ // Check if this is a type alias to an option type
123+ mut type_name := 'Option'
124+ mut unwrapped_typ := g.table.unaliased_type (typ.typ)
125+ if unwrapped_typ != typ.typ {
126+ // This is a type alias, check if it's an alias to an option type
127+ alias_sym := g.table.sym (typ.typ)
128+ if alias_sym.kind == .alias && alias_sym.info is ast.Alias {
129+ // Check if the parent type has the option flag
130+ if alias_sym.info.parent_type.has_flag (.option) {
131+ // This is an alias to an option type (e.g., type MyOpt = ?MyStruct)
132+ // Use the alias name instead of "Option"
133+ mut alias_name := alias_sym.name
134+ if alias_name.contains ('.' ) {
135+ alias_name = alias_name.all_after_last ('.' )
136+ }
137+ type_name = '?${alias_name} '
138+ }
139+ }
140+ }
141+ g.gen_str_for_option (typ.typ, opt_typ, str_fn_name, type_name)
123142 return
124143 }
125144 if typ.typ.has_flag (.result) {
@@ -177,7 +196,7 @@ fn (mut g Gen) final_gen_str(typ StrType) {
177196 }
178197}
179198
180- fn (mut g Gen) gen_str_for_option (typ ast.Type, styp string , str_fn_name string ) {
199+ fn (mut g Gen) gen_str_for_option (typ ast.Type, styp string , str_fn_name string , type_name string ) {
181200 $if trace_autostr ? {
182201 eprintln ('> gen_str_for_option: ${typ.debug()} | ${styp} | ${str_fn_name} ' )
183202 }
@@ -224,9 +243,9 @@ fn (mut g Gen) gen_str_for_option(typ ast.Type, styp string, str_fn_name string)
224243 g.auto_str_funcs.writeln ('\t\t res = ${parent_str_fn_name} (${deref} it.data);' )
225244 }
226245 }
227- g.auto_str_funcs.writeln ('\t\t return ${str_intp_sub('Option (%%)', 'res')} ;' )
246+ g.auto_str_funcs.writeln ('\t\t return ${str_intp_sub('${type_name} (%% )' , 'res' )};')
228247 g.auto_str_funcs.writeln(' \t}')
229- g.auto_str_funcs.writeln ('\t return _S("Option (none)");' )
248+ g.auto_str_funcs.writeln(' \treturn _S ("${type_name} (none)" );')
230249 g.auto_str_funcs.writeln(' }')
231250}
232251
0 commit comments