1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: better immutability error messages

This commit is contained in:
Alexander Medvednikov 2019-08-08 06:55:08 +02:00
parent 1c38661ff7
commit 1470b3da11
2 changed files with 20 additions and 6 deletions

View File

@ -631,7 +631,7 @@ fn (p mut Parser) fn_call(f Fn, method_ph int, receiver_var, receiver_type strin
receiver := f.args.first() receiver := f.args.first()
if receiver.is_mut && !p.expr_var.is_mut { if receiver.is_mut && !p.expr_var.is_mut {
println('$method_call recv=$receiver.name recv_mut=$receiver.is_mut') println('$method_call recv=$receiver.name recv_mut=$receiver.is_mut')
p.error('`$p.expr_var.name` is immutable') p.error('`$p.expr_var.name` is immutable, declare it with `mut`')
} }
if !p.expr_var.is_changed { if !p.expr_var.is_changed {
p.cur_fn.mark_var_changed(p.expr_var) p.cur_fn.mark_var_changed(p.expr_var)

View File

@ -788,9 +788,9 @@ fn (p mut Parser) error(s string) {
// os.write_to_file('/var/tmp/lang.vars', q.J(p.table.vars)) // os.write_to_file('/var/tmp/lang.vars', q.J(p.table.vars))
os.write_file('fns.txt', p.table.debug_fns()) os.write_file('fns.txt', p.table.debug_fns())
} }
//if p.pref.is_verbose { if p.pref.is_verbose || p.pref.is_debug {
println('pass=$p.pass fn=`$p.cur_fn.name`') println('pass=$p.pass fn=`$p.cur_fn.name`\n')
//} }
p.cgen.save() p.cgen.save()
// V git pull hint // V git pull hint
cur_path := os.getwd() cur_path := os.getwd()
@ -1200,7 +1200,14 @@ fn (p mut Parser) assign_statement(v Var, ph int, is_map bool) {
tok := p.tok tok := p.tok
//if !v.is_mut && !v.is_arg && !p.pref.translated && !v.is_global{ //if !v.is_mut && !v.is_arg && !p.pref.translated && !v.is_global{
if !v.is_mut && !p.pref.translated && !v.is_global && !is_vid { if !v.is_mut && !p.pref.translated && !v.is_global && !is_vid {
p.error('`$v.name` is immutable') if v.is_arg {
if p.cur_fn.args.len > 0 && p.cur_fn.args[0].name == v.name {
println('make the receiver `$v.name` mutable:
fn ($v.name mut $v.typ) $p.cur_fn.name (...) {
')
}
}
p.error('`$v.name` is immutable.')
} }
if !v.is_changed { if !v.is_changed {
p.cur_fn.mark_var_changed(v) p.cur_fn.mark_var_changed(v)
@ -1766,7 +1773,14 @@ fn (p mut Parser) dot(str_typ string, method_ph int) string {
modifying := next.is_assign() || next == .inc || next == .dec modifying := next.is_assign() || next == .inc || next == .dec
is_vi := p.fileis('vid') is_vi := p.fileis('vid')
if !p.builtin_mod && !p.pref.translated && modifying && !field.is_mut && !is_vi { if !p.builtin_mod && !p.pref.translated && modifying && !field.is_mut && !is_vi {
p.error('cannot modify immutable field `$field_name` (type `$typ.name`)') p.error('cannot modify immutable field `$field_name` (type `$typ.name`)\n' +
'declare the field with `mut:`
struct $typ.name {
mut:
$field_name $field.typ
}
')
} }
if !p.builtin_mod && p.mod != typ.mod { if !p.builtin_mod && p.mod != typ.mod {
} }