Skip to content

Commit d409d8d

Browse files
authored
cgen: fix map comptime var type resolution on generic arg (fix #20037) (#20085)
1 parent 77e65ac commit d409d8d

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

vlib/v/gen/c/fn.v

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,9 +1073,21 @@ fn (mut g Gen) change_comptime_args(func ast.Fn, mut node_ ast.CallExpr, concret
10731073
mut ctyp := g.get_comptime_var_type(call_arg.expr)
10741074
if ctyp != ast.void_type {
10751075
arg_sym := g.table.sym(ctyp)
1076-
if arg_sym.kind == .array && param_typ.has_flag(.generic)
1077-
&& g.table.final_sym(param_typ).kind == .array {
1078-
ctyp = (arg_sym.info as ast.Array).elem_type
1076+
param_sym := g.table.final_sym(param_typ)
1077+
if arg_sym.info is ast.Array && param_sym.kind == .array {
1078+
ctyp = arg_sym.info.elem_type
1079+
} else if arg_sym.info is ast.Map && param_sym.info is ast.Map {
1080+
if call_arg.expr.obj.ct_type_var == .value_var {
1081+
ctyp = arg_sym.info.value_type
1082+
if param_sym.info.value_type.nr_muls() > 0 && ctyp.nr_muls() > 0 {
1083+
ctyp = ctyp.set_nr_muls(0)
1084+
}
1085+
} else if call_arg.expr.obj.ct_type_var == .key_var {
1086+
ctyp = arg_sym.info.key_type
1087+
if param_sym.info.key_type.nr_muls() > 0 && ctyp.nr_muls() > 0 {
1088+
ctyp = ctyp.set_nr_muls(0)
1089+
}
1090+
}
10791091
}
10801092
comptime_args[i] = ctyp
10811093
}
@@ -1113,9 +1125,9 @@ fn (mut g Gen) change_comptime_args(func ast.Fn, mut node_ ast.CallExpr, concret
11131125
}
11141126
}
11151127
} else if arg_sym.kind == .any {
1116-
mut cparam_type_sym := g.table.sym(g.unwrap_generic(ctyp))
1117-
if param_typ_sym.kind == .array && cparam_type_sym.kind == .array {
1118-
ctyp = (cparam_type_sym.info as ast.Array).elem_type
1128+
cparam_type_sym := g.table.sym(g.unwrap_generic(ctyp))
1129+
if param_typ_sym.kind == .array && cparam_type_sym.info is ast.Array {
1130+
ctyp = cparam_type_sym.info.elem_type
11191131
comptime_args[i] = ctyp
11201132
} else {
11211133
if node_.args[i].expr.is_auto_deref_var() {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
fn merged[K, V](a map[K]V, b map[K]V) map[K]V {
2+
mut o := a.clone()
3+
for k, v in b {
4+
$if V is $map {
5+
o[k] = merged(o[k], v)
6+
} $else {
7+
o[k] = v
8+
}
9+
}
10+
return o
11+
}
12+
13+
fn test_main() {
14+
a := {
15+
'aa': {
16+
'11': 1
17+
}
18+
}
19+
b := {
20+
'bb': {
21+
'22': 2
22+
}
23+
}
24+
c := merged(a, b)
25+
assert c == {
26+
'aa': {
27+
'11': 1
28+
}
29+
'bb': {
30+
'22': 2
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)