vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.71k stars 2.16k forks source link

cannot pass struct where its interface type is expected #18701

Closed einar-hjortdal closed 1 year ago

einar-hjortdal commented 1 year ago

Describe the bug

Ran into this cgen error, don't know how to isolate it so I am linking the repository here. The branch cgen_error is not going to change so the problem will be frozen in time.

error: cannot convert 'struct __StatusCmd' to 'struct __Cmder'

Expected Behavior

Not a cgen error

Current Behavior

cgen error

Reproduction Steps

  1. git clone https://github.com/Coachonko/redis.git && cd redis/
  2. git checkout cgen_error
  3. v run src/cmd_test.v

Possible Solution

No response

Additional Information/Context

No response

V version

Current V version: V 0.3.4 499d052, timestamp: 2023-06-28 13:40:36 +0300

Environment details (OS name and version, etc.)

V full version: V 0.3.4 ee4150f.499d052
OS: linux, Linux version 5.14.0-284.11.1.el9_2.x86_64 (mockbuild@pp-el9-x86) (gcc (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4), GNU ld version 2.35.2-37.el9) #1 SMP PREEMPT_DYNAMIC Tue May 9 11:00:23 UTC 2023
Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i3-5005U CPU @ 2.00GHz

getwd: /home/coachonko/Documents/projects/vlang/redis
vexe: /home/coachonko/.local/lib64/v/v
vexe mtime: 2023-06-28 10:51:40

vroot: OK, value: /home/coachonko/.local/lib64/v
VMODULES: OK, value: /home/coachonko/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.39.3
Git vroot status: weekly.2023.26-19-g499d0526
.git/config present: true

CC version: cc (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4)
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3
felipensp commented 1 year ago
VV_LOCAL_SYMBOL redis__StatusCmd redis__Cmdable_ping(redis__Cmdable c) {
    redis__StatusCmd cmd = redis__new_status_cmd(new_array_from_c_array(1, 1, sizeof(x__json2__Any), _MOV((x__json2__Any[1]){string_to_sumtype_x__json2__Any(ADDR(string, (_SLIT("ping"))))})));
    c.cmdable_function(cmd); // <<----- error
    return cmd;
}

error: cannot convert 'struct redisStatusCmd' to 'struct redisCmder'

felipensp commented 1 year ago

As a workaround, you can cast the StatusCmd to Cmder one, like:

diff --git a/src/cmdable.v b/src/cmdable.v
index 4714a99..d9706cd 100644
--- a/src/cmdable.v
+++ b/src/cmdable.v
@@ -18,7 +18,7 @@ mut:

 fn (c Cmdable) ping() StatusCmd {
        cmd := new_status_cmd('ping')
-       c.cmdable_function(cmd)
+       c.cmdable_function(Cmder(cmd))
        return cmd
 }

@@ -38,19 +38,19 @@ mut:

 pub fn (c CmdableStateful) auth(password string) StatusCmd {
        cmd := new_status_cmd('auth', password)
-       c.cmdable_stateful_function(cmd)
+       c.cmdable_stateful_function(Cmder(cmd))
        return cmd
 }

 pub fn (c CmdableStateful) auth_acl(username string, password string) StatusCmd {
        cmd := new_status_cmd('auth', username, password)
-       c.cmdable_stateful_function(cmd)
+       c.cmdable_stateful_function(Cmder(cmd))
        return cmd
 }
felipensp commented 1 year ago

@medvednikov is it expected to be done automatically, right?

einar-hjortdal commented 1 year ago

Thanks for the help. I also assumed it should be done automatically.