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

checker: minor cleanup in assign_stmt() (#14107)

This commit is contained in:
yuyi 2022-04-21 02:03:41 +08:00 committed by GitHub
parent 0374f021c5
commit 0260c2a552
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -275,19 +275,11 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
if !c.inside_unsafe && !c.pref.translated && !c.file.is_translated { if !c.inside_unsafe && !c.pref.translated && !c.file.is_translated {
c.error('modifying variables via dereferencing can only be done in `unsafe` blocks', c.error('modifying variables via dereferencing can only be done in `unsafe` blocks',
node.pos) node.pos)
} else { } else if mut left.right is ast.Ident {
// mark `p` in `*p = val` as used: // mark `p` in `*p = val` as used:
match mut left.right { if mut left.right.obj is ast.Var {
ast.Ident {
match mut left.right.obj {
ast.Var {
left.right.obj.is_used = true left.right.obj.is_used = true
} }
else {}
}
}
else {}
}
} }
} }
if is_decl { if is_decl {
@ -330,7 +322,7 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
left_sym := c.table.sym(left_type_unwrapped) left_sym := c.table.sym(left_type_unwrapped)
right_sym := c.table.sym(right_type_unwrapped) right_sym := c.table.sym(right_type_unwrapped)
if left_sym.kind == .array && !c.inside_unsafe && node.op in [.assign, .decl_assign] if left_sym.kind == .array && !c.inside_unsafe && node.op in [.assign, .decl_assign]
&& right_sym.kind == .array && (left is ast.Ident && !left.is_blank_ident()) && right_sym.kind == .array && left is ast.Ident && !left.is_blank_ident()
&& right is ast.Ident { && right is ast.Ident {
// Do not allow `a = b`, only `a = b.clone()` // Do not allow `a = b`, only `a = b.clone()`
c.error('use `array2 $node.op.str() array1.clone()` instead of `array2 $node.op.str() array1` (or use `unsafe`)', c.error('use `array2 $node.op.str() array1.clone()` instead of `array2 $node.op.str() array1` (or use `unsafe`)',
@ -348,7 +340,7 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
} }
} }
if left_sym.kind == .array_fixed && !c.inside_unsafe && node.op in [.assign, .decl_assign] if left_sym.kind == .array_fixed && !c.inside_unsafe && node.op in [.assign, .decl_assign]
&& right_sym.kind == .array_fixed && (left is ast.Ident && !left.is_blank_ident()) && right_sym.kind == .array_fixed && left is ast.Ident && !left.is_blank_ident()
&& right is ast.Ident { && right is ast.Ident {
if right_sym.info is ast.ArrayFixed { if right_sym.info is ast.ArrayFixed {
if right_sym.info.elem_type.is_ptr() { if right_sym.info.elem_type.is_ptr() {
@ -358,8 +350,8 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
} }
} }
if left_sym.kind == .map && node.op in [.assign, .decl_assign] && right_sym.kind == .map if left_sym.kind == .map && node.op in [.assign, .decl_assign] && right_sym.kind == .map
&& ((right is ast.Ident && right.is_auto_deref_var()) && !left.is_blank_ident() && right.is_lvalue()
|| !right_type.is_ptr()) && !left.is_blank_ident() && right.is_lvalue() { && (!right_type.is_ptr() || (right is ast.Ident && right.is_auto_deref_var())) {
// Do not allow `a = b` // Do not allow `a = b`
c.error('cannot copy map: call `move` or `clone` method (or use a reference)', c.error('cannot copy map: call `move` or `clone` method (or use a reference)',
right.pos()) right.pos())
@ -375,8 +367,8 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
c.error('cannot assign to `$left`: ' + c.error('cannot assign to `$left`: ' +
c.expected_msg(right_type_unwrapped, left_type_unwrapped), right.pos()) c.expected_msg(right_type_unwrapped, left_type_unwrapped), right.pos())
} }
if (right is ast.StructInit || !right_is_ptr) && !(right_sym.is_number() if !right_sym.is_number() && !left_type.has_flag(.shared_f)
|| left_type.has_flag(.shared_f)) { && (right is ast.StructInit || !right_is_ptr) {
left_name := c.table.type_to_str(left_type_unwrapped) left_name := c.table.type_to_str(left_type_unwrapped)
mut rtype := right_type_unwrapped mut rtype := right_type_unwrapped
if rtype.is_ptr() { if rtype.is_ptr() {
@ -487,8 +479,8 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
else {} else {}
} }
if node.op in [.plus_assign, .minus_assign, .mod_assign, .mult_assign, .div_assign] if node.op in [.plus_assign, .minus_assign, .mod_assign, .mult_assign, .div_assign]
&& ((left_sym.kind == .struct_ && right_sym.kind == .struct_) && (left_sym.kind == .alias || (left_sym.kind == .struct_
|| left_sym.kind == .alias) { && right_sym.kind == .struct_)) {
left_name := c.table.type_to_str(left_type_unwrapped) left_name := c.table.type_to_str(left_type_unwrapped)
right_name := c.table.type_to_str(right_type_unwrapped) right_name := c.table.type_to_str(right_type_unwrapped)
parent_sym := c.table.final_sym(left_type_unwrapped) parent_sym := c.table.final_sym(left_type_unwrapped)
@ -563,7 +555,7 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
is_shared = left_first.info.share == .shared_t is_shared = left_first.info.share == .shared_t
} }
old_inside_ref_lit := c.inside_ref_lit old_inside_ref_lit := c.inside_ref_lit
c.inside_ref_lit = (c.inside_ref_lit || right_node.op == .amp || is_shared) c.inside_ref_lit = c.inside_ref_lit || right_node.op == .amp || is_shared
c.expr(right_node.right) c.expr(right_node.right)
c.inside_ref_lit = old_inside_ref_lit c.inside_ref_lit = old_inside_ref_lit
if right_node.op == .amp { if right_node.op == .amp {