@@ -118,7 +118,8 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
118
118
is_dump_expr := expr is ast.DumpExpr
119
119
is_var_mut := expr.is_auto_deref_var ()
120
120
str_fn_name := g.get_str_fn (exp_typ)
121
- temp_var_needed := expr is ast.CallExpr && expr.return_type.is_ptr ()
121
+ temp_var_needed := expr is ast.CallExpr
122
+ && (expr.return_type.is_ptr () || g.table.sym (expr.return_type).is_c_struct ())
122
123
mut tmp_var := ''
123
124
if temp_var_needed {
124
125
tmp_var = g.new_tmp_var ()
@@ -206,6 +207,21 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
206
207
str_fn_name := g.get_str_fn (typ)
207
208
g.write ('${str_fn_name} (' )
208
209
if sym.kind != .function {
210
+ unwrap_option := expr is ast.Ident && expr.or_expr.kind == .propagate_option
211
+ exp_typ := if unwrap_option { typ.clear_flag (.option) } else { typ }
212
+ temp_var_needed := expr is ast.CallExpr
213
+ && (expr.return_type.is_ptr () || g.table.sym (expr.return_type).is_c_struct ())
214
+ mut tmp_var := ''
215
+ if temp_var_needed {
216
+ tmp_var = g.new_tmp_var ()
217
+ ret_typ := g.styp (exp_typ)
218
+ line := g.go_before_last_stmt ().trim_space ()
219
+ g.empty_line = true
220
+ g.write ('${ret_typ} ${tmp_var} = ' )
221
+ g.expr (expr)
222
+ g.writeln (';' )
223
+ g.write (line)
224
+ }
209
225
if str_method_expects_ptr && ! is_ptr && ! typ.has_flag (.option) {
210
226
g.write ('&' )
211
227
} else if (! str_method_expects_ptr && is_ptr && ! is_shared) || is_var_mut {
@@ -215,7 +231,11 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
215
231
g.write (c_struct_ptr (sym, typ, str_method_expects_ptr))
216
232
}
217
233
}
218
- g.expr_with_cast (expr, typ, typ)
234
+ if temp_var_needed {
235
+ g.write (tmp_var)
236
+ } else {
237
+ g.expr_with_cast (expr, typ, typ)
238
+ }
219
239
} else if typ.has_flag (.option) {
220
240
// only Option fn receive argument
221
241
g.expr_with_cast (expr, typ, typ)
0 commit comments