From 1397d532c62c9f63010a9d0f303e275fa4ea3d6f Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 17 Sep 2019 13:09:58 +0300 Subject: [PATCH] compiler: check if mutable receivers are modified --- compiler/cgen.v | 8 ++++---- compiler/comptime.v | 3 --- compiler/fn.v | 3 ++- compiler/live.v | 4 ++-- compiler/parser.v | 2 +- compiler/repl.v | 2 +- compiler/scanner.v | 7 +++++-- compiler/table.v | 4 ++-- vlib/builtin/array_test.v | 2 -- 9 files changed, 17 insertions(+), 18 deletions(-) diff --git a/compiler/cgen.v b/compiler/cgen.v index 2146c5ff6d..ed115f76c3 100644 --- a/compiler/cgen.v +++ b/compiler/cgen.v @@ -120,7 +120,7 @@ fn (g mut CGen) end_tmp() string { return res } -fn (g mut CGen) add_placeholder() int { +fn (g &CGen) add_placeholder() int { if g.is_tmp { return g.tmp_line.len } @@ -176,7 +176,7 @@ fn (g mut CGen) register_thread_fn(wrapper_name, wrapper_text, struct_text strin g.thread_args << wrapper_text } -fn (c mut V) prof_counters() string { +fn (v &V) prof_counters() string { mut res := []string // Global fns //for f in c.table.fns { @@ -196,7 +196,7 @@ fn (c mut V) prof_counters() string { return res.join(';\n') } -fn (p mut Parser) print_prof_counters() string { +fn (p &Parser) print_prof_counters() string { mut res := []string // Global fns //for f in p.table.fns { @@ -296,7 +296,7 @@ fn platform_postfix_to_ifdefguard(name string) string { // C struct definitions, ordered // Sort the types, make sure types that are referenced by other types // are added before them. -fn (v mut V) type_definitions() string { +fn (v &V) type_definitions() string { mut types := []Type // structs that need to be sorted mut builtin_types := []Type // builtin types // builtin types need to be on top diff --git a/compiler/comptime.v b/compiler/comptime.v index 4ed72289b9..2d7937b1f7 100644 --- a/compiler/comptime.v +++ b/compiler/comptime.v @@ -263,9 +263,6 @@ return strings__Builder_str(sb); } ' } -fn (p mut Parser) parse_t() { - -} diff --git a/compiler/fn.v b/compiler/fn.v index 6f47078308..8981e41cd4 100644 --- a/compiler/fn.v +++ b/compiler/fn.v @@ -510,7 +510,8 @@ fn (p mut Parser) check_unused_variables() { p.scanner.line_nr = var.line_nr - 1 p.error('`$var.name` declared and not used') } - if !var.is_changed && var.is_mut && !p.pref.is_repl && !var.is_arg && !p.pref.translated && var.name != '_' { + if !var.is_changed && var.is_mut && !p.pref.is_repl && + !p.pref.translated && var.name != '_' { p.scanner.line_nr = var.line_nr - 1 p.error('`$var.name` is declared as mutable, but it was never changed') } diff --git a/compiler/live.v b/compiler/live.v index 9fd0f3edd8..84e31d1138 100644 --- a/compiler/live.v +++ b/compiler/live.v @@ -2,7 +2,7 @@ module main import os -fn (v mut V) generate_hotcode_reloading_compiler_flags() []string { +fn (v &V) generate_hotcode_reloading_compiler_flags() []string { mut a := []string if v.pref.is_live || v.pref.is_so { // See 'man dlopen', and test running a GUI program compiled with -live @@ -16,7 +16,7 @@ fn (v mut V) generate_hotcode_reloading_compiler_flags() []string { return a } -fn (v mut V) generate_hotcode_reloading_declarations() { +fn (v &V) generate_hotcode_reloading_declarations() { mut cgen := v.cgen if v.os != .windows && v.os != .msvc { if v.pref.is_so { diff --git a/compiler/parser.v b/compiler/parser.v index 2129467050..6838eacadb 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -770,7 +770,7 @@ if p.scanner.line_comment != '' { } } -fn (p mut Parser) warn(s string) { +fn (p &Parser) warn(s string) { println('warning: $p.scanner.file_path:${p.scanner.line_nr+1}: $s') } diff --git a/compiler/repl.v b/compiler/repl.v index 33db8ef4aa..a6c21c48e3 100644 --- a/compiler/repl.v +++ b/compiler/repl.v @@ -41,7 +41,7 @@ fn (r mut Repl) checks(line string) bool { return r.in_func || (was_indent && r.indent <= 0) || r.indent > 0 } -fn (r mut Repl) function_call(line string) bool { +fn (r &Repl) function_call(line string) bool { for function in r.functions_name { if line.starts_with(function) { return true diff --git a/compiler/scanner.v b/compiler/scanner.v index 30c86fc0e6..f8decb967d 100644 --- a/compiler/scanner.v +++ b/compiler/scanner.v @@ -752,6 +752,9 @@ fn (s mut Scanner) ident_char() string { s.error('invalid character literal (more than one character: $len)') } } + if c == '\\`' { + return '`' + } // Escapes a `'` character return if c == '\'' { '\\' + c } else { c } } @@ -776,7 +779,7 @@ fn (s mut Scanner) peek() Token { return tok } -fn (s mut Scanner) expect(want string, start_pos int) bool { +fn (s &Scanner) expect(want string, start_pos int) bool { end_pos := start_pos + want.len if start_pos < 0 || start_pos >= s.text.len { return false @@ -825,7 +828,7 @@ fn is_nl(c byte) bool { return c == `\r` || c == `\n` } -fn (s mut Scanner) get_opening_bracket() int { +fn (s &Scanner) get_opening_bracket() int { mut pos := s.pos mut parentheses := 0 mut inside_string := false diff --git a/compiler/table.v b/compiler/table.v index 8b723b9c82..fb596731f2 100644 --- a/compiler/table.v +++ b/compiler/table.v @@ -236,7 +236,7 @@ fn new_table(obfuscate bool) &Table { } // If `name` is a reserved C keyword, returns `v_name` instead. -fn (t mut Table) var_cgen_name(name string) string { +fn (t &Table) var_cgen_name(name string) string { if name in CReserved { return 'v_$name' } @@ -707,7 +707,7 @@ fn (t mut Table) register_generic_fn(fn_name string) { t.generic_fns << GenTable{fn_name, []string} } -fn (t mut Table) fn_gen_types(fn_name string) []string { +fn (t &Table) fn_gen_types(fn_name string) []string { for _, f in t.generic_fns { if f.fn_name == fn_name { return f.types diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index a2a551967d..c1b277b9b2 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -166,7 +166,6 @@ const ( ) fn test_fixed() { - /* mut nums := [4]int assert nums[0] == 0 assert nums[1] == 0 @@ -177,7 +176,6 @@ fn test_fixed() { /////// nums2 := [N]int assert nums2[N - 1] == 0 - */ } fn modify (numbers mut []int) {