mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: allow mut var.$(field.name)
(#17911)
This commit is contained in:
parent
5c439b6621
commit
063f4023c5
@ -1973,6 +1973,7 @@ pub fn (expr Expr) is_lvalue() bool {
|
|||||||
SelectorExpr { return expr.expr.is_lvalue() }
|
SelectorExpr { return expr.expr.is_lvalue() }
|
||||||
ParExpr { return expr.expr.is_lvalue() } // for var := &{...(*pointer_var)}
|
ParExpr { return expr.expr.is_lvalue() } // for var := &{...(*pointer_var)}
|
||||||
PrefixExpr { return expr.right.is_lvalue() }
|
PrefixExpr { return expr.right.is_lvalue() }
|
||||||
|
ComptimeSelector { return expr.field_expr.is_lvalue() }
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
@ -466,7 +466,7 @@ 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
|
||||||
&& !left.is_blank_ident() && right.is_lvalue()
|
&& !left.is_blank_ident() && right.is_lvalue() && right !is ast.ComptimeSelector
|
||||||
&& (!right_type.is_ptr() || (right is ast.Ident && right.is_auto_deref_var())) {
|
&& (!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)',
|
||||||
|
33
vlib/v/tests/comptimeselector_mut_test.v
Normal file
33
vlib/v/tests/comptimeselector_mut_test.v
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
struct Struct {
|
||||||
|
mut:
|
||||||
|
a string
|
||||||
|
b int
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode_string[T](mut value T) {
|
||||||
|
value = 'gg'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode_string_int[T](mut value T) {
|
||||||
|
value = 123
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode[T]() T {
|
||||||
|
key := 'a'
|
||||||
|
mut result := T{}
|
||||||
|
$for field in T.fields {
|
||||||
|
$if field.typ is string {
|
||||||
|
decode_string(mut result.$(field.name))
|
||||||
|
} $else $if field.typ is int {
|
||||||
|
decode_string_int(mut result.$(field.name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
assert decode[Struct]() == Struct{
|
||||||
|
a: 'gg'
|
||||||
|
b: 123
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user