v.parser: show better position for sort struct init warning (#10939)

pull/10944/head
zakuro 2021-07-25 02:41:59 +09:00 committed by GitHub
parent 3be8ef3b5a
commit 3979e5c5ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 3 deletions

View File

@ -0,0 +1,13 @@
vlib/v/checker/tests/struct_short_init_warning.vv:6:9: warning: short struct initalization is deprecated, use explicit struct name
4 |
5 | fn f(foo Foo) Foo {
6 | return {v: foo.v}
| ~~~~~~~~~~
7 | }
8 |
vlib/v/checker/tests/struct_short_init_warning.vv:10:4: warning: short struct initalization is deprecated, use explicit struct name
8 |
9 | fn main() {
10 | f({v: 10})
| ~~~~~~~
11 | }

View File

@ -0,0 +1,11 @@
struct Foo {
v int
}
fn f(foo Foo) Foo {
return {v: foo.v}
}
fn main() {
f({v: 10})
}

View File

@ -312,9 +312,9 @@ pub fn (mut p Parser) check_expr(precedence int) ?ast.Expr {
node = p.assoc()
} else if (p.tok.kind == .name && p.peek_tok.kind == .colon)
|| p.tok.kind in [.rcbr, .comment, .ellipsis] {
p.warn_with_pos('short struct initalization is deprecated, use explicit struct name',
p.prev_tok.position())
node = p.struct_init(true) // short_syntax: true
p.warn_with_pos('short struct initalization is deprecated, use explicit struct name',
node.position())
} else if p.tok.kind == .name {
p.next()
return p.error_with_pos('unexpected $p.tok, expecting `:` after struct field name',

View File

@ -329,7 +329,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
}
fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
first_pos := p.tok.position()
first_pos := (if short_syntax && p.prev_tok.kind == .lcbr { p.prev_tok } else { p.tok }).position()
typ := if short_syntax { ast.void_type } else { p.parse_type() }
p.expr_mod = ''
// sym := p.table.get_type_symbol(typ)