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

parser: fix attributes with optional or result types (#16401)

This commit is contained in:
Taegon Kim 2022-11-13 00:35:01 +09:00 committed by GitHub
parent 9a8602ff03
commit b54f9c2949
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 7 deletions

View File

@ -381,15 +381,23 @@ pub fn (mut p Parser) parse_type() ast.Type {
p.next()
is_result = true
}
if (is_optional || is_result) && (p.tok.line_nr > line_nr || p.tok.kind in [.comma, .rpar]) {
mut typ := ast.void_type
if is_optional {
typ = typ.set_flag(.optional)
} else if is_result {
typ = typ.set_flag(.result)
if is_optional || is_result {
// maybe the '[' is the start of the field attribute
is_required_field := p.inside_struct_field_decl && p.tok.kind == .lsbr
&& p.peek_tok.kind == .name && p.peek_tok.lit == 'required'
if p.tok.line_nr > line_nr || p.tok.kind in [.comma, .rpar] || is_required_field {
mut typ := ast.void_type
if is_optional {
typ = typ.set_flag(.optional)
} else if is_result {
typ = typ.set_flag(.result)
}
return typ
}
return typ
}
is_shared := p.tok.kind == .key_shared
is_atomic := p.tok.kind == .key_atomic
if is_shared {

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/struct_field_required_fn_optional_type.vv:6:7: error: field `Foo.foo` must be initialized
4 |
5 | fn main() {
6 | f := Foo{}
| ~~~~~
7 | println(f)
8 | }

View File

@ -0,0 +1,8 @@
struct Foo {
foo fn() ? [required]
}
fn main() {
f := Foo{}
println(f)
}

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/struct_field_required_fn_result_type.vv:6:7: error: field `Foo.foo` must be initialized
4 |
5 | fn main() {
6 | f := Foo{}
| ~~~~~
7 | println(f)
8 | }

View File

@ -0,0 +1,8 @@
struct Foo {
foo fn() ! [required]
}
fn main() {
f := Foo{}
println(f)
}