-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
cgen: fix option reference string interpolation #27760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -239,28 +239,50 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) { | |
| } | ||
| } | ||
|
|
||
| exp_typ := if unwrap_opt_or_res { typ.clear_option_and_result() } else { typ } | ||
| mut exp_typ := if unwrap_opt_or_res { typ.clear_option_and_result() } else { typ } | ||
| if unwrap_opt_or_res { | ||
| typ = exp_typ | ||
| } | ||
| is_dump_expr := expr is ast.DumpExpr | ||
| is_var_mut := g.expr_is_auto_deref_var(expr) && !typ.has_flag(.option) | ||
| mut option_payload_ref_tmp := false | ||
| if expr is ast.PrefixExpr { | ||
| right_expr := expr.right.remove_par() | ||
| right_type := g.table.fully_unaliased_type(expr.right_type) | ||
| if expr.op == .amp && right_type.has_flag(.option) { | ||
|
Comment on lines
+251
to
+252
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In generic instantiations where Useful? React with 👍 / 👎. |
||
| option_payload_ref_tmp = match right_expr { | ||
| ast.Ident, ast.IndexExpr, ast.SelectorExpr { true } | ||
| else { false } | ||
| } | ||
| } | ||
| } | ||
| if option_payload_ref_tmp && expr is ast.PrefixExpr { | ||
| exp_typ = | ||
| g.table.fully_unaliased_type(expr.right_type).clear_option_and_result().ref().set_flag(.option) | ||
|
Comment on lines
+260
to
+261
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| typ = exp_typ | ||
| } | ||
| str_fn_name := if mut_arg_option_type != 0 { | ||
| g.get_str_fn(mut_arg_option_type) | ||
| } else { | ||
| g.get_str_fn(exp_typ) | ||
| } | ||
| temp_var_needed := expr is ast.CallExpr | ||
| && (expr.return_type.is_ptr() || g.table.sym(expr.return_type).is_c_struct()) | ||
| temp_var_needed := (expr is ast.CallExpr && (expr.return_type.is_ptr() | ||
| || g.table.sym(expr.return_type).is_c_struct())) | ||
| || option_payload_ref_tmp | ||
| mut tmp_var := '' | ||
| if temp_var_needed { | ||
| tmp_var = g.new_tmp_var() | ||
| ret_typ := g.styp(exp_typ) | ||
| line := g.go_before_last_stmt().trim_space() | ||
| g.empty_line = true | ||
| g.write('${ret_typ} ${tmp_var} = ') | ||
| g.expr(expr) | ||
| g.writeln(';') | ||
| if option_payload_ref_tmp && expr is ast.PrefixExpr { | ||
| g.writeln('${ret_typ} ${tmp_var};') | ||
| g.gen_option_payload_ref(expr, exp_typ, tmp_var) | ||
| } else { | ||
| g.write('${ret_typ} ${tmp_var} = ') | ||
| g.expr(expr) | ||
| g.writeln(';') | ||
| } | ||
| g.write(line) | ||
| } | ||
| if is_ptr && !is_var_mut { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this option-reference lowering is added only inside the later auto-string branch, option references whose payload symbol is handled by an earlier branch never use it. For example, with
enum Color { red }andval := ?Color(.red),${&val}enters thesym.kind == .enumpath above before this block and still passes the original?Colorstorage to the?&Colorstringifier, leaving this fix incomplete for option enum payloads.Useful? React with 👍 / 👎.