Skip to content

Commit 0632822

Browse files
authored
cgen: fix codegen for sumtype cast from option variants on map_set (fix #23654) (#23669)
1 parent c025cf4 commit 0632822

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

vlib/v/gen/c/assign.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ fn (mut g Gen) expr_opt_with_cast(expr ast.Expr, expr_typ ast.Type, ret_typ ast.
119119
if ret_sym.kind == .sum_type {
120120
exp_sym := g.table.sym(expr_typ)
121121
fname := g.get_sumtype_casting_fn(expr_typ, ret_typ)
122-
g.call_cfn_for_casting_expr(fname, expr, ret_typ.is_ptr(), ret_sym.cname,
123-
expr_typ.is_ptr(), exp_sym.kind == .function, g.styp(expr_typ))
122+
g.call_cfn_for_casting_expr(fname, expr, ret_typ, ret_sym.cname, expr_typ.is_ptr(),
123+
exp_sym.kind == .function, g.styp(expr_typ))
124124
} else {
125125
g.write('*((${g.base_type(expr_typ)}*)')
126126
g.expr(expr)

vlib/v/gen/c/cgen.v

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,11 +2702,11 @@ fn (mut g Gen) write_sumtype_casting_fn(fun SumtypeCastingFn) {
27022702
g.auto_fn_definitions << sb.str()
27032703
}
27042704

2705-
fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr bool, exp_styp string,
2705+
fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp ast.Type, exp_styp string,
27062706
got_is_ptr bool, got_is_fn bool, got_styp string) {
27072707
mut rparen_n := 1
27082708
is_comptime_variant := expr is ast.Ident && g.comptime.is_comptime_variant_var(expr)
2709-
if exp_is_ptr {
2709+
if exp.is_ptr() {
27102710
g.write('HEAP(${exp_styp}, ')
27112711
rparen_n++
27122712
}
@@ -2744,7 +2744,10 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr
27442744
ast.void_type)
27452745
g.write(g.type_default(ctyp))
27462746
} else {
2747+
old_left_is_opt := g.left_is_opt
2748+
g.left_is_opt = !exp.has_flag(.option)
27472749
g.expr(expr)
2750+
g.left_is_opt = old_left_is_opt
27482751
}
27492752
g.write(')'.repeat(rparen_n))
27502753
}
@@ -2838,8 +2841,8 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
28382841
if exp_sym.info.is_generic {
28392842
fname = g.generic_fn_name(exp_sym.info.concrete_types, fname)
28402843
}
2841-
g.call_cfn_for_casting_expr(fname, expr, expected_is_ptr, exp_styp, true,
2842-
false, got_styp)
2844+
g.call_cfn_for_casting_expr(fname, expr, expected_type, exp_styp, true, false,
2845+
got_styp)
28432846
g.inside_cast_in_heap--
28442847
} else {
28452848
got_styp := g.cc_type(got_type, true)
@@ -2865,7 +2868,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
28652868
return
28662869
}
28672870
}
2868-
g.call_cfn_for_casting_expr(fname, expr, expected_is_ptr, exp_styp, got_is_ptr,
2871+
g.call_cfn_for_casting_expr(fname, expr, expected_type, exp_styp, got_is_ptr,
28692872
false, got_styp)
28702873
}
28712874
return
@@ -2930,7 +2933,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
29302933
g.write('${fname}(&${tmp_var})')
29312934
return
29322935
} else {
2933-
g.call_cfn_for_casting_expr(fname, expr, expected_is_ptr, unwrapped_exp_sym.cname,
2936+
g.call_cfn_for_casting_expr(fname, expr, expected_type, unwrapped_exp_sym.cname,
29342937
got_is_ptr, got_is_fn, got_styp)
29352938
}
29362939
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
type Any = ?int | ?string
2+
3+
fn test_main() {
4+
nothing := ?int(none)
5+
zero := ?int(0)
6+
one := ?string('one')
7+
ten := ?int(10)
8+
9+
mut m := map[string]Any{}
10+
m['nothing'] = Any(nothing)
11+
m['zero'] = Any(zero)
12+
m['one'] = Any(one)
13+
m['ten'] = Any(ten)
14+
15+
assert m.str() == "{'nothing': Any(Option(none)), 'zero': Any(Option(0)), 'one': Any(Option('one')), 'ten': Any(Option(10))}"
16+
}

0 commit comments

Comments
 (0)