v icon indicating copy to clipboard operation
v copied to clipboard

builder error when spawning receiver method on a generic struct

Open LouisBrunner opened this issue 2 years ago • 0 comments

Describe the bug

Builder fails when compiling C when trying to spawn a receiver method from a generic struct.

Expected Behavior

Program builds.

Current Behavior

/tmp/v_501/bug.8279956369860035445.tmp.c:1222:24: error: use of undeclared identifier 'T'
        void (*fn) (main__Gen[T]);
                              ^
/tmp/v_501/bug.8279956369860035445.tmp.c:2116:10: error: argument type 'main__Gen' (aka 'struct main__Gen') is incomplete
        arg->fn(arg->arg0);
                ^~~~~~~~~
/tmp/v_501/bug.8279956369860035445.tmp.c:76:16: note: forward declaration of 'struct main__Gen'
typedef struct main__Gen main__Gen;
               ^
/tmp/v_501/bug.8279956369860035445.tmp.c:2244:4: warning: expression result unused [-Wunused-value]
  (*(int*)_t1.data);
   ^~~~~~~~~~~~~~~
/tmp/v_501/bug.8279956369860035445.tmp.c:12463:16: error: use of undeclared identifier 'main__Gen_internal_T_int'; did you mean 'main__Gen_T_int_internal_T_int'?
        arg__t1->fn = main__Gen_internal_T_int;
                      ^~~~~~~~~~~~~~~~~~~~~~~~
                      main__Gen_T_int_internal_T_int
/tmp/v_501/bug.8279956369860035445.tmp.c:1912:6: note: 'main__Gen_T_int_internal_T_int' declared here
void main__Gen_T_int_internal_T_int(main__Gen_T_int g);
     ^
1 warning and 3 errors generated.

Reproduction Steps

Minimal example:

struct Gen[T] {
	data T
}

fn (g Gen[T]) process() {
	th := spawn g.internal()
	th.wait()
}

fn (g Gen[T]) internal() {
	println(g.data * g.data)
}

fn main() {
	g := Gen[int]{
		data: 5
	}
	g.process()
	g.internal()
}

Possible Solution

Issue can be bypassed by wrapping the method received in an anonymous function

struct Gen[T] {
	data T
}

fn (g Gen[T]) process() {
	th := spawn fn [g] [T]() {
		g.internal()
	}()
	th.wait()
}

fn (g Gen[T]) internal() {
	println(g.data * g.data)
}

fn main() {
	g := Gen[int]{
		data: 5
	}
	g.process()
	g.internal()
}

Additional Information/Context

No response

V version

V 0.3.3 9ad1c2f

Environment details (OS name and version, etc.)

V full version: V 0.3.3 ee4150f.9ad1c2f OS: macos, macOS, 13.0, 22A380 Processor: 8 cpus, 64bit, little endian, Apple M1

getwd: /Users/louis/Dev/Projects/vgrapher vexe: /Users/louis/Dev/v/v vexe mtime: 2023-03-13 19:00:06

vroot: OK, value: /Users/louis/Dev/v VMODULES: OK, value: /Users/louis/.vmodules VTMP: OK, value: /tmp/v_501

Git version: git version 2.38.1 Git vroot status: weekly.2023.10-46-g9ad1c2f9 .git/config present: true

CC version: Apple clang version 14.0.0 (clang-1400.0.29.202) thirdparty/tcc status: thirdparty-macos-arm64 a668e5a0

LouisBrunner avatar Mar 13 '23 19:03 LouisBrunner