Skip to content

cgen: C backend emits an empty assignment for Result propagation in a sum-type match nested in an if expression #27988

Description

@skandhas

Describe the bug

When a sum-type match block is nested inside an if expression, using the ! operator to propagate a Result type inside the match branches causes a C compilation error.

The V compiler successfully passes semantic checks. However, it looks like the generated C code emits an incomplete/empty assignment statement (e.g., _t4 = ;) for the temporary variables intended to capture the match branch results, resulting in a syntax error (error: expected expression before ';' token) from the underlying C compiler.

Reproduction Steps

struct First {}
struct Second {}

type Node = First | Second

fn lower_first(_ First) !int {
	return 1
}

fn lower_second(_ Second) !int {
	return 2
}

fn select_value(node ?Node) !int {
	result := if value := node {
		match value {
			First {
				lower_first(value)!
			}
			Second {
				lower_second(value)!
			}
		}
	} else {
		0
	}

	return result
}

fn main() {
	println(select_value(First{})!)
}

Expected Behavior

The code should compile successfully without any C compiler errors and output 1.

Current Behavior

warning: tcc compilation failed, falling back to gcc
================== C compilation error (from gcc): ==============
cc: C:\\Users\\skand\\AppData\\Local\\Temp\\v_0\\ISC65B~1.C: In function 'main__select_value':
cc: C:\\Users\\skand\\AppData\\Local\\Temp\\v_0\\ISC65B~1.C:4670:31: error: expected expression before ';' token
cc:  4670 |                         _t4 = ;
cc:       |                               ^
cc: C:\\Users\\skand\\AppData\\Local\\Temp\\v_0\\ISC65B~1.C:4681:31: error: expected expression before ';' token
cc:  4681 |                         _t4 = ;
cc:       |                               ^
cc: At top level:
cc: cc1.exe: note: unrecognized command-line option '-Wno-incompatible-function-pointer-types' may have been intended to silence earlier diagnostics
...
cc: cc1.exe: note: unrecognized command-line option '-Wno-incompatible-function-pointer-types' may have been intended to silence earlier diagnostics
(note: the original output was 26 lines long; it was truncated to its first 9 lines + the last line)
=================================================================
Try passing `-g` when compiling, to see a .v file:line information, that correlates more with the C error.
(Alternatively, pass `-show-c-output`, to print the full C error message).
================== C compiler bug report ==============
Sent C compiler bug report to https://bugs.vlang.io/bug-report.
Report id: bdc2946f-2b8d-41f2-8654-5a0138a1d980
Delete this report from the server with:
  curl -X DELETE "https://bugs.vlang.io/bug-report/bdc2946f-2b8d-41f2-8654-5a0138a1d980?token=75b3fc86-3610-4626-be1b-d5fba7cf1411"
V V 0.5.2 09062b4577f57aa3ca57adb528d751824d8f51a8.be51760, Windows/amd64, cc: gcc, build options: gc:boehm_full_opt skip_unused -d gcboehm -d gcboehm_full -d gcboehm_opt -d windows
Generated C lines sent from C:\Users\skand\AppData\Local\Temp\v_0\issue_2_sum_type_match_in_if.01KYRWQN0SKTBJ47VV4K8CYNPD.tmp.c:0:
  (no source lines available)
Corresponding V lines sent from C:\\Users\\skand\\AppData\\Local\\Temp\\v_0\\ISC65B~1.C:4670:
    4665 |                              _t6.is_error = true;
    4666 |                              _t6.err = _t5.err;
    4667 |                              return _t6;
    4668 |                      }
    4669 |
>   4670 |                      _t4 = ;
    4671 |              }
    4672 |              else if (value._typ == 150) {
    4673 | _result_int _t7 = main__lower_second((*value._main__Second));
    4674 |                      if (_t7.is_error) {
    4675 |                              _result_int _t8 = {0};
=======================================================
builder error:
==================
C error found while compiling generated C code.
This can be caused by invalid C interop code, C compiler flags, or a V compiler bug.
If your code is pure V and this still happens, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.5.2 be51760

Environment details (OS name and version, etc.)

|V full version      |V 0.5.2 09062b4577f57aa3ca57adb528d751824d8f51a8.be51760
|:-------------------|:-------------------
|OS                  |windows, Microsoft Windows 11 Home 26200 64-bit
|Processor           |16 cpus, 64bit, little endian, 12th Gen Intel(R) Core(TM) i5-1240P
|Memory              |17.57GB/31.58GB
|                    |
|V executable        |D:\sdks\v\v.exe
|V last modified time|2026-07-30 06:37:26
|                    |
|V home dir          |OK, value: D:\sdks\v
|VMODULES            |OK, value: C:\Users\skand\.vmodules
|VTMP                |OK, value: C:\Users\skand\AppData\Local\Temp\v_0
|Current working dir |OK, value: D:\workd\vtools\bug\vbug-repro
|                    |
|Git version         |git version 2.51.1
|V git status        |be517609
|.git/config present |true
|                    |
|cc version          |cc (Rev3, Built by MSYS2 project) 16.1.0
|gcc version         |gcc (Rev3, Built by MSYS2 project) 16.1.0
|clang version       |clang version 22.1.7 (https://github.com/msys2/MINGW-packages b8f306ad57e0c8f3f15aed0dc809d7e3e8e2eee9)
|msvc version        |N/A
|tcc version         |tcc version 0.9.28rc 2026-07-14_HEAD@d9d02c56* (x86_64 Windows)
|tcc git status      |thirdparty-windows-amd64 f7c7199b-dirty
|emcc version        |N/A
|glibc version       |ldd (cygwin) 3.6.9

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