Skip to content

C error when passing interface method to struct field #24810

Description

@vsm0

Describe the bug

A struct can have a field of type function:

struct Structure {
    action fn()
}

An interface can have a method:

interface Interfacer {
    id int
}
fn (i Interfacer) process() {
    println('proc #${id}')
}

But this causes a C error:

struct Process {
    id int
}
i := Interfacer(Process{22})
s := Structure{
    action: i.process
}

Reproduction Steps

Run the following program:

interface Doer{
	job string
}

fn (d Doer) do() {
	println('did:  ${d.job}')
}


struct  Expector {
	do_fn fn() @[required]
}

struct Actioner {
	job string
}

fn main() {
	a := &Actioner{"dier"}
	e := Expector{
		do_fn: Doer(a).do
	}
	e.do_fn()
}

Expected Behavior

Expected console output:

did: dier

Current Behavior

Instead, an error occurs, something like the following:

================== C compilation error (from cc): ==============
cc: /usr/tmp/v_10230/main.01JYX7BF26C2Q81F135BK1QYBA.tmp.c:1938:2: error: use of undeclared identifier 'main__Doer_name_table'
cc:  1938 |         main__Doer_name_table[a0->_typ]._method_do(a0);
cc:       |         ^
cc: 1 error generated.
================================================================

Possible Solution

Workaround by @spytheman :

do_fn: fn [a] () { Doer(a).do() }

Additional Information/Context

No response

V version

V 0.4.11 bd465b5

Environment details (OS name and version, etc.)

I ran this on a laptop, but had the same error running on my phone:

|V full version      |V 0.4.11 bd465b5254c10ac2ef2d214bc7845e5cebfef946
|:-------------------|:-------------------
|OS                  |termux, 4.19.191-g6f453d21fb7f-dirty, #1 SMP PREEMPT Fri Feb 23 18:16:46 CST 2024
|Processor           |8 cpus, 64bit, little endian
|Memory              |N/A
|                    |
|V executable        |/data/data/com.termux/files/home/tmp/v/v
|V last modified time|2025-06-28 12:46:57
|                    |
|V home dir          |OK, value: /data/data/com.termux/files/home/tmp/v
|VMODULES            |OK, value: /data/data/com.termux/files/home/.vmodules
|VTMP                |OK, value: /data/data/com.termux/files/usr/tmp/v_10230
|Current working dir |OK, value: /data/data/com.termux/files/home/tmp
|                    |
|Git version         |git version 2.50.0
|V git status        |bd465b52
|.git/config present |true
|                    |
|cc version          |clang version 20.1.7
|gcc version         |clang version 20.1.7
|clang version       |clang version 20.1.7
|tcc version         |N/A
|tcc git status      |thirdparty-unknown-unknown de82a130
|emcc version        |N/A
|glibc version       |N/A

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.Status: Has WorkaroundThis issue has workaround.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