1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

gen: fix comptime attrs stmts (#10925)

This commit is contained in:
Louis Schmieder 2021-07-23 22:24:56 +02:00 committed by GitHub
parent 1e5627e777
commit 3d907caa3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 8 deletions

View File

@ -519,12 +519,11 @@ fn (mut g Gen) comp_for(node ast.CompFor) {
}
for attr in sym.info.attrs {
g.writeln('/* attribute $i */ {')
g.writeln('\t${node.val_var}.name = _SLIT("$attr.name");')
g.writeln('\t${node.val_var}.has_arg = $attr.has_arg;')
g.writeln('\t${node.val_var}.arg = _SLIT("$attr.arg");')
g.writeln('\t${node.val_var}.kind = AttributeKind__$attr.kind;')
g.stmts(node.stmts)
g.writeln('}')
}
}

View File

@ -1,12 +1,31 @@
[test: 'hello']
[name: 'abc']
[amount: 2]
[abc]
struct Test {}
struct Abc {}
fn test_comp_for_attributes() {
mut res := ''
mut amount := 0
$for attr in Abc.attributes {
if attr.name == 'amount' && attr.has_arg && attr.kind == .number {
amount = attr.arg.int()
}
if attr.name == 'name' && attr.has_arg && attr.kind == .string {
res = attr.arg
}
}
res = res.repeat(amount)
assert res == 'abcabc'
}
fn test_attributes() {
$for attr in Test.attributes {
if attr.has_arg {
assert attr.name == 'test'
assert attr.arg == 'hello'
$for attr in Abc.attributes {
if attr.has_arg && attr.kind == .string {
assert attr.name == 'name'
assert attr.arg == 'abc'
} else if attr.has_arg && attr.kind == .number {
assert attr.name == 'amount'
assert attr.arg == '2'
} else {
assert attr.name == 'abc'
}