diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index eaa27480bf..acf26ee2f4 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -505,9 +505,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { left.recursive_mapset_is_setter(true) } - if right is ast.Ident && c.comptime.is_comptime_var(right) { - right_type = c.comptime.get_type(right) - } + right_type = c.comptime.get_type_or_default(right, right_type) } if mut left is ast.InfixExpr { c.error('cannot use infix expression on the left side of `${node.op}`', diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 3b78c76cd5..e3034529c3 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3803,16 +3803,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type { // second use if node.kind in [.constant, .global, .variable] { info := node.info as ast.IdentVar - typ := if c.comptime.is_comptime_var(node) { - ctype := c.comptime.get_type(node) - if ctype != ast.void_type { - ctype - } else { - info.typ - } - } else { - info.typ - } + typ := c.comptime.get_type_or_default(node, info.typ) // Got a var with type T, return current generic type if node.or_expr.kind != .absent { if !typ.has_flag(.option) { diff --git a/vlib/v/checker/str.v b/vlib/v/checker/str.v index a6266315de..c4d781cc32 100644 --- a/vlib/v/checker/str.v +++ b/vlib/v/checker/str.v @@ -45,13 +45,8 @@ fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Type { c.inside_interface_deref = true for i, mut expr in node.exprs { mut ftyp := c.expr(mut expr) - ftyp = c.check_expr_option_or_result_call(expr, ftyp) - if c.comptime.is_comptime_var(expr) { - ctyp := c.comptime.get_type(expr) - if ctyp != ast.void_type { - ftyp = ctyp - } - } + ftyp = c.comptime.get_type_or_default(expr, c.check_expr_option_or_result_call(expr, + ftyp)) if ftyp == ast.void_type { c.error('expression does not return a value', expr.pos()) } else if ftyp == ast.char_type && ftyp.nr_muls() == 0 { diff --git a/vlib/v/comptime/comptimeinfo.v b/vlib/v/comptime/comptimeinfo.v index 2eef68dbbe..f16f39dfa0 100644 --- a/vlib/v/comptime/comptimeinfo.v +++ b/vlib/v/comptime/comptimeinfo.v @@ -80,6 +80,22 @@ pub fn (mut ct ComptimeInfo) get_expr_type(node ast.Expr) ast.Type { return ctyp } +// get_type_or_default retries the comptime value if the AST node is related to comptime otherwise default_typ is returned +pub fn (mut ct ComptimeInfo) get_type_or_default(node ast.Expr, default_typ ast.Type) ast.Type { + match node { + ast.Ident { + if ct.is_comptime_var(node) { + ctyp := ct.get_type(node) + return if ctyp != ast.void_type { ctyp } else { default_typ } + } + } + else { + return default_typ + } + } + return default_typ +} + // get_type retrieves the actual type from a comptime related ast node @[inline] pub fn (mut ct ComptimeInfo) get_type(node ast.Expr) ast.Type { diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 6259922d44..e4db56346e 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -459,11 +459,7 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) { fn (mut g Gen) get_expr_type(cond ast.Expr) ast.Type { match cond { ast.Ident { - return if g.comptime.is_comptime_var(cond) { - g.unwrap_generic(g.comptime.get_type(cond)) - } else { - g.unwrap_generic(cond.obj.typ) - } + return g.unwrap_generic(g.comptime.get_type_or_default(cond, cond.obj.typ)) } ast.TypeNode { return g.unwrap_generic(cond.typ) diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 8fe9c0fbc5..b916ceffed 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -2178,13 +2178,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) { defer { g.inside_interface_deref = false } - mut typ := node.args[0].typ - if g.comptime.is_comptime_var(node.args[0].expr) { - ctyp := g.comptime.get_type(node.args[0].expr) - if ctyp != ast.void_type { - typ = ctyp - } - } + mut typ := g.comptime.get_type_or_default(node.args[0].expr, node.args[0].typ) if typ == 0 { g.checker_bug('print arg.typ is 0', node.pos) } diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 031b823723..45cf38b6fc 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -795,11 +795,8 @@ fn (mut g Gen) infix_expr_in_optimization(left ast.Expr, left_type ast.Type, rig // infix_expr_is_op generates code for `is` and `!is` fn (mut g Gen) infix_expr_is_op(node ast.InfixExpr) { - mut left_sym := if g.comptime.is_comptime_var(node.left) { - g.table.sym(g.unwrap_generic(g.comptime.get_type(node.left))) - } else { - g.table.sym(node.left_type) - } + mut left_sym := g.table.sym(g.unwrap_generic(g.comptime.get_type_or_default(node.left, + node.left_type))) is_aggregate := left_sym.kind == .aggregate if is_aggregate { parent_left_type := (left_sym.info as ast.Aggregate).sum_type