Skip to content

Commit

Permalink
v.type_resolver: optimize infix ct checking (#23362)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Jan 4, 2025
1 parent 3ed799e commit 8774f77
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 2 additions & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions vlib/v/type_resolver/comptime_resolver.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/type_resolver/type_resolver.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8774f77

Please sign in to comment.