From e5b63bf265f72de298f31b5614df63c5811135bb Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 26 Feb 2026 10:25:51 +0300 Subject: [PATCH] parser: fix wrong error when declaring attribute after incomplete variable declaration (fixes #23396) --- vlib/v/parser/parser.v | 20 +++++++++++++++++++ ...valid_struct_decl_with_attr_script_err.out | 12 +++++++++++ ...nvalid_struct_decl_with_attr_script_err.vv | 6 ++++++ 3 files changed, 38 insertions(+) create mode 100644 vlib/v/parser/tests/invalid_struct_decl_with_attr_script_err.out create mode 100644 vlib/v/parser/tests/invalid_struct_decl_with_attr_script_err.vv diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 640a97e20..690532659 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1030,6 +1030,26 @@ fn (mut p Parser) stmt(is_top_level bool) ast.Stmt { } } } + .at, .lsbr { + is_stmt_attr := if p.tok.kind == .at { + p.peek_tok.kind == .lsbr + } else { + p.is_attributes() + } + if p.script_mode && is_stmt_attr { + attr_pos := p.tok.pos() + p.attributes() + if token.is_decl(p.tok.kind) { + stmt := p.stmt(is_top_level) + p.attrs = [] + return stmt + } + p.attrs = [] + return p.error_with_pos('attributes can only be used before declarations', + attr_pos) + } + return p.parse_multi_expr(is_top_level) + } .name { if p.peek_tok.kind == .name && p.tok.lit == 'sql' { return p.sql_stmt() diff --git a/vlib/v/parser/tests/invalid_struct_decl_with_attr_script_err.out b/vlib/v/parser/tests/invalid_struct_decl_with_attr_script_err.out new file mode 100644 index 000000000..16cd687cb --- /dev/null +++ b/vlib/v/parser/tests/invalid_struct_decl_with_attr_script_err.out @@ -0,0 +1,12 @@ +vlib/v/parser/tests/invalid_struct_decl_with_attr_script_err.vv:1:1: notice: script mode started here + 1 | test_var = 'Test' + | ~~~~~~~~ + 2 | + 3 | @[table: 'users'] +vlib/v/parser/tests/invalid_struct_decl_with_attr_script_err.vv:4:8: error: all definitions must occur before code in script mode + 2 | + 3 | @[table: 'users'] + 4 | struct User { + | ~~~~ + 5 | name string + 6 | } diff --git a/vlib/v/parser/tests/invalid_struct_decl_with_attr_script_err.vv b/vlib/v/parser/tests/invalid_struct_decl_with_attr_script_err.vv new file mode 100644 index 000000000..1c5fd0551 --- /dev/null +++ b/vlib/v/parser/tests/invalid_struct_decl_with_attr_script_err.vv @@ -0,0 +1,6 @@ +test_var = 'Test' + +@[table: 'users'] +struct User { + name string +} -- 2.39.5