parser: a small immutable field fix

pull/2318/head
Alexander Medvednikov 2019-10-13 01:50:15 +03:00
parent 343dced36d
commit ae2af4c36d
3 changed files with 14 additions and 6 deletions

View File

@ -2179,9 +2179,8 @@ fn (p mut Parser) dot(str_typ_ string, method_ph int) string {
next := p.peek()
modifying := next.is_assign() || next == .inc || next == .dec ||
(field.typ.starts_with('array_') && next == .left_shift)
is_vi := p.fileis('vid')
if !p.builtin_mod && !p.pref.translated && modifying && !is_vi
&& p.has_immutable_field {
if !p.builtin_mod && !p.pref.translated && modifying &&
p.has_immutable_field {
f := p.first_immutable_field
p.error_with_token_index('cannot modify immutable field `$f.name` (type `$f.parent_fn`)\n' +
'declare the field with `mut:`
@ -2190,8 +2189,6 @@ struct $f.parent_fn {
$f.name $f.typ
}
', fname_tidx)
}
if !p.builtin_mod && p.mod != typ.mod {
}
// Don't allow `arr.data`
if field.access_mod == .private && !p.builtin_mod && !p.pref.translated && p.mod != typ.mod {

View File

@ -107,3 +107,14 @@ fn test_reserved_keywords() {
assert rk_holder2.volatile == 11
assert rk_holder2.while == 0 //Zero value as not specified.
}
struct User2 {
mut:
name string
}
fn test_mutable_fields() {
mut u := User2{}
u.name = 'Peter'
assert u.name == 'Peter'
}

View File

@ -9,7 +9,7 @@ const (
fn init_os_args(argc int, argv &byteptr) []string {
mut args := []string
for i := 0; i < argc; i++ {
for i in 0 .. argc {
args << string(argv[i])
}
return args