From 3d907caa3fde44cd623ff312d572c22a21826e1b Mon Sep 17 00:00:00 2001 From: Louis Schmieder Date: Fri, 23 Jul 2021 22:24:56 +0200 Subject: [PATCH] gen: fix comptime attrs stmts (#10925) --- vlib/v/gen/c/comptime.v | 3 +- .../tests/comptime_attribute_selector_test.v | 31 +++++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 78ed3ff44c..ee45f91262 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -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('}') } } diff --git a/vlib/v/tests/comptime_attribute_selector_test.v b/vlib/v/tests/comptime_attribute_selector_test.v index 3b60d231e0..63cf1d0eea 100644 --- a/vlib/v/tests/comptime_attribute_selector_test.v +++ b/vlib/v/tests/comptime_attribute_selector_test.v @@ -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' }