Skip to content

fmt: failing on mutually exclusive $if blocks with duplicate method definitions #26271

@fleximus

Description

@fleximus

Describe the bug

v fmt reports duplicate method error when the same method is defined in mutually exclusive $if/$else or $if flag ?/$if !flag ? blocks. The code compiles and runs correctly, but cannot be formatted.

Reproduction Steps

module main

$if foo ? {

pub fn (x int) str() string {
    return 'foo mode'
}

} $else {

pub fn (x int) str() string {
    return 'normal mode'
}

}

fn main() {
    println(42.str())
}

Steps to reproduce:

v run test.v        # works
v -d foo run test.v # works
v fmt test.v        # fails

Expected Behavior

v fmt should recognize mutually exclusive $if blocks and not report duplicate methods

Current Behavior

  test.v:10:16: error: duplicate method `str`
     8 | } $else {
     9 |
    10 | pub fn (x int) str() string {
       |                ~~~
    11 |     return 'normal mode'
    12 | }

Possible Solution

No response

Additional Information/Context

Impact: Cannot format files which use this pattern for conditional compilation.

V version

V 0.5.0 e3616f2

Environment details (OS name and version, etc.)

V full version: V 0.5.0 9a73766.e3616f2
OS: Linux

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions