Skip to content

Commit

Permalink
comptime: fix missing bool AttributeKind.kind (#23159)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 14, 2024
1 parent 78389c8 commit b39cad2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion vlib/builtin/builtin.v
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ pub enum AttributeKind {
plain // [name]
string // ['name']
number // [123]
comptime_define // [if name]
bool // [true] || [false]
comptime_define // [if name]
}

pub struct VAttribute {
Expand Down
5 changes: 4 additions & 1 deletion vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -2045,7 +2045,7 @@ fn (mut p Parser) parse_attr(is_at bool) ast.Attr {
if p.tok.kind == .colon {
has_arg = true
p.next()
if p.tok.kind == .name || (p.tok.kind != .string && token.is_key(p.tok.lit)) { // `name: arg`
if p.tok.kind == .name { // `name: arg`
kind = .plain
arg = p.check_name()
} else if p.tok.kind == .number { // `name: 123`
Expand All @@ -2060,6 +2060,9 @@ fn (mut p Parser) parse_attr(is_at bool) ast.Attr {
kind = .bool
arg = p.tok.kind.str()
p.next()
} else if token.is_key(p.tok.lit) { // // `name: keyword`
kind = .plain
arg = p.check_name()
} else {
p.unexpected(additional_msg: 'an argument is expected after `:`')
}
Expand Down
14 changes: 13 additions & 1 deletion vlib/v/tests/comptime/comptime_attribute_selector_test.v
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@[foo: true]
@[name: 'abc']
@[amount: 2]
@[abc]
Expand Down Expand Up @@ -26,8 +27,19 @@ fn test_attributes() {
} else if attr.has_arg && attr.kind == .number {
assert attr.name == 'amount'
assert attr.arg == '2'
} else {
} else if attr.kind != .bool {
assert attr.name == 'abc'
}
}
}

fn test_attr_boolean() {
mut bool_fields := []string{}
$for attr in Abc.attributes {
if attr.kind == .bool {
bool_fields << attr.name
}
}
assert bool_fields.len == 1
assert bool_fields[0] == 'foo'
}

0 comments on commit b39cad2

Please sign in to comment.