gen: fix comptime attrs stmts (#10925)

pull/10936/head
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 { for attr in sym.info.attrs {
g.writeln('/* attribute $i */ {') g.writeln('/* attribute $i */ {')
g.writeln('\t${node.val_var}.name = _SLIT("$attr.name");') 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}.has_arg = $attr.has_arg;')
g.writeln('\t${node.val_var}.arg = _SLIT("$attr.arg");') g.writeln('\t${node.val_var}.arg = _SLIT("$attr.arg");')
g.writeln('\t${node.val_var}.kind = AttributeKind__$attr.kind;') g.writeln('\t${node.val_var}.kind = AttributeKind__$attr.kind;')
g.stmts(node.stmts)
g.writeln('}') g.writeln('}')
} }
} }

View File

@ -1,12 +1,31 @@
[test: 'hello'] [name: 'abc']
[amount: 2]
[abc] [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() { fn test_attributes() {
$for attr in Test.attributes { $for attr in Abc.attributes {
if attr.has_arg { if attr.has_arg && attr.kind == .string {
assert attr.name == 'test' assert attr.name == 'name'
assert attr.arg == 'hello' assert attr.arg == 'abc'
} else if attr.has_arg && attr.kind == .number {
assert attr.name == 'amount'
assert attr.arg == '2'
} else { } else {
assert attr.name == 'abc' assert attr.name == 'abc'
} }