From f4151302bd3ac9f248fc7c27f4ceebc854acc0f9 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 29 Dec 2024 10:18:56 -0300 Subject: [PATCH 1/6] cleanup --- vlib/v/checker/assign.v | 4 ++-- vlib/v/checker/comptime.v | 14 +++++++------- vlib/v/checker/for.v | 10 +++++----- vlib/v/checker/if.v | 4 ++-- vlib/v/gen/c/assign.v | 5 ++--- vlib/v/gen/c/comptime.v | 20 ++++++++++---------- vlib/v/gen/c/for.v | 4 ++-- vlib/v/type_resolver/type_resolver.v | 19 +++++++++++++++++++ 8 files changed, 49 insertions(+), 31 deletions(-) diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index 3c4854e2a67d92..2c3c66b16f076e 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -420,7 +420,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { } else { fn_ret_type.clear_option_and_result() } - c.type_resolver.type_map['g.${left.name}.${left.obj.pos.pos}'] = var_type + c.type_resolver.update_generic(left, var_type) } } else if right.is_static_method && right.left_type.has_flag(.generic) { @@ -430,7 +430,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { } else { fn_ret_type.clear_option_and_result() } - c.type_resolver.type_map['g.${left.name}.${left.obj.pos.pos}'] = var_type + c.type_resolver.update_generic(left, var_type) } } } diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index 93bcfd90eb42c4..0f17b7947ad3d7 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -300,8 +300,8 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) { } c.comptime.comptime_for_field_value = field c.comptime.comptime_for_field_var = node.val_var - c.type_resolver.type_map[node.val_var] = c.field_data_type - c.type_resolver.type_map['${node.val_var}.typ'] = node.typ + c.type_resolver.update_ct_type(node.val_var, c.field_data_type) + c.type_resolver.update_ct_type('${node.val_var}.typ', node.typ) c.comptime.comptime_for_field_type = field.typ c.stmts(mut node.stmts) @@ -345,8 +345,8 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) { c.enum_data_type = c.table.find_type('EnumData') } c.comptime.comptime_for_enum_var = node.val_var - c.type_resolver.type_map[node.val_var] = c.enum_data_type - c.type_resolver.type_map['${node.val_var}.typ'] = node.typ + c.type_resolver.update_ct_type(node.val_var, c.enum_data_type) + c.type_resolver.update_ct_type('${node.val_var}.typ', node.typ) c.stmts(mut node.stmts) c.pop_comptime_info() c.table.used_features.comptime_for = true @@ -363,7 +363,7 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) { c.comptime.comptime_for_method = unsafe { &method } c.comptime.comptime_for_method_var = node.val_var c.comptime.comptime_for_method_ret_type = method.return_type - c.type_resolver.type_map['${node.val_var}.return_type'] = method.return_type + c.type_resolver.update_ct_type('${node.val_var}.return_type', method.return_type) c.stmts(mut node.stmts) c.pop_comptime_info() } @@ -398,8 +398,8 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) { c.push_new_comptime_info() c.comptime.inside_comptime_for = true c.comptime.comptime_for_variant_var = node.val_var - c.type_resolver.type_map[node.val_var] = c.variant_data_type - c.type_resolver.type_map['${node.val_var}.typ'] = variant + c.type_resolver.update_ct_type(node.val_var, c.variant_data_type) + c.type_resolver.update_ct_type('${node.val_var}.typ', variant) c.stmts(mut node.stmts) c.pop_comptime_info() } diff --git a/vlib/v/checker/for.v b/vlib/v/checker/for.v index e98f3d84a5a0c1..6e1dc735636a61 100644 --- a/vlib/v/checker/for.v +++ b/vlib/v/checker/for.v @@ -159,7 +159,7 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) { node.scope.update_var_type(node.val_var, val_type) if is_comptime { - c.type_resolver.type_map[node.val_var] = val_type + c.type_resolver.update_ct_type(node.val_var, val_type) node.scope.update_ct_var_kind(node.val_var, .value_var) defer { @@ -182,7 +182,7 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) { node.scope.update_var_type(node.key_var, key_type) if is_comptime { - c.type_resolver.type_map[node.key_var] = key_type + c.type_resolver.update_ct_type(node.key_var, key_type) node.scope.update_ct_var_kind(node.key_var, .key_var) defer { @@ -195,7 +195,7 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) { node.scope.update_var_type(node.val_var, value_type) if is_comptime { - c.type_resolver.type_map[node.val_var] = value_type + c.type_resolver.update_ct_type(node.val_var, value_type) node.scope.update_ct_var_kind(node.val_var, .value_var) defer { @@ -220,7 +220,7 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) { node.scope.update_var_type(node.key_var, key_type) if is_comptime { - c.type_resolver.type_map[node.key_var] = key_type + c.type_resolver.update_ct_type(node.key_var, key_type) node.scope.update_ct_var_kind(node.key_var, .key_var) defer { @@ -279,7 +279,7 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) { node.val_type = value_type node.scope.update_var_type(node.val_var, value_type) if is_comptime { - c.type_resolver.type_map[node.val_var] = value_type + c.type_resolver.update_ct_type(node.val_var, value_type) node.scope.update_ct_var_kind(node.val_var, .value_var) defer { diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index 2387cfc37ca1cf..381cd1e8d20d9d 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -387,9 +387,9 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { } if comptime_field_name.len > 0 { if comptime_field_name == c.comptime.comptime_for_method_var { - c.type_resolver.type_map[comptime_field_name] = c.comptime.comptime_for_method_ret_type + c.type_resolver.update_ct_type(comptime_field_name, c.comptime.comptime_for_method_ret_type) } else if comptime_field_name == c.comptime.comptime_for_field_var { - c.type_resolver.type_map[comptime_field_name] = c.comptime.comptime_for_field_type + c.type_resolver.update_ct_type(comptime_field_name, c.comptime.comptime_for_field_type) } } c.skip_flags = cur_skip_flags diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index 15cc6511e1dcc1..6c43fce333c7d2 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -353,15 +353,14 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { var_type = fn_ret_type val_type = var_type left.obj.typ = var_type - g.type_resolver.type_map['g.${left.name}.${left.obj.pos.pos}'] = var_type - // eprintln('>> ${func.name} > resolve ${left.name}.${left.obj.pos.pos}.generic to ${g.table.type_to_str(var_type)}') + g.type_resolver.update_generic(left, var_type) } } else if val.is_static_method && val.left_type.has_flag(.generic) { fn_ret_type := g.resolve_return_type(val) var_type = fn_ret_type val_type = var_type left.obj.typ = var_type - g.type_resolver.type_map['g.${left.name}.${left.obj.pos.pos}'] = var_type + g.type_resolver.update_generic(left, var_type) g.assign_ct_type = var_type } } diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 12b84dee799154..2610a08dbe9b42 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -873,7 +873,7 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { if j < len - 1 { g.write(', ') } - g.type_resolver.type_map['${node.val_var}.args[${j}].typ'] = typ + g.type_resolver.update_ct_type('${node.val_var}.args[${j}].typ', typ) } g.writeln('}));\n') } @@ -892,15 +892,15 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { if ret_type != 'void' { sig += ' ${ret_type}' } - styp := g.table.find_type(sig) + typ := g.table.find_type(sig) // TODO: type aliases ret_typ := method.return_type - g.writeln('\t${node.val_var}.typ = ${int(styp)};') + g.writeln('\t${node.val_var}.typ = ${int(typ)};') g.writeln('\t${node.val_var}.return_type = ${int(ret_typ.idx())};') - g.type_resolver.type_map['${node.val_var}.return_type'] = ret_typ - g.type_resolver.type_map['${node.val_var}.typ'] = styp + g.type_resolver.update_ct_type('${node.val_var}.return_type', ret_typ) + g.type_resolver.update_ct_type('${node.val_var}.typ', typ) g.stmts(node.stmts) i++ g.writeln('}') @@ -962,8 +962,8 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { g.writeln('\t${node.val_var}.indirections = ${field.typ.nr_muls()};') - g.type_resolver.type_map['${node.val_var}.typ'] = field.typ - g.type_resolver.type_map['${node.val_var}.unaliased_typ'] = unaliased_styp + g.type_resolver.update_ct_type('${node.val_var}.typ', field.typ) + g.type_resolver.update_ct_type('${node.val_var}.unaliased_typ', unaliased_styp) g.stmts(node.stmts) i++ g.writeln('}') @@ -979,7 +979,7 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { g.push_new_comptime_info() for val in sym.info.vals { g.comptime.comptime_for_enum_var = node.val_var - g.type_resolver.type_map['${node.val_var}.typ'] = node.typ + g.type_resolver.update_ct_type('${node.val_var}.typ', node.typ) g.writeln('/* enum vals ${i} */ {') g.writeln('\t${node.val_var}.name = _SLIT("${val}");') @@ -1030,7 +1030,7 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { g.push_new_comptime_info() for variant in sym.info.variants { g.comptime.comptime_for_variant_var = node.val_var - g.type_resolver.type_map['${node.val_var}.typ'] = variant + g.type_resolver.update_ct_type('${node.val_var}.typ', variant) g.writeln('/* variant ${i} */ {') g.writeln('\t${node.val_var}.typ = ${int(variant)};') @@ -1050,7 +1050,7 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { g.comptime.inside_comptime_for = true g.comptime.comptime_for_method_param_var = node.val_var for param in method.params[1..] { - g.type_resolver.type_map['${node.val_var}.typ'] = param.typ + g.type_resolver.update_ct_type('${node.val_var}.typ', param.typ) g.writeln('/* method param ${i} */ {') g.writeln('\t${node.val_var}.typ = ${int(param.typ)};') diff --git a/vlib/v/gen/c/for.v b/vlib/v/gen/c/for.v index e1be2ab6ae0797..6b5245811a0549 100644 --- a/vlib/v/gen/c/for.v +++ b/vlib/v/gen/c/for.v @@ -158,7 +158,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) { node.kind = unwrapped_sym.kind if is_comptime { - g.type_resolver.type_map[node.val_var] = node.val_type + g.type_resolver.update_ct_type(node.val_var, node.val_type) node.scope.update_ct_var_kind(node.val_var, .value_var) defer { @@ -175,7 +175,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) { node.scope.update_var_type(node.key_var, key_type) if is_comptime { - g.type_resolver.type_map[node.key_var] = node.key_type + g.type_resolver.update_ct_type(node.key_var, node.key_type) node.scope.update_ct_var_kind(node.key_var, .key_var) defer { diff --git a/vlib/v/type_resolver/type_resolver.v b/vlib/v/type_resolver/type_resolver.v index c07ac0ad81f5df..943396741bbdd9 100644 --- a/vlib/v/type_resolver/type_resolver.v +++ b/vlib/v/type_resolver/type_resolver.v @@ -67,6 +67,25 @@ pub fn TypeResolver.new(table &ast.Table, resolver &IResolverType) &TypeResolver } } +// update_generic updates generic type for specific variables (for overwriting its last known checker type) +// `var` are usually variables as `var := fn_returning_generic()` +@[inline] +pub fn (mut t TypeResolver) update_generic(var ast.Ident, var_type ast.Type) { + if var.obj is ast.Var { + t.type_map['g.${var.name}.${var.obj.pos.pos}'] = var_type + } +} + +// update_ct_type updates current type for specific key (comptime vars) +// `var` iteration vars, comptime vars +// `var.typ` => for comptime $for variables +// `var.unaliased_typ` => for comptime $for variables +// `var.return_type` => for .method return type +@[inline] +pub fn (mut t TypeResolver) update_ct_type(key string, var_type ast.Type) { + t.type_map[key] = var_type +} + @[noreturn] fn (t &TypeResolver) error(s string, pos token.Pos) { util.show_compiler_message('cgen error:', pos: pos, file_path: t.resolver.file.path, message: s) From ae094438f644f4e8d624ec5110b38606b19808fe Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 29 Dec 2024 10:48:49 -0300 Subject: [PATCH 2/6] cleanup --- vlib/v/checker/assign.v | 4 ++-- vlib/v/checker/checker.v | 9 ++++++--- vlib/v/checker/comptime.v | 2 +- vlib/v/checker/if.v | 7 ++++--- vlib/v/checker/infix.v | 3 ++- vlib/v/gen/c/assign.v | 20 +++++++++++--------- vlib/v/gen/c/cgen.v | 10 ++++++---- vlib/v/gen/c/comptime.v | 5 +++-- vlib/v/gen/c/fn.v | 4 ++-- vlib/v/gen/c/infix.v | 5 ++--- vlib/v/type_resolver/comptime_resolver.v | 4 ++-- vlib/v/type_resolver/type_resolver.v | 17 ++++++++++++----- 12 files changed, 53 insertions(+), 37 deletions(-) diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index 2c3c66b16f076e..3f8d115c4647c5 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -420,7 +420,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { } else { fn_ret_type.clear_option_and_result() } - c.type_resolver.update_generic(left, var_type) + // c.type_resolver.update_generic(left, var_type) } } else if right.is_static_method && right.left_type.has_flag(.generic) { @@ -430,7 +430,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { } else { fn_ret_type.clear_option_and_result() } - c.type_resolver.update_generic(left, var_type) + // c.type_resolver.update_generic(left, var_type) } } } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 4c05126c2e1252..d64381972cd1e5 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1672,7 +1672,8 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type { } else if c.comptime.inside_comptime_for && typ == c.enum_data_type && node.field_name == 'value' { // for comp-time enum.values - node.expr_type = c.type_resolver.type_map['${c.comptime.comptime_for_enum_var}.typ'] + node.expr_type = c.type_resolver.get_ct_type_or_default('${c.comptime.comptime_for_enum_var}.typ', + node.expr_type) node.typ = typ return node.expr_type } @@ -3056,7 +3057,8 @@ pub fn (mut c Checker) expr(mut node ast.Expr) ast.Type { if node.expr.ct_expr { node.expr_type = c.type_resolver.get_type(node.expr as ast.Ident) } else if (node.expr as ast.Ident).name in c.type_resolver.type_map { - node.expr_type = c.type_resolver.type_map[(node.expr as ast.Ident).name] + node.expr_type = c.type_resolver.get_ct_type_or_default((node.expr as ast.Ident).name, + node.expr_type) } } c.check_expr_option_or_result_call(node.expr, node.expr_type) @@ -3348,7 +3350,8 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { if mut node.expr is ast.ComptimeSelector { node.expr_type = c.type_resolver.get_comptime_selector_type(node.expr, node.expr_type) } else if node.expr is ast.Ident && c.comptime.is_comptime_variant_var(node.expr) { - node.expr_type = c.type_resolver.type_map['${c.comptime.comptime_for_variant_var}.typ'] + node.expr_type = c.type_resolver.get_ct_type_or_default('${c.comptime.comptime_for_variant_var}.typ', + ast.void_type) } mut from_type := c.unwrap_generic(node.expr_type) from_sym := c.table.sym(from_type) diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index 0f17b7947ad3d7..3b1aca10e2247c 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -250,7 +250,7 @@ fn (mut c Checker) comptime_selector(mut node ast.ComptimeSelector) ast.Type { } expr_name := node.field_expr.expr.str() if expr_name in c.type_resolver.type_map { - return c.type_resolver.type_map[expr_name] + return c.type_resolver.get_ct_type_or_default(expr_name, ast.void_type) } c.error('unknown `\$for` variable `${expr_name}`', left_pos) } else { diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index 381cd1e8d20d9d..1e1f1ee039880e 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -188,8 +188,8 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { c.comptime.comptime_for_enum_var, ] { if left.field_name == 'typ' { - skip_state = c.check_compatible_types(c.type_resolver.type_map['${comptime_field_name}.typ'], - right as ast.TypeNode) + skip_state = c.check_compatible_types(c.type_resolver.get_ct_type_or_default('${comptime_field_name}.typ', + ast.void_type), right as ast.TypeNode) } } } else if left is ast.TypeNode { @@ -608,7 +608,8 @@ fn (mut c Checker) smartcast_if_conds(mut node ast.Expr, mut scope ast.Scope, co ast.Ident { if right_expr.name == c.comptime.comptime_for_variant_var { is_comptime = true - c.type_resolver.type_map['${c.comptime.comptime_for_variant_var}.typ'] + c.type_resolver.get_ct_type_or_default('${c.comptime.comptime_for_variant_var}.typ', + ast.no_type) } else { c.error('invalid type `${right_expr}`', right_expr.pos) ast.no_type diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index 89d89ba622fa2a..47206adabed330 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -763,7 +763,8 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { } ast.Ident { if right_expr.name == c.comptime.comptime_for_variant_var { - c.type_resolver.type_map['${c.comptime.comptime_for_variant_var}.typ'] + c.type_resolver.get_ct_type_or_default('${c.comptime.comptime_for_variant_var}.typ', + ast.void_type) } else { c.error('invalid type `${right_expr}`', right_expr.pos) ast.no_type diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index 6c43fce333c7d2..ca4b5d36ed2036 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -308,17 +308,19 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { key_str := g.comptime.get_comptime_selector_key_type(val) if key_str != '' { if is_decl { - var_type = g.type_resolver.type_map[key_str] or { var_type } + var_type = g.type_resolver.get_ct_type_or_default(key_str, + var_type) val_type = var_type left.obj.typ = var_type } else { - val_type = g.type_resolver.type_map[key_str] or { var_type } + val_type = g.type_resolver.get_ct_type_or_default(key_str, + var_type) } g.assign_ct_type = var_type } } else if val is ast.ComptimeCall { key_str := '${val.method_name}.return_type' - var_type = g.type_resolver.type_map[key_str] or { var_type } + var_type = g.type_resolver.get_ct_type_or_default(key_str, var_type) left.obj.typ = var_type g.assign_ct_type = var_type } else if is_decl && val is ast.Ident && val.info is ast.IdentVar { @@ -331,7 +333,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { } else if val is ast.DumpExpr && val.expr is ast.ComptimeSelector { key_str := g.comptime.get_comptime_selector_key_type(val.expr as ast.ComptimeSelector) if key_str != '' { - var_type = g.type_resolver.type_map[key_str] or { var_type } + var_type = g.type_resolver.get_ct_type_or_default(key_str, var_type) val_type = var_type left.obj.typ = var_type } @@ -353,14 +355,14 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { var_type = fn_ret_type val_type = var_type left.obj.typ = var_type - g.type_resolver.update_generic(left, var_type) + // g.type_resolver.update_generic(left, var_type) } } else if val.is_static_method && val.left_type.has_flag(.generic) { fn_ret_type := g.resolve_return_type(val) var_type = fn_ret_type val_type = var_type left.obj.typ = var_type - g.type_resolver.update_generic(left, var_type) + // g.type_resolver.update_generic(left, var_type) g.assign_ct_type = var_type } } @@ -369,13 +371,13 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { } else if mut left is ast.ComptimeSelector { key_str := g.comptime.get_comptime_selector_key_type(left) if key_str != '' { - var_type = g.type_resolver.type_map[key_str] or { var_type } + var_type = g.type_resolver.get_ct_type_or_default(key_str, var_type) } g.assign_ct_type = var_type if val is ast.ComptimeSelector { key_str_right := g.comptime.get_comptime_selector_key_type(val) if key_str_right != '' { - val_type = g.type_resolver.type_map[key_str_right] or { var_type } + val_type = g.type_resolver.get_ct_type_or_default(key_str_right, var_type) } } else if val is ast.CallExpr { g.assign_ct_type = g.comptime.comptime_for_field_type @@ -383,7 +385,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { } else if mut left is ast.IndexExpr && val is ast.ComptimeSelector { key_str := g.comptime.get_comptime_selector_key_type(val) if key_str != '' { - val_type = g.type_resolver.type_map[key_str] or { var_type } + val_type = g.type_resolver.get_ct_type_or_default(key_str, var_type) } g.assign_ct_type = val_type } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 43bd9d63e79a74..61b8d092f93f47 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2718,7 +2718,8 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr if got_styp == 'none' && !g.cur_fn.return_type.has_flag(.option) { g.write('(none){EMPTY_STRUCT_INITIALIZATION}') } else if is_comptime_variant { - g.write(g.type_default(g.type_resolver.type_map['${g.comptime.comptime_for_variant_var}.typ'])) + g.write(g.type_default(g.type_resolver.get_ct_type_or_default('${g.comptime.comptime_for_variant_var}.typ', + ast.void_type))) } else { g.expr(expr) } @@ -4337,7 +4338,8 @@ fn (mut g Gen) debugger_stmt(node ast.DebuggerStmt) { dot := if obj.orig_type.is_ptr() || obj.is_auto_heap { '->' } else { '.' } if obj.ct_type_var == .smartcast { - cur_variant_sym := g.table.sym(g.unwrap_generic(g.type_resolver.type_map['${g.comptime.comptime_for_variant_var}.typ'])) + cur_variant_sym := g.table.sym(g.unwrap_generic(g.type_resolver.get_ct_type_or_default('${g.comptime.comptime_for_variant_var}.typ', + ast.void_type))) param_var.write_string('${obj.name}${dot}_${cur_variant_sym.cname}') } else if cast_sym.info is ast.Aggregate { sym := g.table.sym(cast_sym.info.types[g.aggregate_type_idx]) @@ -5141,8 +5143,8 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { if node_typ_is_option && node.expr is ast.None { g.gen_option_error(node.typ, node.expr) } else if node.expr is ast.Ident && g.comptime.is_comptime_variant_var(node.expr) { - g.expr_with_cast(node.expr, g.type_resolver.type_map['${g.comptime.comptime_for_variant_var}.typ'], - node_typ) + g.expr_with_cast(node.expr, g.type_resolver.get_ct_type_or_default('${g.comptime.comptime_for_variant_var}.typ', + ast.void_type), node_typ) } else if node_typ_is_option { g.expr_with_opt(node.expr, expr_type, node.typ) } else { diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 2610a08dbe9b42..684368654d85e6 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -478,7 +478,7 @@ fn (mut g Gen) get_expr_type(cond ast.Expr) ast.Type { } else { name := '${cond.expr}.${cond.field_name}' if name in g.type_resolver.type_map { - return g.type_resolver.type_map[name] + return g.type_resolver.get_ct_type_or_default(name, ast.void_type) } else { return g.unwrap_generic(cond.typ) } @@ -1083,7 +1083,8 @@ fn (mut g Gen) comptime_selector_type(node ast.SelectorExpr) ast.Type { } if g.comptime.inside_comptime_for && typ == g.enum_data_type && node.field_name == 'value' { // for comp-time enum.values - return g.type_resolver.type_map['${g.comptime.comptime_for_enum_var}.typ'] + return g.type_resolver.get_ct_type_or_default('${g.comptime.comptime_for_enum_var}.typ', + ast.void_type) } field_name := node.field_name sym := g.table.sym(typ) diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index f69407a0b4274d..9a79fc8e01d785 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1304,7 +1304,7 @@ fn (mut g Gen) gen_to_str_method_call(node ast.CallExpr) bool { if left_node is ast.ComptimeSelector { key_str := g.comptime.get_comptime_selector_key_type(left_node) if key_str != '' { - rec_type = g.type_resolver.type_map[key_str] or { rec_type } + rec_type = g.type_resolver.get_ct_type_or_default(key_str, rec_type) g.gen_expr_to_string(left_node, rec_type) return true } @@ -2276,7 +2276,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) { if expr is ast.ComptimeSelector { key_str := g.comptime.get_comptime_selector_key_type(expr) if key_str != '' { - typ = g.type_resolver.type_map[key_str] or { typ } + typ = g.type_resolver.get_ct_type_or_default(key_str, typ) } } else if expr is ast.ComptimeCall { if expr.method_name == 'method' { diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 96fadb5e254ced..784f9140581417 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -886,9 +886,8 @@ fn (mut g Gen) infix_expr_is_op(node ast.InfixExpr) { if node.right is ast.None { g.write('${ast.none_type.idx()}') } else if node.right is ast.Ident && node.right.name == g.comptime.comptime_for_variant_var { - variant_idx := g.type_resolver.type_map['${g.comptime.comptime_for_variant_var}.typ'] or { - ast.void_type - } + variant_idx := g.type_resolver.get_ct_type_or_default('${g.comptime.comptime_for_variant_var}.typ', + ast.void_type) g.write('${int(variant_idx)}') } else { g.expr(node.right) diff --git a/vlib/v/type_resolver/comptime_resolver.v b/vlib/v/type_resolver/comptime_resolver.v index 67ebd67949648e..685cecd1b8065c 100644 --- a/vlib/v/type_resolver/comptime_resolver.v +++ b/vlib/v/type_resolver/comptime_resolver.v @@ -113,10 +113,10 @@ pub fn (mut t TypeResolver) get_expr_type_or_default(node ast.Expr, default_typ pub fn (t &TypeResolver) get_type_from_comptime_var(var ast.Ident) ast.Type { return match var.name { t.info.comptime_for_variant_var { - t.type_map['${t.info.comptime_for_variant_var}.typ'] + t.get_ct_type_or_default('${t.info.comptime_for_variant_var}.typ', ast.void_type) } t.info.comptime_for_method_param_var { - t.type_map['${t.info.comptime_for_method_param_var}.typ'] + t.get_ct_type_or_default('${t.info.comptime_for_method_param_var}.typ', ast.void_type) } else { // field_var.typ from $for field diff --git a/vlib/v/type_resolver/type_resolver.v b/vlib/v/type_resolver/type_resolver.v index 943396741bbdd9..c786591713d762 100644 --- a/vlib/v/type_resolver/type_resolver.v +++ b/vlib/v/type_resolver/type_resolver.v @@ -72,7 +72,7 @@ pub fn TypeResolver.new(table &ast.Table, resolver &IResolverType) &TypeResolver @[inline] pub fn (mut t TypeResolver) update_generic(var ast.Ident, var_type ast.Type) { if var.obj is ast.Var { - t.type_map['g.${var.name}.${var.obj.pos.pos}'] = var_type + // t.type_map['g.${var.name}.${var.obj.pos.pos}'] = var_type } } @@ -86,6 +86,12 @@ pub fn (mut t TypeResolver) update_ct_type(key string, var_type ast.Type) { t.type_map[key] = var_type } +// get_ct_type_or_default retrieves a comptime variable value on type map or default_type otherwise +@[inline] +pub fn (t &TypeResolver) get_ct_type_or_default(key string, default_type ast.Type) ast.Type { + return t.type_map[key] or { default_type } +} + @[noreturn] fn (t &TypeResolver) error(s string, pos token.Pos) { util.show_compiler_message('cgen error:', pos: pos, file_path: t.resolver.file.path, message: s) @@ -136,7 +142,7 @@ pub fn (mut t TypeResolver) get_type(node ast.Expr) ast.Type { if node.obj is ast.Var { return match node.obj.ct_type_var { .generic_param { - // generic parameter from infoent function + // generic parameter from generic function node.obj.typ } .generic_var { @@ -144,11 +150,12 @@ pub fn (mut t TypeResolver) get_type(node ast.Expr) ast.Type { if node.obj.smartcasts.len > 0 { node.obj.smartcasts.last() } else { - t.type_map['t.${node.name}.${node.obj.pos.pos}'] or { node.obj.typ } + node.obj.typ } } .smartcast { - ctyp := t.type_map['${t.info.comptime_for_variant_var}.typ'] or { node.obj.typ } + ctyp := t.get_ct_type_or_default('${t.info.comptime_for_variant_var}.typ', + node.obj.typ) return if (node.obj as ast.Var).is_unwrapped { ctyp.clear_flag(.option) } else { @@ -157,7 +164,7 @@ pub fn (mut t TypeResolver) get_type(node ast.Expr) ast.Type { } .key_var, .value_var { // key and value variables from normal for stmt - t.type_map[node.name] or { ast.void_type } + t.get_ct_type_or_default(node.name, ast.void_type) } .field_var { // field var from $for loop From b56d6538baf864a1c26e28c4a19a8c071be34bcc Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 29 Dec 2024 10:51:11 -0300 Subject: [PATCH 3/6] cleanup --- vlib/v/checker/assign.v | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index 3f8d115c4647c5..7eeec93471d117 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -412,24 +412,24 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { // mark variable as generic var because its type changes according to fn return generic resolution type left.obj.ct_type_var = .generic_var if right.return_type_generic.has_flag(.generic) { - fn_ret_type := c.resolve_return_type(right) - if fn_ret_type != ast.void_type - && c.table.final_sym(fn_ret_type).kind != .multi_return { - var_type := if right.or_block.kind == .absent { - fn_ret_type - } else { - fn_ret_type.clear_option_and_result() - } - // c.type_resolver.update_generic(left, var_type) - } + // fn_ret_type := c.resolve_return_type(right) + // if fn_ret_type != ast.void_type + // && c.table.final_sym(fn_ret_type).kind != .multi_return { + // var_type := if right.or_block.kind == .absent { + // fn_ret_type + // } else { + // fn_ret_type.clear_option_and_result() + // } + // // c.type_resolver.update_generic(left, var_type) + // } } else if right.is_static_method && right.left_type.has_flag(.generic) { - fn_ret_type := c.unwrap_generic(c.resolve_return_type(right)) - var_type := if right.or_block.kind == .absent { - fn_ret_type - } else { - fn_ret_type.clear_option_and_result() - } + // fn_ret_type := c.unwrap_generic(c.resolve_return_type(right)) + // var_type := if right.or_block.kind == .absent { + // fn_ret_type + // } else { + // fn_ret_type.clear_option_and_result() + // } // c.type_resolver.update_generic(left, var_type) } } From c72231c413ec24463e6d447914f369e033a463c8 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 29 Dec 2024 10:56:53 -0300 Subject: [PATCH 4/6] cleanup --- vlib/v/checker/assign.v | 21 --------------------- vlib/v/gen/c/assign.v | 2 -- vlib/v/type_resolver/type_resolver.v | 9 --------- 3 files changed, 32 deletions(-) diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index 7eeec93471d117..5494e2eadf80a1 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -411,27 +411,6 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { && c.type_resolver.is_generic_expr(right) { // mark variable as generic var because its type changes according to fn return generic resolution type left.obj.ct_type_var = .generic_var - if right.return_type_generic.has_flag(.generic) { - // fn_ret_type := c.resolve_return_type(right) - // if fn_ret_type != ast.void_type - // && c.table.final_sym(fn_ret_type).kind != .multi_return { - // var_type := if right.or_block.kind == .absent { - // fn_ret_type - // } else { - // fn_ret_type.clear_option_and_result() - // } - // // c.type_resolver.update_generic(left, var_type) - // } - } else if right.is_static_method - && right.left_type.has_flag(.generic) { - // fn_ret_type := c.unwrap_generic(c.resolve_return_type(right)) - // var_type := if right.or_block.kind == .absent { - // fn_ret_type - // } else { - // fn_ret_type.clear_option_and_result() - // } - // c.type_resolver.update_generic(left, var_type) - } } } } diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index ca4b5d36ed2036..34420e0efe7ca3 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -355,14 +355,12 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { var_type = fn_ret_type val_type = var_type left.obj.typ = var_type - // g.type_resolver.update_generic(left, var_type) } } else if val.is_static_method && val.left_type.has_flag(.generic) { fn_ret_type := g.resolve_return_type(val) var_type = fn_ret_type val_type = var_type left.obj.typ = var_type - // g.type_resolver.update_generic(left, var_type) g.assign_ct_type = var_type } } diff --git a/vlib/v/type_resolver/type_resolver.v b/vlib/v/type_resolver/type_resolver.v index c786591713d762..fc0225faa0f937 100644 --- a/vlib/v/type_resolver/type_resolver.v +++ b/vlib/v/type_resolver/type_resolver.v @@ -67,15 +67,6 @@ pub fn TypeResolver.new(table &ast.Table, resolver &IResolverType) &TypeResolver } } -// update_generic updates generic type for specific variables (for overwriting its last known checker type) -// `var` are usually variables as `var := fn_returning_generic()` -@[inline] -pub fn (mut t TypeResolver) update_generic(var ast.Ident, var_type ast.Type) { - if var.obj is ast.Var { - // t.type_map['g.${var.name}.${var.obj.pos.pos}'] = var_type - } -} - // update_ct_type updates current type for specific key (comptime vars) // `var` iteration vars, comptime vars // `var.typ` => for comptime $for variables From 1f6af1511c032315c5bf6b3ba4db4e8283bd6f5f Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 29 Dec 2024 11:11:29 -0300 Subject: [PATCH 5/6] cleanup --- vlib/v/checker/fn.v | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index dd416f869e34a4..e5c7a3001ab7fa 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -4005,29 +4005,6 @@ fn (mut c Checker) resolve_fn_return_type(func &ast.Fn, node ast.CallExpr) ast.T return ret_type } -// resolve_return_type resolves the generic return type of CallExpr -fn (mut c Checker) resolve_return_type(node ast.CallExpr) ast.Type { - if node.is_method { - left_sym := c.table.sym(c.unwrap_generic(node.left_type)) - if method := c.table.find_method(left_sym, node.name) { - return c.resolve_fn_return_type(method, node) - } - } else if node.is_static_method { - if c.table.cur_fn != unsafe { nil } { - _, name := c.table.convert_generic_static_type_name(node.name, c.table.cur_fn.generic_names, - c.table.cur_concrete_types) - if func := c.table.find_fn(name) { - return c.resolve_fn_return_type(func, node) - } - } - } else { - if func := c.table.find_fn(node.name) { - return c.resolve_fn_return_type(func, node) - } - } - return node.return_type -} - fn (mut c Checker) check_must_use_call_result(node &ast.CallExpr, f &ast.Fn, label string) { if node.is_return_used { return From 21a5dc4b94689dce83157bd06a938a8586cd455e Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 29 Dec 2024 12:00:34 -0300 Subject: [PATCH 6/6] fix autofree --- vlib/v/gen/c/cgen.v | 5 +++-- vlib/v/type_resolver/comptime_resolver.v | 10 ++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 61b8d092f93f47..f430d812c80f20 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2718,8 +2718,9 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr if got_styp == 'none' && !g.cur_fn.return_type.has_flag(.option) { g.write('(none){EMPTY_STRUCT_INITIALIZATION}') } else if is_comptime_variant { - g.write(g.type_default(g.type_resolver.get_ct_type_or_default('${g.comptime.comptime_for_variant_var}.typ', - ast.void_type))) + ctyp := g.type_resolver.get_ct_type_or_default('${g.comptime.comptime_for_variant_var}.typ', + ast.void_type) + g.write(g.type_default(ctyp)) } else { g.expr(expr) } diff --git a/vlib/v/type_resolver/comptime_resolver.v b/vlib/v/type_resolver/comptime_resolver.v index 685cecd1b8065c..48ad48ad38de91 100644 --- a/vlib/v/type_resolver/comptime_resolver.v +++ b/vlib/v/type_resolver/comptime_resolver.v @@ -111,16 +111,18 @@ pub fn (mut t TypeResolver) get_expr_type_or_default(node ast.Expr, default_typ // get_type_from_comptime_var retrives the comptime type related to $for variable @[inline] pub fn (t &TypeResolver) get_type_from_comptime_var(var ast.Ident) ast.Type { - return match var.name { + match var.name { t.info.comptime_for_variant_var { - t.get_ct_type_or_default('${t.info.comptime_for_variant_var}.typ', ast.void_type) + return t.get_ct_type_or_default('${t.info.comptime_for_variant_var}.typ', + ast.void_type) } t.info.comptime_for_method_param_var { - t.get_ct_type_or_default('${t.info.comptime_for_method_param_var}.typ', ast.void_type) + return t.get_ct_type_or_default('${t.info.comptime_for_method_param_var}.typ', + ast.void_type) } else { // field_var.typ from $for field - t.info.comptime_for_field_type + return t.info.comptime_for_field_type } } }