Skip to content

cgen, coroutines: go expr handle references undeclared thread__tN C variable #27338

@medvednikov

Description

@medvednikov

Description

With -use-coroutines, a go expr whose handle is stored/used makes cgen reference a C variable thread__tN that it never declares, so the generated C fails to compile.

V version: V 0.5.1, current master (27f394507)
OS/Backend: reproduced on macOS arm64; originally reported on Linux/cc. Requires -use-coroutines.

Reproducer

struct App {
mut:
	worker thread
}

fn (a App) loop() {}

fn main() {
	mut a := App{}
	a.worker = go a.loop()
	a.worker.wait()
}

Compile with:

v -use-coroutines file.v

Current behavior

C compilation fails — V references an undeclared thread__tN:

error: use of undeclared identifier 'thread__t2'
 4112 | thread__t2;

The generated C for the coroutine handle is:

a.worker = /*go (coroutine) */ thread__t2;

The coroutine branch emits the photon_thread_create_and_migrate_to_work_pool(...) call but never declares the thread__tN handle variable it then reads.

Expected behavior

Compiles and runs (as the non-coroutine spawn path does).

Notes

  • Reported via the bugs.vlang.io crash reporter (original symbol thread__t4, from db.flush_thread = go db.flush_loop()).
  • Likely in vlib/v/gen/c/spawn_and_go.v: the is_go (coroutine) branch sets the handle expression to thread_${tmp} without emitting its declaration, unlike the regular spawn path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugs.vlang.ioReported via the bugs.vlang.io crash reporter

    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