cgen: fix map comptime var type resolution on generic arg (fix #20037) (#20085)

This commit is contained in:
Felipe Pena 2023-12-05 06:33:11 -03:00 committed by GitHub
parent 77e65ac82b
commit d409d8d66c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 6 deletions

View file

@ -1073,9 +1073,21 @@ fn (mut g Gen) change_comptime_args(func ast.Fn, mut node_ ast.CallExpr, concret
mut ctyp := g.get_comptime_var_type(call_arg.expr) mut ctyp := g.get_comptime_var_type(call_arg.expr)
if ctyp != ast.void_type { if ctyp != ast.void_type {
arg_sym := g.table.sym(ctyp) arg_sym := g.table.sym(ctyp)
if arg_sym.kind == .array && param_typ.has_flag(.generic) param_sym := g.table.final_sym(param_typ)
&& g.table.final_sym(param_typ).kind == .array { if arg_sym.info is ast.Array && param_sym.kind == .array {
ctyp = (arg_sym.info as ast.Array).elem_type ctyp = arg_sym.info.elem_type
} else if arg_sym.info is ast.Map && param_sym.info is ast.Map {
if call_arg.expr.obj.ct_type_var == .value_var {
ctyp = arg_sym.info.value_type
if param_sym.info.value_type.nr_muls() > 0 && ctyp.nr_muls() > 0 {
ctyp = ctyp.set_nr_muls(0)
}
} else if call_arg.expr.obj.ct_type_var == .key_var {
ctyp = arg_sym.info.key_type
if param_sym.info.key_type.nr_muls() > 0 && ctyp.nr_muls() > 0 {
ctyp = ctyp.set_nr_muls(0)
}
}
} }
comptime_args[i] = ctyp comptime_args[i] = ctyp
} }
@ -1113,9 +1125,9 @@ fn (mut g Gen) change_comptime_args(func ast.Fn, mut node_ ast.CallExpr, concret
} }
} }
} else if arg_sym.kind == .any { } else if arg_sym.kind == .any {
mut cparam_type_sym := g.table.sym(g.unwrap_generic(ctyp)) cparam_type_sym := g.table.sym(g.unwrap_generic(ctyp))
if param_typ_sym.kind == .array && cparam_type_sym.kind == .array { if param_typ_sym.kind == .array && cparam_type_sym.info is ast.Array {
ctyp = (cparam_type_sym.info as ast.Array).elem_type ctyp = cparam_type_sym.info.elem_type
comptime_args[i] = ctyp comptime_args[i] = ctyp
} else { } else {
if node_.args[i].expr.is_auto_deref_var() { if node_.args[i].expr.is_auto_deref_var() {

View file

@ -0,0 +1,33 @@
fn merged[K, V](a map[K]V, b map[K]V) map[K]V {
mut o := a.clone()
for k, v in b {
$if V is $map {
o[k] = merged(o[k], v)
} $else {
o[k] = v
}
}
return o
}
fn test_main() {
a := {
'aa': {
'11': 1
}
}
b := {
'bb': {
'22': 2
}
}
c := merged(a, b)
assert c == {
'aa': {
'11': 1
}
'bb': {
'22': 2
}
}
}