comptime: fix missing bool AttributeKind.kind (#23159)

This commit is contained in:
Felipe Pena 2024-12-14 13:19:29 -03:00 committed by GitHub
parent 78389c8a45
commit b39cad2f24
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 3 deletions

View File

@ -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 {

View File

@ -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`
@ -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 `:`')
}

View File

@ -1,3 +1,4 @@
@[foo: true]
@[name: 'abc']
@[amount: 2]
@[abc]
@ -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'
}