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:
parent
9a8602ff03
commit
b54f9c2949
@ -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 {
|
||||
|
@ -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 | }
|
@ -0,0 +1,8 @@
|
||||
struct Foo {
|
||||
foo fn() ? [required]
|
||||
}
|
||||
|
||||
fn main() {
|
||||
f := Foo{}
|
||||
println(f)
|
||||
}
|
@ -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 | }
|
@ -0,0 +1,8 @@
|
||||
struct Foo {
|
||||
foo fn() ! [required]
|
||||
}
|
||||
|
||||
fn main() {
|
||||
f := Foo{}
|
||||
println(f)
|
||||
}
|
Loading…
Reference in New Issue
Block a user