From 35bbb546a2e82fd51a0a5c4a549ee66b2764637f Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 27 Sep 2025 11:19:37 -0300 Subject: [PATCH] cgen: fix match codegen with option (fix #25394) (#25399) --- vlib/v/gen/c/match.v | 6 ++- vlib/v/tests/options/option_case_test.v | 52 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/options/option_case_test.v diff --git a/vlib/v/gen/c/match.v b/vlib/v/gen/c/match.v index b11284e54..7abd1de17 100644 --- a/vlib/v/gen/c/match.v +++ b/vlib/v/gen/c/match.v @@ -435,6 +435,11 @@ fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var str mut has_goto := false for j, branch in node.branches { is_last := j == node.branches.len - 1 + reset_if = branch.exprs.any(g.match_must_reset_if(it)) + if reset_if { + g.writeln('') + g.set_current_pos_as_last_stmt_pos() + } if branch.is_else || (use_ternary && is_last) { if node.branches.len > 1 { if use_ternary { @@ -469,7 +474,6 @@ fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var str g.write_v_source_line_info(branch) g.write('if (') } - reset_if = branch.exprs.any(g.match_must_reset_if(it)) for i, expr in branch.exprs { if i > 0 { g.write(' || ') diff --git a/vlib/v/tests/options/option_case_test.v b/vlib/v/tests/options/option_case_test.v new file mode 100644 index 000000000..80d604b74 --- /dev/null +++ b/vlib/v/tests/options/option_case_test.v @@ -0,0 +1,52 @@ +module main + +type SecondTokenizerValue = []rune | Keyword + +enum AdvancedTokenType { + identifier + keyword + newline +} + +struct SecondTokenizerToken { + type AdvancedTokenType + value ?SecondTokenizerValue +} + +enum Keyword { + module + import +} + +fn test_main() { + mut tokens := []SecondTokenizerToken{} + tokens << SecondTokenizerToken{ + type: AdvancedTokenType.keyword + value: Keyword.module + } + build_ast(tokens) or { assert err.msg() == 'Handling module' } +} + +struct RootAST { +mut: + module []rune +} + +fn build_ast(tokens []SecondTokenizerToken) !RootAST { + for _ in 0 .. 1 { + token := tokens[0] + match true { + false { + return error('Expected `module` keyword at the start of the file, but got `${token.type}`') + } + token.type == .keyword && token.value == ?SecondTokenizerValue(Keyword.module) { + return error('Handling module') + } + else { + continue + } + } + } + + return error('Not implemented') +} -- 2.39.5