Skip to content

[bug] v3: false-positive "duplicate attribute" when a struct attribute shares a name with a field attribute (regression in edadb203929) #28804

Description

@Jengro777

Describe the bug

Since commit edadb20 ("parser: validate attribute declarations", 2026-09-19), the new compiler rejects valid code whenever a struct-level attribute has the same name as a field-level attribute of that struct.

The duplicate check added to parse_field_attrs_with_kinds_mode() also compares the current attribute against p.pending_decl_attrs. While a struct body is parsed, p.pending_decl_attrs still holds the attributes of the enclosing struct declaration, so a field attribute such as @[comment: ...] is falsely reported as a duplicate of the struct-level @[comment: ...].

This pattern is used in V's own tests (vlib/db/pg/pg_orm_test.v, vlib/db/mysql/mysql_orm_test.v), and v -check on them now fails.

Reproduction Steps

main.v:

module main

@[comment: 'struct comment']
@[table: 'foo']
pub struct Foo {
pub:
	id string @[comment: 'UUID'; primary; sql_type: 'CHAR(36)']
}

fn main() {}
$ v -new-compiler run main.v
main.v:7:14: error: duplicate attribute `comment`
    5 | pub struct Foo {
    6 | pub:
    7 |     id string @[comment: 'UUID'; primary; sql_type: 'CHAR(36)']
      |                 ~~~~~~~
    8 | }

The same happens with @[json: 'x'] on the struct and on a field, and with the default compiler (v run main.v). One error is emitted per matching field attribute.

Existing in-tree reproduction:

$ v -check vlib/db/pg/pg_orm_test.v
vlib/db/pg/pg_orm_test.v:47:22: error: duplicate attribute `comment`

TestCommentAttribute has @[comment: 'This is a table comment'] on the struct and @[comment: 'real user name'] on the name field. vlib/db/mysql/mysql_orm_test.v:43 fails the same way.

Expected Behavior

The code compiles. Attributes on a declaration and attributes on its fields are independent scopes; a struct-level @[comment: ...] must not conflict with a field-level @[comment: ...].

Current Behavior

False duplicate attribute errors stop compilation. In a real project this reaches the diagnostic limit:

model/schema_iam/iam_api_key.v:9:19: error: duplicate attribute `comment`
model/schema_iam/iam_api_key.v:10:19: error: duplicate attribute `comment`
...
error: too many errors; stopping after 100 diagnostics

v -old-compiler main.v (V1 fallback) compiles the repro fine; v and v -new-compiler fail, so this is a new-compiler regression.

Possible Solution

The p.pending_decl_attrs comparison is needed for duplicates across separate declaration-level @[...] groups (see vlib/v/parser/tests/fn_attributes_duplicate_multiple.vv), but it must not run while parsing a declaration's fields. Please make that cross-group check apply to declaration attributes only (e.g. pass an explicit flag from parse_pending_decl_attrs()), and add a regression test for a struct with same-named struct-level and field-level attributes.

The offending code, added by edadb20, is in vlib/v/parser/parser.v:3859 at bc640f2:

if piece_name.len > 0
	&& (attrs.any(it.all_before(':').trim_space() == piece_name)
		|| p.pending_decl_attrs.any(it.all_before(':').trim_space() == piece_name)) {
	p.record_diagnostic_span('duplicate attribute `${piece_name}`', piece_start, piece_end)
}

V version

V 0.5.2 bc640f2 (master, 2026-09-20)

Environment details (OS name and version, etc.)

Linux x86_64, Deepin 25, kernel 6.18.48-amd64-desktop-rolling, gcc 12.3.0.

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions