mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: add mutability check for comptime assignments (#18354)
This commit is contained in:
parent
125921db66
commit
c06fd556e8
@ -642,6 +642,13 @@ fn (mut c Checker) fail_if_immutable(expr_ ast.Expr) (string, token.Pos) {
|
|||||||
return '', expr.pos
|
return '', expr.pos
|
||||||
}
|
}
|
||||||
ast.ComptimeSelector {
|
ast.ComptimeSelector {
|
||||||
|
if mut expr.left is ast.Ident {
|
||||||
|
if mut expr.left.obj is ast.Var {
|
||||||
|
if expr.left.obj.ct_type_var != .generic_param {
|
||||||
|
c.fail_if_immutable(expr.left)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return '', expr.pos
|
return '', expr.pos
|
||||||
}
|
}
|
||||||
ast.Ident {
|
ast.Ident {
|
||||||
|
7
vlib/v/checker/tests/comptime_assign_missing_mut_err.out
Normal file
7
vlib/v/checker/tests/comptime_assign_missing_mut_err.out
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
vlib/v/checker/tests/comptime_assign_missing_mut_err.vv:13:4: error: `res` is immutable, declare it with `mut` to make it mutable
|
||||||
|
11 | val := doc.value(field.name)
|
||||||
|
12 | $if field.typ is string {
|
||||||
|
13 | res.$(field.name) = val.string()
|
||||||
|
| ~~~
|
||||||
|
14 | }
|
||||||
|
15 | }
|
21
vlib/v/checker/tests/comptime_assign_missing_mut_err.vv
Normal file
21
vlib/v/checker/tests/comptime_assign_missing_mut_err.vv
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import toml
|
||||||
|
|
||||||
|
struct Person {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode[T](toml_str string) !T {
|
||||||
|
res := T{}
|
||||||
|
doc := toml.parse_text(toml_str)!.to_any()
|
||||||
|
$for field in T.fields {
|
||||||
|
val := doc.value(field.name)
|
||||||
|
$if field.typ is string {
|
||||||
|
res.$(field.name) = val.string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
p_str := 'name = "John"'
|
||||||
|
|
||||||
|
decode[Person](p_str)!
|
Loading…
Reference in New Issue
Block a user