Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Jun 22, 2024
1 parent 87c4daf commit e1be3b3
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
1 change: 0 additions & 1 deletion vlib/v/checker/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
&& right.return_type_generic != 0
&& right.return_type_generic.has_flag(.generic) {
left.obj.ct_type_var = .generic_var
// eprintln('>>> ${right.name} (.generic_var)')
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ fn (mut c Checker) resolve_comptime_args(func ast.Fn, node_ ast.CallExpr, concre
param_typ := param.typ
if call_arg.expr is ast.Ident {
if call_arg.expr.obj is ast.Var {
if call_arg.expr.obj.ct_type_var !in [.generic_param, .no_comptime] {
if call_arg.expr.obj.ct_type_var !in [.generic_var, .generic_param, .no_comptime] {
mut ctyp := c.comptime.get_comptime_var_type(call_arg.expr)
if ctyp != ast.void_type {
arg_sym := c.table.sym(ctyp)
Expand Down Expand Up @@ -2162,6 +2162,9 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
node.is_noreturn = method.is_noreturn
node.is_ctor_new = method.is_ctor_new
node.return_type = method.return_type
if method.return_type.has_flag(.generic) {
// node.return_type_generic = method.return_type
}
if !method.is_pub && method.mod != c.mod {
// If a private method is called outside of the module
// its receiver type is defined in, show an error.
Expand Down
47 changes: 46 additions & 1 deletion vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,51 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
} else if left.obj.ct_type_var == .generic_var && val is ast.CallExpr {
if val.return_type_generic != 0 && val.return_type_generic.has_flag(.generic) {
if val.is_method {
if func := g.table.find_method(g.table.sym(val.left_type),
val.name)
{
if func.generic_names.len > 0 {
mut concrete_types := val.concrete_types.map(g.unwrap_generic(it))
mut rec_len := 0
if val.left_type.has_flag(.generic) {
rec_sym := g.table.final_sym(g.unwrap_generic(val.left_type))
match rec_sym.info {
ast.Struct, ast.Interface, ast.SumType {
rec_len += rec_sym.info.generic_types.len
}
else {}
}
}

mut call_ := unsafe { val }
comptime_args := g.resolve_comptime_args(func, mut
call_, concrete_types)
if concrete_types.len > 0 {
for k, v in comptime_args {
if (rec_len + k) < concrete_types.len {
if !val.concrete_types[k].has_flag(.generic) {
concrete_types[rec_len + k] = g.unwrap_generic(v)
}
}
}
}
if gen_type := g.table.resolve_generic_to_concrete(val.return_type_generic,
func.generic_names, concrete_types)
{
if !gen_type.has_flag(.generic) {
var_type = if val.or_block.kind == .absent {
gen_type
} else {
gen_type.clear_option_and_result()
}
val_type = var_type
left.obj.typ = var_type
g.comptime.type_map['${left.name}.${left.obj.pos.pos}.generic'] = var_type
// eprintln('>>2 ${func.name} > resolve ${left.name}.${left.obj.pos.pos}.generic to ${g.table.type_to_str(var_type)}')
}
}
}
}
} else {
if func := g.table.find_fn(val.name) {
if func.generic_names.len > 0 {
Expand Down Expand Up @@ -324,7 +369,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
val_type = var_type
left.obj.typ = var_type
g.comptime.type_map['${left.name}.${left.obj.pos.pos}.generic'] = var_type
eprintln('>>> ${func.name} > resolve ${left.name}.${left.obj.pos.pos}.generic to ${g.table.type_to_str(var_type)}')
// eprintln('>>> ${func.name} > resolve ${left.name}.${left.obj.pos.pos}.generic to ${g.table.type_to_str(var_type)}')
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -4780,7 +4780,7 @@ fn (mut g Gen) ident(node ast.Ident) {
if node.info is ast.IdentVar {
if node.obj is ast.Var {
if !g.is_assign_lhs
&& node.obj.ct_type_var !in [.smartcast, .generic_param, .no_comptime, .generic_var] {
&& node.obj.ct_type_var !in [.smartcast, .generic_param, .no_comptime] {
comptime_type := g.comptime.get_comptime_var_type(node)
if comptime_type.has_flag(.option) {
if (g.inside_opt_or_res || g.left_is_opt) && node.or_expr.kind == .absent {
Expand Down
14 changes: 8 additions & 6 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ fn (mut g Gen) resolve_comptime_args(func ast.Fn, mut node_ ast.CallExpr, concre
if mut call_arg.expr is ast.Ident {
if mut call_arg.expr.obj is ast.Var {
node_.args[i].typ = call_arg.expr.obj.typ
if call_arg.expr.obj.ct_type_var !in [.generic_param, .no_comptime] {
if call_arg.expr.obj.ct_type_var !in [.generic_var, .generic_param, .no_comptime] {
mut ctyp := g.comptime.get_comptime_var_type(call_arg.expr)
if ctyp != ast.void_type {
arg_sym := g.table.sym(ctyp)
Expand Down Expand Up @@ -1293,11 +1293,13 @@ fn (mut g Gen) resolve_comptime_args(func ast.Fn, mut node_ ast.CallExpr, concre
comptime_args[k] = comptime_args[k].set_nr_muls(0)
}
} else if mut call_arg.expr.right is ast.Ident {
mut ctyp := g.comptime.get_comptime_var_type(call_arg.expr.right)
if ctyp != ast.void_type {
comptime_args[k] = ctyp
if param_typ.nr_muls() > 0 && comptime_args[k].nr_muls() > 0 {
comptime_args[k] = comptime_args[k].set_nr_muls(0)
if g.comptime.get_ct_type_var(call_arg.expr.right) != .generic_var {
mut ctyp := g.comptime.get_comptime_var_type(call_arg.expr.right)
if ctyp != ast.void_type {
comptime_args[k] = ctyp
if param_typ.nr_muls() > 0 && comptime_args[k].nr_muls() > 0 {
comptime_args[k] = comptime_args[k].set_nr_muls(0)
}
}
}
}
Expand Down

0 comments on commit e1be3b3

Please sign in to comment.