From ae2af4c36ddacddce9bc93bba9cf0ca9a668f5f1 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 13 Oct 2019 01:50:15 +0300 Subject: [PATCH] parser: a small immutable field fix --- compiler/parser.v | 7 ++----- compiler/tests/struct_test.v | 11 +++++++++++ vlib/os/os_nix.v | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/compiler/parser.v b/compiler/parser.v index e75ede9e54..7d775a9623 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -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 { diff --git a/compiler/tests/struct_test.v b/compiler/tests/struct_test.v index 6ad3f7d60e..75a9cf7e59 100644 --- a/compiler/tests/struct_test.v +++ b/compiler/tests/struct_test.v @@ -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' +} diff --git a/vlib/os/os_nix.v b/vlib/os/os_nix.v index 3c1e597764..ecfe769a22 100644 --- a/vlib/os/os_nix.v +++ b/vlib/os/os_nix.v @@ -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