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

parser: add a better error message, for multiple attributes, used on the same struct field (#16954)

This commit is contained in:
Felipe Pena 2023-01-13 18:27:46 -03:00 committed by GitHub
parent 3832f076c5
commit 41b9e513ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 0 deletions

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/struct_multiple_attrs_test.vv:4:10: error: multiple attributes should be in the same [], with ; separators
2 |
3 | struct Test {
4 | a int [a][b]
| ~~
5 | }
6 |

View File

@ -0,0 +1,9 @@
module main
struct Test {
a int [a][b]
}
fn test_main() {
assert true
}

View File

@ -60,6 +60,7 @@ mut:
inside_generic_params bool // indicates if parsing between `<` and `>` of a method/function
inside_receiver_param bool // indicates if parsing the receiver parameter inside the first `(` and `)` of a method
inside_struct_field_decl bool
inside_struct_attr_decl bool
inside_map_init bool
or_is_handled bool // ignore `or` in this expression
builtin_mod bool // are we in the `builtin` module?
@ -1773,6 +1774,11 @@ fn (mut p Parser) attributes() {
p.error_with_pos('attributes cannot be empty', p.prev_tok.pos().extend(p.tok.pos()))
return
}
if p.inside_struct_attr_decl && p.tok.kind == .lsbr {
p.error_with_pos('multiple attributes should be in the same [], with ; separators',
p.prev_tok.pos().extend(p.tok.pos()))
return
}
}
fn (mut p Parser) parse_attr() ast.Attr {

View File

@ -268,6 +268,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
// Comments after type (same line)
comments << p.eat_comments()
if p.tok.kind == .lsbr {
p.inside_struct_attr_decl = true
// attrs are stored in `p.attrs`
p.attributes()
for fa in p.attrs {
@ -275,6 +276,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
is_field_deprecated = true
}
}
p.inside_struct_attr_decl = false
}
mut default_expr := ast.empty_expr
mut has_default_expr := false