diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 483f75578a3bdf..237e835031d982 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -1134,8 +1134,10 @@ pub mut: ct_left_value_evaled bool ct_left_value ComptTimeConstValue = empty_comptime_const_value + left_ct_expr bool // true when left is comptime/generic expr ct_right_value_evaled bool ct_right_value ComptTimeConstValue = empty_comptime_const_value + right_ct_expr bool // true when right is comptime/generic expr before_op_comments []Comment after_op_comments []Comment diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index 1495fd72493150..eb8abd60616e96 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -18,6 +18,13 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { c.expected_type = chan_info.elem_type } + if !node.left_ct_expr && !node.left.is_literal() { + node.left_ct_expr = c.comptime.is_comptime(node.left) + } + if !node.right_ct_expr && !node.right.is_literal() { + node.right_ct_expr = c.comptime.is_comptime(node.right) + } + // `if n is ast.Ident && n.is_mut { ... }` if !c.inside_sql && node.op == .and { mut left_node := node.left diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index ad68ab8db72351..36c447abc8dc64 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -1188,9 +1188,9 @@ fn (mut g Gen) gen_plain_infix_expr(node ast.InfixExpr) { && node.op in [.plus, .minus, .mul, .div, .mod] && !(g.pref.translated || g.file.is_translated) if needs_cast { - typ_str := if !node.left.is_literal() && g.comptime.is_comptime(node.left) { + typ_str := if node.left_ct_expr { g.styp(g.type_resolver.get_type_or_default(node.left, node.promoted_type)) - } else if !node.right.is_literal() && g.comptime.is_comptime(node.right) { + } else if node.right_ct_expr { g.styp(g.type_resolver.get_type_or_default(node.right, node.promoted_type)) } else { g.styp(node.promoted_type) diff --git a/vlib/v/type_resolver/comptime_resolver.v b/vlib/v/type_resolver/comptime_resolver.v index c7cf6b0236df60..d09e9a60af89fa 100644 --- a/vlib/v/type_resolver/comptime_resolver.v +++ b/vlib/v/type_resolver/comptime_resolver.v @@ -50,11 +50,7 @@ pub fn (t &ResolverInfo) is_comptime(node ast.Expr) bool { return node.expr is ast.Ident && node.expr.ct_expr } ast.InfixExpr { - if node.op in [.plus, .minus, .mul, .div, .mod] { - t.is_comptime(node.left) || t.is_comptime(node.right) - } else { - false - } + return node.left_ct_expr || node.right_ct_expr } ast.ParExpr { return t.is_comptime(node.expr) diff --git a/vlib/v/type_resolver/type_resolver.v b/vlib/v/type_resolver/type_resolver.v index 0bdeb8b48d4bbf..484d28583c808b 100644 --- a/vlib/v/type_resolver/type_resolver.v +++ b/vlib/v/type_resolver/type_resolver.v @@ -130,6 +130,10 @@ pub fn (mut t TypeResolver) get_type_or_default(node ast.Expr, default_typ ast.T } } } + ast.ComptimeSelector { + // val.$(field.name) + return t.get_comptime_selector_type(node, ast.void_type) + } ast.CastExpr { if node.typ.has_flag(.generic) { return t.resolver.unwrap_generic(node.typ)