Skip to content

C compilation error with generics and nested if expressions #23723

Description

@bbodi

Describe the bug

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/bug.01JM1Y22T3STYJDSY1ZAVTAY18.tmp.c:9296: error: expression expected before 'if'
=================================================================

Reproduction Steps

struct Message {
	prio bool
}

struct Throttler[T] {
	max_wait int
mut:
	wait_counter int
	low_prio_requests []T
	high_prio_requests []T
}

fn new_throttler[T](max_wait int) Throttler[T] {
	return Throttler[T] {
		max_wait, 0, []T{cap: 512}, []T{cap: 512}
	}
}

fn pop[T](mut t Throttler[T]) ?T {
	if t.wait_counter >= t.max_wait {
		return if t.high_prio_requests.len > 0 {
			t.high_prio_requests.pop()
		} else {
			t.wait_counter = 0
			if t.low_prio_requests.len > 0 {
				t.low_prio_requests.pop()
			} else {
				none
			}
		}
	} else {
		return none
	}
}

fn main() {
	c := chan Message{}
	mut throttler := new_throttler[Message](10)
	msg := pop(mut throttler) or {
		return
	}

}

Expected Behavior

It should compile

Current Behavior

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/bug.01JM1Y4BT1ZDYYPF0EE7S1HT25.tmp.c:9283: error: expression expected before 'if'
=================================================================

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.9 5be2fca

Environment details (OS name and version, etc.)

V full version V 0.4.9 5be2fca
OS linux, Ubuntu 24.10
Processor 16 cpus, 64bit, little endian, AMD Ryzen 7 8840U w/ Radeon 780M Graphics
Memory 18.25GB/22.78GB
V executable /home/sharp/dev/v/v
V last modified time 2025-02-13 12:21:16
V home dir OK, value: /home/sharp/dev/v
VMODULES OK, value: /home/sharp/.vmodules
VTMP OK, value: /tmp/v_1000
Git version git version 2.45.2
V git status weekly.2025.03-35-g5be2fcab-dirty (102 commit(s) behind V master)
.git/config present true
cc version cc (Ubuntu 14.2.0-4ubuntu2) 14.2.0
gcc version gcc (Ubuntu 14.2.0-4ubuntu2) 14.2.0
clang version Ubuntu clang version 19.1.1 (1ubuntu1)
tcc version tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
tcc git status thirdparty-linux-amd64 0134e9b9
emcc version emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.6 ()
glibc version ldd (Ubuntu GLIBC 2.40-1ubuntu3.1) 2.40

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 type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions