1
0
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:
Felipe Pena 2023-04-10 04:37:02 -03:00 committed by GitHub
parent 5c439b6621
commit 063f4023c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 1 deletions

View File

@ -1973,6 +1973,7 @@ pub fn (expr Expr) is_lvalue() bool {
SelectorExpr { return expr.expr.is_lvalue() }
ParExpr { return expr.expr.is_lvalue() } // for var := &{...(*pointer_var)}
PrefixExpr { return expr.right.is_lvalue() }
ComptimeSelector { return expr.field_expr.is_lvalue() }
else {}
}
return false

View File

@ -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
&& !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())) {
// Do not allow `a = b`
c.error('cannot copy map: call `move` or `clone` method (or use a reference)',

View 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
}
}