From 20150352187388781b1f64476f508954a369e204 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 22 Sep 2025 16:25:13 -0300 Subject: [PATCH] cgen: fix match with option cast expr (fix #25342) (#25372) --- vlib/v/gen/c/match.v | 3 ++ .../options/option_match_case_cast_test.v | 49 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 vlib/v/tests/options/option_match_case_cast_test.v diff --git a/vlib/v/gen/c/match.v b/vlib/v/gen/c/match.v index 6d61ff66f..b11284e54 100644 --- a/vlib/v/gen/c/match.v +++ b/vlib/v/gen/c/match.v @@ -585,6 +585,9 @@ fn (mut g Gen) match_must_reset_if(node ast.Expr) bool { ast.CallExpr { node.or_block.kind != .absent } + ast.CastExpr { + node.typ.has_flag(.option) + } ast.InfixExpr { g.match_must_reset_if(node.left) || g.match_must_reset_if(node.right) } diff --git a/vlib/v/tests/options/option_match_case_cast_test.v b/vlib/v/tests/options/option_match_case_cast_test.v new file mode 100644 index 000000000..f4fc0ec93 --- /dev/null +++ b/vlib/v/tests/options/option_match_case_cast_test.v @@ -0,0 +1,49 @@ +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 { + module []rune +} + +fn build_ast(tokens []SecondTokenizerToken) !RootAST { + token := tokens[0] + match true { + token.type == .keyword && token.value == ?SecondTokenizerValue(Keyword.module) { + return error('Handling module') + } + token.type == .keyword && token.value == ?SecondTokenizerValue(Keyword.import) { + return error('Handling import') + } + else { + return error('else') + } + } + + return error('Not implemented') +} -- 2.39.5