parser: fix fn attributes empty error

pull/5364/head
yuyi 2020-06-13 00:27:05 +08:00 committed by GitHub
parent 0d1e5abc41
commit 90f07eb64a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 3 deletions

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/fn_attributes_empty_err.v:1:1: error: attributes cannot be empty
1 | [] fn tt() {
| ~~
2 | println('text')
3 | }

View File

@ -0,0 +1,6 @@
[] fn tt() {
println('text')
}
fn main() {
tt()
}

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/multiple_fn_attributes.v:2:1: error: multiple attributes detected vlib/v/checker/tests/multiple_fn_attributes.v:1:1: error: multiple attributes detected
1 | [inline;deprecated] 1 | [inline;deprecated]
| ~~~~~~~~~~~~~~~~~~~
2 | fn foo(name string) string {} 2 | fn foo(name string) string {}
| ~~

View File

@ -407,9 +407,14 @@ pub fn (mut p Parser) top_stmt() ast.Stmt {
} }
} }
.lsbr { .lsbr {
start_pos := p.tok.position()
attrs := p.attributes() attrs := p.attributes()
if attrs.len > 1 { if attrs.len > 1 {
p.error('multiple attributes detected') end_pos := p.tok.position()
p.error_with_pos('multiple attributes detected', start_pos.extend(end_pos))
} else if attrs.len == 0 {
end_pos := p.tok.position()
p.error_with_pos('attributes cannot be empty', start_pos.extend(end_pos))
} }
return attrs[0] return attrs[0]
} }