Skip to content

Commit 63bc769

Browse files
authored
fmt: fix parser failing on duplicate methods in $if/$else blocks (fix #26271) (#26278)
1 parent 3bd3934 commit 63bc769

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Test for issue #26271: v fmt fails on mutually exclusive $if blocks with duplicate method definitions
2+
module main
3+
4+
struct MyType {
5+
value int
6+
}
7+
8+
$if foo ? {
9+
pub fn (x MyType) str() string {
10+
return 'foo mode'
11+
}
12+
} $else {
13+
pub fn (x MyType) str() string {
14+
return 'normal mode'
15+
}
16+
}
17+
18+
$if bar ? {
19+
fn (m MyType) debug() string {
20+
return 'bar debug'
21+
}
22+
} $else $if baz ? {
23+
fn (m MyType) debug() string {
24+
return 'baz debug'
25+
}
26+
} $else {
27+
fn (m MyType) debug() string {
28+
return 'default debug'
29+
}
30+
}
31+
32+
fn main() {
33+
t := MyType{42}
34+
println(t.str())
35+
}

vlib/v/parser/fn.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,9 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
658658
is_duplicate = !type_sym.info.has_method(name)
659659
}
660660
}
661-
if is_duplicate {
661+
// when formatting, methods in mutually exclusive $if/$else branches
662+
// may appear as duplicates since all branches are parsed
663+
if is_duplicate && !p.pref.is_fmt {
662664
if type_sym.kind == .enum
663665
&& name in ['is_empty', 'has', 'all', 'set', 'set_all', 'clear', 'clear_all', 'toggle', 'zero', 'from'] {
664666
if enum_fn := type_sym.find_method(name) {

0 commit comments

Comments
 (0)