From 15896beacee0a7f23dcf14f1a7176e0a6d14540d Mon Sep 17 00:00:00 2001 From: spaceface Date: Mon, 1 Mar 2021 01:01:56 +0100 Subject: [PATCH] cgen: scope `$for` fields/methods independently (#9017) --- vlib/v/gen/c/comptime.v | 16 +++++++++------- vlib/v/tests/comptime_for_test.v | 4 +++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index bfd46d4f54..09fe309cd0 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -298,7 +298,8 @@ fn (mut g Gen) comp_if_cond(cond ast.Expr) bool { fn (mut g Gen) comp_for(node ast.CompFor) { sym := g.table.get_type_symbol(g.unwrap_generic(node.typ)) - g.writeln('{ // 2comptime: \$for $node.val_var in ${sym.name}($node.kind.str()) {') + g.writeln('/* \$for $node.val_var in ${sym.name}($node.kind.str()) */ {') + g.indent++ // vweb_result_type := table.new_type(g.table.find_type_idx('vweb.Result')) mut i := 0 // g.writeln('string method = _SLIT("");') @@ -307,7 +308,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) { methods_with_attrs := sym.methods.filter(it.attrs.len > 0) // methods with attrs second methods << methods_with_attrs if methods.len > 0 { - g.writeln('\tFunctionData $node.val_var = {0};') + g.writeln('FunctionData $node.val_var = {0};') } for method in methods { // sym.methods { /* @@ -316,7 +317,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) { } */ g.comp_for_method = method.name - g.writeln('\t// method $i') + g.writeln('/* method $i */ {') g.writeln('\t${node.val_var}.name = _SLIT("$method.name");') if method.attrs.len == 0 { g.writeln('\t${node.val_var}.attrs = __new_array_with_default(0, 0, sizeof(string), 0);') @@ -366,7 +367,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) { g.comptime_var_type_map['${node.val_var}.typ'] = styp g.stmts(node.stmts) i++ - g.writeln('') + g.writeln('}') for key, _ in g.comptime_var_type_map { if key.starts_with(node.val_var) { g.comptime_var_type_map.delete(key) @@ -385,7 +386,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) { for field in fields { g.comp_for_field_var = node.val_var g.comp_for_field_value = field - g.writeln('\t// field $i') + g.writeln('/* field $i */ {') g.writeln('\t${node.val_var}.name = _SLIT("$field.name");') if field.attrs.len == 0 { g.writeln('\t${node.val_var}.attrs = __new_array_with_default(0, 0, sizeof(string), 0);') @@ -404,12 +405,13 @@ fn (mut g Gen) comp_for(node ast.CompFor) { g.comptime_var_type_map['${node.val_var}.typ'] = styp g.stmts(node.stmts) i++ - g.writeln('') + g.writeln('}') } g.comptime_var_type_map.delete(node.val_var) } } - g.writeln('} // } comptime for') + g.indent-- + g.writeln('}// \$for') } fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) ?string { diff --git a/vlib/v/tests/comptime_for_test.v b/vlib/v/tests/comptime_for_test.v index b4d596147f..05d20f40ee 100644 --- a/vlib/v/tests/comptime_for_test.v +++ b/vlib/v/tests/comptime_for_test.v @@ -39,7 +39,9 @@ fn test_comptime_for() { println(@FN) methods := ['run', 'method2', 'int_method1', 'int_method2', 'string_arg'] $for method in App.methods { - println(' method: $method.name | ' + no_lines('$method')) + // ensure each method is scoped under a new block in the generated code + x := ' method: $method.name | ' + no_lines('$method') + println(x) assert method.name in methods } }