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

parser: improve anon fn pos (#8210)

This commit is contained in:
zakuro 2021-01-21 19:01:40 +09:00 committed by GitHub
parent d97543605b
commit 0c249fa040
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 11 additions and 12 deletions

View File

@ -1,6 +1,6 @@
vlib/v/checker/tests/array_filter_anon_fn_err_a.vv:2:24: error: function needs exactly 1 argument
1 | fn main() {
2 | a := [1,2,3,4].filter(fn(a int, b int) bool { return a > 0 })
| ~~
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 | println(a)
4 | }

View File

@ -1,6 +1,6 @@
vlib/v/checker/tests/array_filter_anon_fn_err_b.vv:2:24: error: type mismatch, should use `fn(a int) bool {...}`
1 | fn main() {
2 | a := [1,2,3,4].filter(fn(a string) bool { return a.len > 0 })
| ~~
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 | println(a)
4 | }

View File

@ -1,6 +1,6 @@
vlib/v/checker/tests/array_map_anon_fn_err_a.vv:2:21: error: function needs exactly 1 argument
1 | fn main() {
2 | a := [1,2,3,4].map(fn(a int, b int) int {return a + b})
| ~~
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 | println(a)
4 | }

View File

@ -1,6 +1,6 @@
vlib/v/checker/tests/array_map_anon_fn_err_b.vv:2:21: error: type mismatch, should use `fn(a int) T {...}`
1 | fn main() {
2 | a := [1,2,3,4].map(fn(a string) string { return a })
| ~~
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 | println(a)
4 | }

View File

@ -1,6 +1,6 @@
vlib/v/checker/tests/array_map_anon_fn_err_c.vv:2:21: error: type mismatch, should use `fn(a int) T {...}`
1 | fn main() {
2 | a := [1,2,3,4].map(fn(a string) {})
| ~~
| ~~~~~~~~~~~~~~~
3 | println(a)
4 | }

View File

@ -14,4 +14,4 @@ vlib/v/checker/tests/fn_var.vv:5:5: error: cannot assign to `f`: expected `fn (i
3 | mut p := &f
4 | p = &[f]
5 | f = fn(mut a []int) {}
| ~~
| ~~~~~~~~~~~~~~~~~~

View File

@ -439,7 +439,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
}
fn (mut p Parser) anon_fn() ast.AnonFn {
mut pos := p.tok.position()
pos := p.tok.position()
p.check(.key_fn)
if p.pref.is_script && p.tok.kind == .name {
p.error_with_pos('function declarations in script mode should be before all script statements',
@ -491,7 +491,6 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
idx := p.table.find_or_register_fn_type(p.mod, func, true, false)
typ := table.new_type(idx)
// name := p.table.get_type_name(typ)
pos.update_last_line(p.prev_tok.line_nr)
return ast.AnonFn{
decl: ast.FnDecl{
name: name
@ -503,7 +502,7 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
is_method: false
is_anon: true
no_body: no_body
pos: pos
pos: pos.extend(p.prev_tok.position())
file: p.file_name
scope: p.scope
}