mirror of
https://github.com/vlang/v.git
synced 2025-09-10 16:00:31 -04:00
v.type_resolver: optimize infix ct checking (#23362)
This commit is contained in:
parent
3ed799ef7b
commit
8774f7761d
@ -1134,8 +1134,10 @@ pub mut:
|
|||||||
|
|
||||||
ct_left_value_evaled bool
|
ct_left_value_evaled bool
|
||||||
ct_left_value ComptTimeConstValue = empty_comptime_const_value
|
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_evaled bool
|
||||||
ct_right_value ComptTimeConstValue = empty_comptime_const_value
|
ct_right_value ComptTimeConstValue = empty_comptime_const_value
|
||||||
|
right_ct_expr bool // true when right is comptime/generic expr
|
||||||
|
|
||||||
before_op_comments []Comment
|
before_op_comments []Comment
|
||||||
after_op_comments []Comment
|
after_op_comments []Comment
|
||||||
|
@ -18,6 +18,13 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
|
|||||||
c.expected_type = chan_info.elem_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 n is ast.Ident && n.is_mut { ... }`
|
||||||
if !c.inside_sql && node.op == .and {
|
if !c.inside_sql && node.op == .and {
|
||||||
mut left_node := node.left
|
mut left_node := node.left
|
||||||
|
@ -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
|
&& node.op in [.plus, .minus, .mul, .div, .mod] && !(g.pref.translated
|
||||||
|| g.file.is_translated)
|
|| g.file.is_translated)
|
||||||
if needs_cast {
|
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))
|
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))
|
g.styp(g.type_resolver.get_type_or_default(node.right, node.promoted_type))
|
||||||
} else {
|
} else {
|
||||||
g.styp(node.promoted_type)
|
g.styp(node.promoted_type)
|
||||||
|
@ -50,11 +50,7 @@ pub fn (t &ResolverInfo) is_comptime(node ast.Expr) bool {
|
|||||||
return node.expr is ast.Ident && node.expr.ct_expr
|
return node.expr is ast.Ident && node.expr.ct_expr
|
||||||
}
|
}
|
||||||
ast.InfixExpr {
|
ast.InfixExpr {
|
||||||
if node.op in [.plus, .minus, .mul, .div, .mod] {
|
return node.left_ct_expr || node.right_ct_expr
|
||||||
t.is_comptime(node.left) || t.is_comptime(node.right)
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ast.ParExpr {
|
ast.ParExpr {
|
||||||
return t.is_comptime(node.expr)
|
return t.is_comptime(node.expr)
|
||||||
|
@ -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 {
|
ast.CastExpr {
|
||||||
if node.typ.has_flag(.generic) {
|
if node.typ.has_flag(.generic) {
|
||||||
return t.resolver.unwrap_generic(node.typ)
|
return t.resolver.unwrap_generic(node.typ)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user