From 104bc212813dee6afe2ac57a1cef25c921d869f8 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 21 Oct 2025 13:49:58 -0300 Subject: [PATCH] cgen: fix match codegen for option expr on case (fix #25545) (#25553) --- vlib/v/gen/c/match.v | 2 - .../v/tests/options/option_match_cases_test.v | 63 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/options/option_match_cases_test.v diff --git a/vlib/v/gen/c/match.v b/vlib/v/gen/c/match.v index be23181ce..a08d84f43 100644 --- a/vlib/v/gen/c/match.v +++ b/vlib/v/gen/c/match.v @@ -459,8 +459,6 @@ fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var str g.write_v_source_line_info(branch) if !reset_if { g.write('else ') - } else { - reset_if = false } } } diff --git a/vlib/v/tests/options/option_match_cases_test.v b/vlib/v/tests/options/option_match_cases_test.v new file mode 100644 index 000000000..a639e841e --- /dev/null +++ b/vlib/v/tests/options/option_match_cases_test.v @@ -0,0 +1,63 @@ +module main + +type SecondTokenizerValue = []rune | Keyword + +enum AdvancedTokenType { + identifier + keyword + newline +} + +struct SecondTokenizerToken { + type AdvancedTokenType + value ?SecondTokenizerValue +} + +enum Keyword { + module + import +} + +pub struct FileAST { +pub mut: + module []rune +} + +pub fn build_ast(tokens []SecondTokenizerToken) !FileAST { + mut file_ast := FileAST{} + mut i := -1 + + for i < tokens.len - 1 { + i++ + token := tokens[i] + match true { + file_ast.module.len == 0 && token.type == .keyword + && token.value == ?SecondTokenizerValue(Keyword.module) { + file_ast.module = [`a`] + } + file_ast.module.len == 0 { + 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('Multiple `module` declarations are not allowed, but got another one') + } + token.type == .keyword && token.value == ?SecondTokenizerValue(Keyword.import) { + dump('import') + } + token.type == .newline { + // ignore newlines + continue + } + else { + // TODO: turn this into an error after implementing all other AST nodes + continue + } + } + } + + return file_ast +} + +fn test_main() { + build_ast([]SecondTokenizerToken{}) or { panic(err) } +} -- 2.39.5