compiler: check if mutable receivers are modified

pull/2020/head
Alexander Medvednikov 2019-09-17 13:09:58 +03:00
parent 9c9e6290a6
commit 1397d532c6
9 changed files with 17 additions and 18 deletions

View File

@ -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

View File

@ -263,9 +263,6 @@ return strings__Builder_str(sb);
} '
}
fn (p mut Parser) parse_t() {
}

View File

@ -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')
}

View File

@ -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 {

View File

@ -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')
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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) {