v icon indicating copy to clipboard operation
v copied to clipboard

compiler bug when adding function in empty interface array

Open alaingilbert opened this issue 8 months ago • 2 comments

Describe the bug

Adding a function in an empty interface array crash the compiler.

Reproduction Steps

module main

fn add(a int, b int) int {
	return a + b
}

interface MyInterface {
}

fn main() {
	mut a := []MyInterface{}
	a << add
	println(a)
}

Expected Behavior

I'm not even sure if this code is supposed to make sense or not in V.

Current Behavior

% v main.v
================== C compilation error (from cc): ==============
cc: /tmp/v_502/main.01JWV7BHKYDVC4T05FF8Q0DJJV.tmp.c:849:3: error: unknown type name 'main__add'
cc:   849 |                 main__add* _main__add;
cc:       |                 ^
cc: /tmp/v_502/main.01JWV7BHKYDVC4T05FF8Q0DJJV.tmp.c:1691:69: error: unknown type name 'main__add'
cc:  1691 | static main__MyInterface I_main__add_to_Interface_main__MyInterface(main__add* x);
cc:       |                                                                     ^
cc: /tmp/v_502/main.01JWV7BHKYDVC4T05FF8Q0DJJV.tmp.c:1699:76: error: unknown type name 'main__add'
cc:  1699 | static inline main__MyInterface I_main__add_to_Interface_main__MyInterface(main__add* x) {
cc:       |                                                                            ^
cc: /tmp/v_502/main.01JWV7BHKYDVC4T05FF8Q0DJJV.tmp.c:1709:4: error: field designator '_voidptr' does not refer to any field in type 'main__MyInterface' (aka 'struct main__MyInterface')
cc:  1709 |                 ._voidptr = x,
cc:       |                 ~^~~~~~~~~~~~
...
cc: 6 errors generated.
(note: the original output was 19 lines long; it was truncated to its first 12 lines + the last line)
================================================================
(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 .
% v -show-c-output main.v
======== Output of the C Compiler (cc) ========
/tmp/v_502/main.01JWV7B1CW96Y0H5SF74WA3VSY.tmp.c:849:3: error: unknown type name 'main__add'
  849 |                 main__add* _main__add;
      |                 ^
/tmp/v_502/main.01JWV7B1CW96Y0H5SF74WA3VSY.tmp.c:1691:69: error: unknown type name 'main__add'
 1691 | static main__MyInterface I_main__add_to_Interface_main__MyInterface(main__add* x);
      |                                                                     ^
/tmp/v_502/main.01JWV7B1CW96Y0H5SF74WA3VSY.tmp.c:1699:76: error: unknown type name 'main__add'
 1699 | static inline main__MyInterface I_main__add_to_Interface_main__MyInterface(main__add* x) {
      |                                                                            ^
/tmp/v_502/main.01JWV7B1CW96Y0H5SF74WA3VSY.tmp.c:1709:4: error: field designator '_voidptr' does not refer to any field in type 'main__MyInterface' (aka 'struct main__MyInterface')
 1709 |                 ._voidptr = x,
      |                 ~^~~~~~~~~~~~
/tmp/v_502/main.01JWV7B1CW96Y0H5SF74WA3VSY.tmp.c:1741:72: error: expected expression
 1741 |                                         {_SLIT("MyInterface("), 0xfe10, {.d_s = main__add_str(*(main__add*)x._main__add)}},
      |                                                                                                           ^
/tmp/v_502/main.01JWV7B1CW96Y0H5SF74WA3VSY.tmp.c:1745:71: error: no member named '_voidptr' in 'struct main__MyInterface'
 1745 |                                         {_SLIT("MyInterface("), 0xfe10, {.d_s = voidptr_str(*(voidptr*)x._voidptr)}},
      |                                                                                                        ~ ^
6 errors generated.
===============================================

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.10 52c7130

Environment details (OS name and version, etc.)

macOS 14.6 - 2.6 GHz 6-Core Intel Core i7

alaingilbert avatar Jun 03 '25 15:06 alaingilbert

Connected to Huly®: V_0.6-23006

huly-for-github[bot] avatar Jun 03 '25 15:06 huly-for-github[bot]

Possible duplicated issue. See this: https://github.com/vlang/v/issues/23013

jorgeluismireles avatar Jun 04 '25 01:06 jorgeluismireles

Current:

bug.v:12:7: error: cannot implement interface `MyInterface` using function
   10 | fn main() {
   11 |     mut a := []MyInterface{}
   12 |     a << add
      |          ~~~
   13 |     println(a)
   14 | }

felipensp avatar Nov 01 '25 13:11 felipensp