Skip to content

comptime if does not work as expected #25123

Description

@kbkpbot

Describe the bug

It seems cgen has some bug when generate code for some comptime $if.

Reproduction Steps

some code extract from vlib/v/tests/comptime/comptime_if_expr_test.v

version.v

const version = 123
const disable_opt_features = true

// Note: the `unknown_fn()` calls are here on purpose, to make sure that anything
// that doesn't match a compile-time condition is not even parsed.
fn ct_expressions() {
        foo := version
        bar := foo
        $if bar == 123 {
                println('bar == 123')
                assert true
        }
        $if bar != 123 {
                unknown_fn()
        } $else $if bar != 124 {
                println('bar != 124')
                assert true
        } $else {
                unknown_fn()
        }
}

fn main() {
        ct_expressions()
}
v run version.v

Expected Behavior

output

bar == 123
bar != 124

Current Behavior

output

bar == 123

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.11 460b48a.4f8a3da

Environment details (OS name and version, etc.)

V full version V 0.4.11 460b48a.4f8a3da
OS linux, Ubuntu 24.04.3 LTS
Processor 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Memory 3.38GB/15.51GB
V executable /media/HD/github/kbkpbot/v/v
V last modified time 2025-08-17 01:46:38
V home dir OK, value: /media/HD/github/kbkpbot/v
VMODULES OK, value: /home/mars/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/mars/v/bug/comptime
Git version git version 2.43.0
V git status weekly.2025.17-589-g206fdf4a
.git/config present true
cc version cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
gcc version gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
clang version Ubuntu clang version 18.1.3 (1ubuntu1)
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version N/A
glibc version ldd (Ubuntu GLIBC 2.39-0ubuntu8.5) 2.39

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.ComptimeFeatures processed during compile time, like $if, $for, $env etcUnit: cgenBugs/feature requests, that are related to the default C generating backend.

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions