From 1b09954622a09eb2072883738b89331e0bf77424 Mon Sep 17 00:00:00 2001 From: zakuro Date: Fri, 22 Jan 2021 16:38:37 +0900 Subject: [PATCH] parser: improve the position of mut receiver warning / error (#8240) --- vlib/v/checker/tests/mut_receiver.out | 14 ++++++++++++++ .../{mut_receiver_warning.vv => mut_receiver.vv} | 3 +++ vlib/v/checker/tests/mut_receiver_warning.out | 7 ------- vlib/v/parser/fn.v | 6 ++++-- 4 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 vlib/v/checker/tests/mut_receiver.out rename vlib/v/checker/tests/{mut_receiver_warning.vv => mut_receiver.vv} (71%) delete mode 100644 vlib/v/checker/tests/mut_receiver_warning.out diff --git a/vlib/v/checker/tests/mut_receiver.out b/vlib/v/checker/tests/mut_receiver.out new file mode 100644 index 0000000000..fb160a4f2c --- /dev/null +++ b/vlib/v/checker/tests/mut_receiver.out @@ -0,0 +1,14 @@ +vlib/v/checker/tests/mut_receiver.vv:5:4: warning: use `(mut f Foo)` instead of `(f mut Foo)` + 3 | name string + 4 | } + 5 | fn (f mut Foo) info() { + | ~~~~~~~~~~~ + 6 | f.name = 'foo' + 7 | } +vlib/v/checker/tests/mut_receiver.vv:8:4: error: use `(mut f Foo)` or `(f &Foo)` instead of `(mut f &Foo)` + 6 | f.name = 'foo' + 7 | } + 8 | fn (mut f &Foo) info2() { + | ~~~~~~~~~~~~ + 9 | f.name = 'foo' + 10 | } diff --git a/vlib/v/checker/tests/mut_receiver_warning.vv b/vlib/v/checker/tests/mut_receiver.vv similarity index 71% rename from vlib/v/checker/tests/mut_receiver_warning.vv rename to vlib/v/checker/tests/mut_receiver.vv index e01bb499e3..fae05df9ba 100644 --- a/vlib/v/checker/tests/mut_receiver_warning.vv +++ b/vlib/v/checker/tests/mut_receiver.vv @@ -5,6 +5,9 @@ mut: fn (f mut Foo) info() { f.name = 'foo' } +fn (mut f &Foo) info2() { + f.name = 'foo' +} fn main() { println('hello, world') } diff --git a/vlib/v/checker/tests/mut_receiver_warning.out b/vlib/v/checker/tests/mut_receiver_warning.out deleted file mode 100644 index 90aaf0eb80..0000000000 --- a/vlib/v/checker/tests/mut_receiver_warning.out +++ /dev/null @@ -1,7 +0,0 @@ -vlib/v/checker/tests/mut_receiver_warning.vv:5:7: warning: use `(mut f Foo)` instead of `(f mut Foo)` - 3 | name string - 4 | } - 5 | fn (f mut Foo) info() { - | ~~~ - 6 | f.name = 'foo' - 7 | } \ No newline at end of file diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 4e6407dfdc..17a14e1b75 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -195,6 +195,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl { mut rec_mut := false mut params := []table.Param{} if p.tok.kind == .lpar { + lpar_pos := p.tok.position() p.next() // ( is_method = true is_shared := p.tok.kind == .key_shared @@ -208,7 +209,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl { if !rec_mut { rec_mut = p.tok.kind == .key_mut if rec_mut { - p.warn_with_pos('use `(mut f Foo)` instead of `(f mut Foo)`', p.tok.position()) + p.warn_with_pos('use `(mut f Foo)` instead of `(f mut Foo)`', lpar_pos.extend(p.peek_tok2.position())) } } receiver_pos = rec_start_pos.extend(p.tok.position()) @@ -231,7 +232,8 @@ fn (mut p Parser) fn_decl() ast.FnDecl { } rec_type_pos = rec_type_pos.extend(p.prev_tok.position()) if is_amp && rec_mut { - p.error('use `(mut f Foo)` or `(f &Foo)` instead of `(mut f &Foo)`') + p.error_with_pos('use `(mut f Foo)` or `(f &Foo)` instead of `(mut f &Foo)`', + lpar_pos.extend(p.tok.position())) return ast.FnDecl{ scope: 0 }