Skip to content

Commit b39cad2

Browse files
authored
comptime: fix missing bool AttributeKind.kind (#23159)
1 parent 78389c8 commit b39cad2

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

vlib/builtin/builtin.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ pub enum AttributeKind {
153153
plain // [name]
154154
string // ['name']
155155
number // [123]
156-
comptime_define // [if name]
156+
bool // [true] || [false]
157+
comptime_define // [if name]
157158
}
158159

159160
pub struct VAttribute {

vlib/v/parser/parser.v

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2045,7 +2045,7 @@ fn (mut p Parser) parse_attr(is_at bool) ast.Attr {
20452045
if p.tok.kind == .colon {
20462046
has_arg = true
20472047
p.next()
2048-
if p.tok.kind == .name || (p.tok.kind != .string && token.is_key(p.tok.lit)) { // `name: arg`
2048+
if p.tok.kind == .name { // `name: arg`
20492049
kind = .plain
20502050
arg = p.check_name()
20512051
} else if p.tok.kind == .number { // `name: 123`
@@ -2060,6 +2060,9 @@ fn (mut p Parser) parse_attr(is_at bool) ast.Attr {
20602060
kind = .bool
20612061
arg = p.tok.kind.str()
20622062
p.next()
2063+
} else if token.is_key(p.tok.lit) { // // `name: keyword`
2064+
kind = .plain
2065+
arg = p.check_name()
20632066
} else {
20642067
p.unexpected(additional_msg: 'an argument is expected after `:`')
20652068
}

vlib/v/tests/comptime/comptime_attribute_selector_test.v

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@[foo: true]
12
@[name: 'abc']
23
@[amount: 2]
34
@[abc]
@@ -26,8 +27,19 @@ fn test_attributes() {
2627
} else if attr.has_arg && attr.kind == .number {
2728
assert attr.name == 'amount'
2829
assert attr.arg == '2'
29-
} else {
30+
} else if attr.kind != .bool {
3031
assert attr.name == 'abc'
3132
}
3233
}
3334
}
35+
36+
fn test_attr_boolean() {
37+
mut bool_fields := []string{}
38+
$for attr in Abc.attributes {
39+
if attr.kind == .bool {
40+
bool_fields << attr.name
41+
}
42+
}
43+
assert bool_fields.len == 1
44+
assert bool_fields[0] == 'foo'
45+
}

0 commit comments

Comments
 (0)