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

yuyi 2022-04-21 02:03:41 +08:00 committed by Jef Roosens
parent 1e42538e22
commit 1a76b50004
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 12 additions and 20 deletions

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 {
c.error('modifying variables via dereferencing can only be done in `unsafe` blocks',
node.pos)
} else {
} else if mut left.right is ast.Ident {
// mark `p` in `*p = val` as used:
match mut left.right {
ast.Ident {
match mut left.right.obj {
ast.Var {
if mut left.right.obj is ast.Var {
left.right.obj.is_used = true
}
else {}
}
}
else {}
}
}
}
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)
right_sym := c.table.sym(right_type_unwrapped)
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 {
// 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`)',
@ -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]
&& 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 {
if right_sym.info is ast.ArrayFixed {
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
&& ((right is ast.Ident && right.is_auto_deref_var())
|| !right_type.is_ptr()) && !left.is_blank_ident() && right.is_lvalue() {
&& !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`
c.error('cannot copy map: call `move` or `clone` method (or use a reference)',
right.pos())
@ -375,8 +367,8 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
c.error('cannot assign to `$left`: ' +
c.expected_msg(right_type_unwrapped, left_type_unwrapped), right.pos())
}
if (right is ast.StructInit || !right_is_ptr) && !(right_sym.is_number()
|| left_type.has_flag(.shared_f)) {
if !right_sym.is_number() && !left_type.has_flag(.shared_f)
&& (right is ast.StructInit || !right_is_ptr) {
left_name := c.table.type_to_str(left_type_unwrapped)
mut rtype := right_type_unwrapped
if rtype.is_ptr() {
@ -487,8 +479,8 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
else {}
}
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 == .alias || (left_sym.kind == .struct_
&& right_sym.kind == .struct_)) {
left_name := c.table.type_to_str(left_type_unwrapped)
right_name := c.table.type_to_str(right_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
}
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.inside_ref_lit = old_inside_ref_lit
if right_node.op == .amp {