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

v: use autocasting in complex conditions (#18731)

This commit is contained in:
yuyi 2023-07-02 14:23:15 +08:00 committed by GitHub
parent 29c8aaeb89
commit 5d4c2cd832
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 28 additions and 31 deletions

View File

@ -346,7 +346,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
left.obj.typ = c.comptime_fields_default_type
}
} else if mut right is ast.Ident && right.obj is ast.Var
&& (right as ast.Ident).or_expr.kind == .absent {
&& right.or_expr.kind == .absent {
if (right.obj as ast.Var).ct_type_var != .no_comptime {
ctyp := c.get_comptime_var_type(right)
if ctyp != ast.void_type {
@ -705,8 +705,8 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
}
}
}
if left_sym.kind == .struct_ && !(left_sym.info as ast.Struct).is_anon
&& right is ast.StructInit && (right as ast.StructInit).is_anon {
if left_sym.info is ast.Struct && !left_sym.info.is_anon && right is ast.StructInit
&& right.is_anon {
c.error('cannot assign anonymous `struct` to a typed `struct`', right.pos())
}
if right_sym.kind == .alias && right_sym.name == 'byte' {

View File

@ -182,12 +182,12 @@ fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool {
fn (c Checker) check_multiple_ptr_match(got ast.Type, expected ast.Type, param ast.Param, arg ast.CallArg) bool {
param_nr_muls := if param.is_mut && !expected.is_ptr() { 1 } else { expected.nr_muls() }
if got.is_ptr() && got.nr_muls() > 1 && got.nr_muls() != param_nr_muls {
if arg.expr is ast.PrefixExpr && (arg.expr as ast.PrefixExpr).op == .amp {
if arg.expr is ast.PrefixExpr && arg.expr.op == .amp {
return false
}
if arg.expr is ast.UnsafeExpr {
expr := (arg.expr as ast.UnsafeExpr).expr
if expr is ast.PrefixExpr && (expr as ast.PrefixExpr).op == .amp {
expr := arg.expr.expr
if expr is ast.PrefixExpr && expr.op == .amp {
return false
}
}
@ -243,7 +243,7 @@ fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type, lan
}
// check int signed/unsigned mismatch
if got == ast.int_literal_type_idx && expected in ast.unsigned_integer_type_idxs
&& arg.expr is ast.IntegerLiteral && (arg.expr as ast.IntegerLiteral).val.i64() < 0 {
&& arg.expr is ast.IntegerLiteral && arg.expr.val.i64() < 0 {
expected_typ_str := c.table.type_to_str(expected.clear_flag(.variadic))
return error('cannot use literal signed integer as `${expected_typ_str}`')
}
@ -931,8 +931,8 @@ fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr) {
}
}
if arg.expr.is_auto_deref_var() || (arg.expr is ast.ComptimeSelector
&& (arg.expr as ast.ComptimeSelector).left.is_auto_deref_var()) {
if arg.expr.is_auto_deref_var()
|| (arg.expr is ast.ComptimeSelector && arg.expr.left.is_auto_deref_var()) {
typ = typ.deref()
}
// resolve &T &&T ...

View File

@ -1604,12 +1604,12 @@ fn (mut c Checker) const_decl(mut node ast.ConstDecl) {
first_stmts := field.expr.branches[0].stmts
second_stmts := field.expr.branches[1].stmts
if first_stmts.len > 0 && first_stmts.last() is ast.ExprStmt
&& (first_stmts.last() as ast.ExprStmt).typ != ast.void_type {
&& first_stmts.last().typ != ast.void_type {
field.expr.is_expr = true
field.expr.typ = (first_stmts.last() as ast.ExprStmt).typ
field.typ = field.expr.typ
} else if second_stmts.len > 0 && second_stmts.last() is ast.ExprStmt
&& (second_stmts.last() as ast.ExprStmt).typ != ast.void_type {
&& second_stmts.last().typ != ast.void_type {
field.expr.is_expr = true
field.expr.typ = (second_stmts.last() as ast.ExprStmt).typ
field.typ = field.expr.typ
@ -2828,9 +2828,8 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
// node.typ: `Outside`
node.expr_type = c.expr(node.expr) // type to be casted
if node.expr is ast.ComptimeSelector {
node.expr_type = c.get_comptime_selector_type(node.expr as ast.ComptimeSelector,
node.expr_type)
if mut node.expr is ast.ComptimeSelector {
node.expr_type = c.get_comptime_selector_type(node.expr, node.expr_type)
}
mut from_type := c.unwrap_generic(node.expr_type)
@ -3821,8 +3820,8 @@ fn (c &Checker) has_return(stmts []ast.Stmt) ?bool {
[inline]
pub fn (mut c Checker) is_comptime_var(node ast.Expr) bool {
return node is ast.Ident && node.info is ast.IdentVar
&& (node as ast.Ident).kind == .variable && ((node as ast.Ident).obj as ast.Var).ct_type_var != .no_comptime
return node is ast.Ident && node.info is ast.IdentVar && node.kind == .variable
&& ((node as ast.Ident).obj as ast.Var).ct_type_var != .no_comptime
}
fn (mut c Checker) mark_as_referenced(mut node ast.Expr, as_interface bool) {

View File

@ -41,7 +41,7 @@ fn (mut c Checker) get_comptime_var_type(node ast.Expr) ast.Type {
} else if node is ast.ComptimeSelector {
// val.$(field.name)
return c.get_comptime_selector_type(node, ast.void_type)
} else if node is ast.SelectorExpr && c.is_comptime_selector_type(node as ast.SelectorExpr) {
} else if node is ast.SelectorExpr && c.is_comptime_selector_type(node) {
// field_var.typ from $for field
return c.comptime_fields_default_type
}

View File

@ -243,14 +243,14 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
left_sym = c.table.sym(unwrapped_left_type)
unwrapped_right_type := c.unwrap_generic(right_type)
right_sym = c.table.sym(unwrapped_right_type)
if mut right_sym.info is ast.Alias && (right_sym.info as ast.Alias).language != .c
if mut right_sym.info is ast.Alias && right_sym.info.language != .c
&& c.mod == c.table.type_to_str(unwrapped_right_type).split('.')[0]
&& c.table.sym((right_sym.info as ast.Alias).parent_type).is_primitive() {
&& c.table.sym(right_sym.info.parent_type).is_primitive() {
right_sym = c.table.sym(right_sym.info.parent_type)
}
if mut left_sym.info is ast.Alias && (left_sym.info as ast.Alias).language != .c
if mut left_sym.info is ast.Alias && left_sym.info.language != .c
&& c.mod == c.table.type_to_str(unwrapped_left_type).split('.')[0]
&& c.table.sym((left_sym.info as ast.Alias).parent_type).is_primitive() {
&& c.table.sym(left_sym.info.parent_type).is_primitive() {
left_sym = c.table.sym(left_sym.info.parent_type)
}
@ -259,7 +259,7 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
&& unwrapped_right_type.is_any_kind_of_pointer() {
return_type = left_type
} else if !c.pref.translated && left_sym.info is ast.Alias
&& !(c.table.sym((left_sym.info as ast.Alias).parent_type).is_primitive()) {
&& !(c.table.sym(left_sym.info.parent_type).is_primitive()) {
if left_sym.has_method(node.op.str()) {
if method := left_sym.find_method(node.op.str()) {
return_type = method.return_type
@ -284,7 +284,7 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
}
}
} else if !c.pref.translated && right_sym.info is ast.Alias
&& !(c.table.sym((right_sym.info as ast.Alias).parent_type).is_primitive()) {
&& !(c.table.sym(right_sym.info.parent_type).is_primitive()) {
if right_sym.has_method(node.op.str()) {
if method := right_sym.find_method(node.op.str()) {
return_type = method.return_type

View File

@ -3755,7 +3755,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
g.write(embed_name)
}
}
alias_to_ptr := sym.info is ast.Alias && (sym.info as ast.Alias).parent_type.is_ptr()
alias_to_ptr := sym.info is ast.Alias && sym.info.parent_type.is_ptr()
if field_is_opt
|| ((node.expr_type.is_ptr() || sym.kind == .chan || alias_to_ptr)
&& node.from_embed_types.len == 0) {

View File

@ -1719,7 +1719,7 @@ pub fn (mut f Gen) infix_expr(node ast.InfixExpr) {
left_type_sym := f.table.final_sym(node.left_type)
is_array_push := left_type_sym.kind == .array && node.op == .left_shift
is_one_val_array_init := node.op in [.key_in, .not_in] && node.right is ast.ArrayInit
&& (node.right as ast.ArrayInit).exprs.len == 1
&& node.right.exprs.len == 1
if is_one_val_array_init {
// `var in [val]` => `var == val`
op := if node.op == .key_in { ' == ' } else { ' != ' }

View File

@ -2247,8 +2247,7 @@ fn (mut g JsGen) need_tmp_var_in_match(node ast.MatchExpr) bool {
if branch.stmts[0] is ast.ExprStmt {
stmt := branch.stmts[0] as ast.ExprStmt
if stmt.expr in [ast.CallExpr, ast.IfExpr, ast.MatchExpr]
|| (stmt.expr is ast.IndexExpr
&& (stmt.expr as ast.IndexExpr).or_expr.kind != .absent) {
|| (stmt.expr is ast.IndexExpr && stmt.expr.or_expr.kind != .absent) {
return true
}
}

View File

@ -647,8 +647,7 @@ fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr {
p.inside_in_array = false
}
p.expecting_type = prev_expecting_type
if p.pref.is_vet && op in [.key_in, .not_in] && right is ast.ArrayInit
&& (right as ast.ArrayInit).exprs.len == 1 {
if p.pref.is_vet && op in [.key_in, .not_in] && right is ast.ArrayInit && right.exprs.len == 1 {
p.vet_error('Use `var == value` instead of `var in [value]`', pos.line_nr, vet.FixKind.vfmt,
.default)
}

View File

@ -330,7 +330,7 @@ pub fn (mut p Parser) parse() &ast.File {
}
stmt := p.top_stmt()
// clear the attributes after each statement
if !(stmt is ast.ExprStmt && (stmt as ast.ExprStmt).expr is ast.Comment) {
if !(stmt is ast.ExprStmt && stmt.expr is ast.Comment) {
p.attrs = []
}
stmts << stmt
@ -2103,7 +2103,7 @@ fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt {
if (is_top_level || p.tok.kind != .rcbr)
&& node !in [ast.CallExpr, ast.PostfixExpr, ast.ComptimeCall, ast.SelectorExpr, ast.DumpExpr] {
is_complex_infix_expr := node is ast.InfixExpr
&& (node as ast.InfixExpr).op in [.left_shift, .right_shift, .unsigned_right_shift, .arrow]
&& node.op in [.left_shift, .right_shift, .unsigned_right_shift, .arrow]
if !is_complex_infix_expr {
return p.error_with_pos('expression evaluated but not used', node.pos())
}