1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: use autocasting in complex if conditions (#18753)

This commit is contained in:
yuyi 2023-07-03 16:03:05 +08:00 committed by GitHub
parent c48ae86132
commit df3c85eb36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -680,8 +680,7 @@ fn (mut c Checker) comptime_if_branch(cond ast.Expr, pos token.Pos) ComptimeBran
} else if cond.left in [ast.Ident, ast.SelectorExpr, ast.TypeNode] { } else if cond.left in [ast.Ident, ast.SelectorExpr, ast.TypeNode] {
// `$if method.@type is string` // `$if method.@type is string`
c.expr(cond.left) c.expr(cond.left)
if cond.left is ast.SelectorExpr if cond.left is ast.SelectorExpr && c.is_comptime_selector_type(cond.left)
&& c.is_comptime_selector_type(cond.left as ast.SelectorExpr)
&& cond.right is ast.ComptimeType { && cond.right is ast.ComptimeType {
checked_type := c.get_comptime_var_type(cond.left) checked_type := c.get_comptime_var_type(cond.left)
return if c.table.is_comptime_type(checked_type, cond.right) { return if c.table.is_comptime_type(checked_type, cond.right) {
@ -703,10 +702,10 @@ fn (mut c Checker) comptime_if_branch(cond ast.Expr, pos token.Pos) ComptimeBran
// $if method.args.len == 1 // $if method.args.len == 1
return .unknown return .unknown
} else if cond.left is ast.SelectorExpr } else if cond.left is ast.SelectorExpr
&& c.check_comptime_is_field_selector_bool(cond.left as ast.SelectorExpr) { && c.check_comptime_is_field_selector_bool(cond.left) {
// field.is_public (from T.fields) // field.is_public (from T.fields)
} else if cond.right is ast.SelectorExpr } else if cond.right is ast.SelectorExpr
&& c.check_comptime_is_field_selector_bool(cond.right as ast.SelectorExpr) { && c.check_comptime_is_field_selector_bool(cond.right) {
// field.is_public (from T.fields) // field.is_public (from T.fields)
} else if cond.left is ast.Ident { } else if cond.left is ast.Ident {
// $if version == 2 // $if version == 2
@ -757,12 +756,9 @@ fn (mut c Checker) comptime_if_branch(cond ast.Expr, pos token.Pos) ComptimeBran
} }
} }
.gt, .lt, .ge, .le { .gt, .lt, .ge, .le {
if cond.left is ast.SelectorExpr && cond.right is ast.IntegerLiteral { if cond.left is ast.SelectorExpr && cond.right is ast.IntegerLiteral
if c.is_comptime_selector_field_name(cond.left as ast.SelectorExpr, && c.is_comptime_selector_field_name(cond.left, 'indirections') {
'indirections') return .unknown
{
return .unknown
}
} }
c.error('invalid `\$if` condition', cond.pos) c.error('invalid `\$if` condition', cond.pos)
} }
@ -905,9 +901,8 @@ fn (mut c Checker) comptime_if_branch(cond ast.Expr, pos token.Pos) ComptimeBran
// get_comptime_selector_type retrieves the var.$(field.name) type when field_name is 'name' otherwise default_type is returned // get_comptime_selector_type retrieves the var.$(field.name) type when field_name is 'name' otherwise default_type is returned
[inline] [inline]
fn (mut c Checker) get_comptime_selector_type(node ast.ComptimeSelector, default_type ast.Type) ast.Type { fn (mut c Checker) get_comptime_selector_type(node ast.ComptimeSelector, default_type ast.Type) ast.Type {
if node.field_expr is ast.SelectorExpr if node.field_expr is ast.SelectorExpr && c.check_comptime_is_field_selector(node.field_expr)
&& c.check_comptime_is_field_selector(node.field_expr as ast.SelectorExpr) && node.field_expr.field_name == 'name' {
&& (node.field_expr as ast.SelectorExpr).field_name == 'name' {
return c.unwrap_generic(c.comptime_fields_default_type) return c.unwrap_generic(c.comptime_fields_default_type)
} }
return default_type return default_type