From 63bc769ce69a29caa6b9bf6300c7acea3ed78123 Mon Sep 17 00:00:00 2001 From: Felix Ehlers Date: Wed, 7 Jan 2026 08:26:35 +0100 Subject: [PATCH] fmt: fix parser failing on duplicate methods in $if/$else blocks (fix #26271) (#26278) --- .../comptime_if_duplicate_method_keep.vv | 35 +++++++++++++++++++ vlib/v/parser/fn.v | 4 ++- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 vlib/v/fmt/tests/comptime_if_duplicate_method_keep.vv diff --git a/vlib/v/fmt/tests/comptime_if_duplicate_method_keep.vv b/vlib/v/fmt/tests/comptime_if_duplicate_method_keep.vv new file mode 100644 index 000000000..32cd4ceb7 --- /dev/null +++ b/vlib/v/fmt/tests/comptime_if_duplicate_method_keep.vv @@ -0,0 +1,35 @@ +// Test for issue #26271: v fmt fails on mutually exclusive $if blocks with duplicate method definitions +module main + +struct MyType { + value int +} + +$if foo ? { + pub fn (x MyType) str() string { + return 'foo mode' + } +} $else { + pub fn (x MyType) str() string { + return 'normal mode' + } +} + +$if bar ? { + fn (m MyType) debug() string { + return 'bar debug' + } +} $else $if baz ? { + fn (m MyType) debug() string { + return 'baz debug' + } +} $else { + fn (m MyType) debug() string { + return 'default debug' + } +} + +fn main() { + t := MyType{42} + println(t.str()) +} diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 4bbb4225e..9216f6287 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -658,7 +658,9 @@ fn (mut p Parser) fn_decl() ast.FnDecl { is_duplicate = !type_sym.info.has_method(name) } } - if is_duplicate { + // when formatting, methods in mutually exclusive $if/$else branches + // may appear as duplicates since all branches are parsed + if is_duplicate && !p.pref.is_fmt { if type_sym.kind == .enum && name in ['is_empty', 'has', 'all', 'set', 'set_all', 'clear', 'clear_all', 'toggle', 'zero', 'from'] { if enum_fn := type_sym.find_method(name) { -- 2.39.5