Skip to content

C error: Calling enum function with argument as Option of Sum when match is struct #24746

Description

@jorgeluismireles
V version: V 0.4.10 080684d, press to see full `v doctor` output
V full version V 0.4.10 7dc3889.080684d
OS linux, Ubuntu 22.04.5 LTS
Processor 4 cpus, 64bit, little endian, Intel(R) Core(TM) i3-4150 CPU @ 3.50GHz
Memory 0.29GB/3.7GB
V executable /home/jorge/v/v
V last modified time 2025-06-17 22:47:06
V home dir OK, value: /home/jorge/v
VMODULES OK, value: /home/jorge/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/jorge/bugs
Git version git version 2.34.1
V git status weekly.2025.16-311-g080684dd
.git/config present true
cc version cc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
gcc version gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
clang version Ubuntu clang version 14.0.0-1ubuntu1.1
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.5 ()
glibc version ldd (Ubuntu GLIBC 2.35-0ubuntu3.10) 2.35

What did you do?
./v -g -o vdbg cmd/v && ./vdbg 250617.v && 250617

type TxPayload = Can | u8

enum Tx {
	empty
	data
	can
}

fn (tx Tx) frame_bytes(payload ?TxPayload) ! []u8 {
	match tx {
		.empty {
			return [ u8(tx) ]
		}
		.data {
			if payload != none {
				if payload is u8 {
					return [ u8(tx), payload ]
				}		
			}
			return error('Invalid data')
		}
		.can {
			if payload != none {
				if payload is Can {
					return [ u8(tx), payload.net ] // fails with this line
				}
			}
			return error('Invalid can')
		}
	}
}

struct Can {
	net  u8
}

fn main()  {
	Tx.empty.frame_bytes(none)!
}

What did you see?

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/250617.01JY02HM2SSEHC2FVKR88KY03Z.tmp.c:5490: error: struct or union expected
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
builder error: 
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, 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 .

What did you expect to see?

Compile

-show-c-output message:

=================================================================================
======== Output of the C Compiler (cc) ========
/tmp/v_1000/250617.01JY02EK6BE8Y0K0W6NFJEA6N1.tmp.c: In function ‘main__Tx_frame_bytes’:
/tmp/v_1000/250617.01JY02EK6BE8Y0K0W6NFJEA6N1.tmp.c:5490:182: error: ‘((main__TxPayload *)&payload.data)-><U3428>._main__Can’ is a pointer; did you mean to use ‘->’?
 5490 |                                 _result_ok(&(Array_u8[]) { new_array_from_c_array_noscan(2, 2, sizeof(u8), _MOV((u8[2]){((u8)(tx)), *((*(main__TxPayload*)(payload.data))._main__Can).net})) }, (_result*)(&_t4), sizeof(Array_u8));
      |                                                                                                                                                                                      ^
      |                                                                                                                                                                                      ->
===============================================

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

Labels

BugThis tag is applied to issues which reports bugs.Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Unit: 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