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 (#18839)

This commit is contained in:
yuyi 2023-07-11 19:49:43 +08:00 committed by GitHub
parent 6b29d628c3
commit 6b792b1257
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 10 additions and 14 deletions

View File

@ -3825,7 +3825,7 @@ fn (c &Checker) has_return(stmts []ast.Stmt) ?bool {
[inline] [inline]
pub fn (mut c Checker) is_comptime_var(node ast.Expr) bool { pub fn (mut c Checker) is_comptime_var(node ast.Expr) bool {
return node is ast.Ident && node.info is ast.IdentVar && node.kind == .variable 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 && (node.obj as ast.Var).ct_type_var != .no_comptime
} }
fn (mut c Checker) mark_as_referenced(mut node ast.Expr, as_interface bool) { fn (mut c Checker) mark_as_referenced(mut node ast.Expr, as_interface bool) {

View File

@ -27,8 +27,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
mut node_is_expr := false mut node_is_expr := false
if node.branches.len > 0 && node.has_else { if node.branches.len > 0 && node.has_else {
stmts := node.branches[0].stmts stmts := node.branches[0].stmts
if stmts.len > 0 && stmts.last() is ast.ExprStmt if stmts.len > 0 && stmts.last() is ast.ExprStmt && stmts.last().typ != ast.void_type {
&& (stmts.last() as ast.ExprStmt).typ != ast.void_type {
node_is_expr = true node_is_expr = true
} }
} }

View File

@ -779,9 +779,8 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
} }
} }
/* /*
if (node.left is ast.InfixExpr && if (node.left is ast.InfixExpr && node.left.op == .inc) ||
(node.left as ast.InfixExpr).op == .inc) || (node.right is ast.InfixExpr && node.right.op == .inc) {
(node.right is ast.InfixExpr && (node.right as ast.InfixExpr).op == .inc) {
c.warn('`++` and `--` are statements, not expressions', node.pos) c.warn('`++` and `--` are statements, not expressions', node.pos)
} }
*/ */

View File

@ -701,8 +701,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
// var = &auto_heap_var // var = &auto_heap_var
old_is_auto_heap := g.is_option_auto_heap old_is_auto_heap := g.is_option_auto_heap
g.is_option_auto_heap = val_type.has_flag(.option) && val is ast.PrefixExpr g.is_option_auto_heap = val_type.has_flag(.option) && val is ast.PrefixExpr
&& val.right is ast.Ident && val.right is ast.Ident && (val.right as ast.Ident).is_auto_heap()
&& ((val as ast.PrefixExpr).right as ast.Ident).is_auto_heap()
defer { defer {
g.is_option_auto_heap = old_is_auto_heap g.is_option_auto_heap = old_is_auto_heap
} }

View File

@ -1899,7 +1899,7 @@ fn (mut g Gen) expr_with_tmp_var(expr ast.Expr, expr_typ ast.Type, ret_typ ast.T
} }
if ret_typ.has_flag(.option) { if ret_typ.has_flag(.option) {
if expr_typ.has_flag(.option) && expr in [ast.StructInit, ast.ArrayInit, ast.MapInit] { if expr_typ.has_flag(.option) && expr in [ast.StructInit, ast.ArrayInit, ast.MapInit] {
if expr is ast.StructInit && (expr as ast.StructInit).init_fields.len > 0 { if expr is ast.StructInit && expr.init_fields.len > 0 {
g.write('_option_ok(&(${styp}[]) { ') g.write('_option_ok(&(${styp}[]) { ')
} else { } else {
g.write('_option_none(&(${styp}[]) { ') g.write('_option_none(&(${styp}[]) { ')
@ -4195,13 +4195,13 @@ fn (mut g Gen) select_expr(node ast.SelectExpr) {
[inline] [inline]
pub fn (mut g Gen) is_generic_param_var(node ast.Expr) bool { pub fn (mut g Gen) is_generic_param_var(node ast.Expr) bool {
return node is ast.Ident && node.info is ast.IdentVar && node.obj is ast.Var return node is ast.Ident && node.info is ast.IdentVar && node.obj is ast.Var
&& ((node as ast.Ident).obj as ast.Var).ct_type_var == .generic_param && (node.obj as ast.Var).ct_type_var == .generic_param
} }
[inline] [inline]
pub fn (mut g Gen) is_comptime_var(node ast.Expr) bool { pub fn (mut g Gen) is_comptime_var(node ast.Expr) bool {
return node is ast.Ident && node.info is ast.IdentVar && node.obj is ast.Var return node is ast.Ident && node.info is ast.IdentVar && node.obj is ast.Var
&& ((node as ast.Ident).obj as ast.Var).ct_type_var != .no_comptime && (node.obj as ast.Var).ct_type_var != .no_comptime
} }
fn (mut g Gen) ident(node ast.Ident) { fn (mut g Gen) ident(node ast.Ident) {

View File

@ -70,9 +70,8 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
if (node.cond in [ast.Ident, ast.IntegerLiteral, ast.StringLiteral, ast.FloatLiteral] if (node.cond in [ast.Ident, ast.IntegerLiteral, ast.StringLiteral, ast.FloatLiteral]
&& (node.cond !is ast.Ident || (node.cond is ast.Ident && (node.cond !is ast.Ident || (node.cond is ast.Ident
&& node.cond.or_expr.kind == .absent))) || (node.cond is ast.SelectorExpr && node.cond.or_expr.kind == .absent))) || (node.cond is ast.SelectorExpr
&& node.cond.or_block.kind == .absent && node.cond.or_block.kind == .absent && (node.cond.expr !is ast.CallExpr
&& ((node.cond as ast.SelectorExpr).expr !is ast.CallExpr || (node.cond.expr as ast.CallExpr).or_block.kind == .absent)) {
|| ((node.cond as ast.SelectorExpr).expr as ast.CallExpr).or_block.kind == .absent)) {
cond_var = g.expr_string(node.cond) cond_var = g.expr_string(node.cond)
} else { } else {
line := if is_expr { line := if is_expr {