parser: improve the position of mut receiver warning / error (#8240)

pull/8092/head
zakuro 2021-01-22 16:38:37 +09:00 committed by GitHub
parent 925ffd76f4
commit 1b09954622
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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