Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Compile-Time) C error: cannot convert 'struct array' to 'int' #15232

Closed
Apocryphon-X opened this issue Jul 26, 2022 · 3 comments · Fixed by #21796
Closed

(Compile-Time) C error: cannot convert 'struct array' to 'int' #15232

Apocryphon-X opened this issue Jul 26, 2022 · 3 comments · Fixed by #21796
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Comptime Features processed during compile time, like $if, $for, $env etc

Comments

@Apocryphon-X
Copy link

Apocryphon-X commented Jul 26, 2022

V Doctor:

OS: linux, Ubuntu 20.04.4 LTS (WSL 2)
Processor: 12 cpus, 64bit, little endian, 11th Gen Intel(R) Core(TM) i5-11400H @ 2.70GHz
CC version: cc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

getwd: /home/apocryphon/tests/v
vmodules: /home/apocryphon/.vmodules
vroot: /home/apocryphon/packages/v
vexe: /home/apocryphon/packages/v/v
vexe mtime: 2022-07-26 18:05:29
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.0 d6de533.c976a69

Git version: git version 2.25.1
Git vroot status: weekly.2022.30-7-gc976a691
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 827f7452

What did you do?

import os

struct Dummy {}

fn (d Dummy) sample(x int) int {
    return x + 1
}

fn main() {
    $for method in Dummy.methods {
        if os.args.len > 1 {
            println(Dummy{}.$method(os.args[1..]))
        }
    }
}

What did you expect to see?

v run bug_comptime1.v 5:

6    # (Console Output)

What did you see instead?

v -cg bug_comptime1.v

/tmp/v_1000/bug_comptime.11542938031589971243.tmp.c:16991: error: cannot convert 'struct array' to 'int'
builder error:
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

v -prod -cg bug_comptime1.v

/tmp/v_1000/bug_comptime.17039179899661275939.tmp.c: In function ‘main__main’:
/tmp/v_1000/bug_comptime.17039179899661275939.tmp.c:16965:112: error: incompatible type for argument 2 of ‘main__Dummy_sample’
16965 |         println(int_str(main__Dummy_sample(((main__Dummy){EMPTY_STRUCT_INITIALIZATION}), (_t1 = _const_os__args, array_slice(_t1, 1, _t1.len)))));
      |                                                                                          ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                                                                                                |
      |                                                                                                                array {aka struct array}
/tmp/v_1000/bug_comptime.17039179899661275939.tmp.c:16948:43: note: expected ‘int’ but argument is of type ‘array’ {aka ‘struct array’}
16948 | int main__Dummy_sample(main__Dummy d, int x) {
      |                                       ~~~~^
builder error:
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang
@Apocryphon-X Apocryphon-X added the Bug This tag is applied to issues which reports bugs. label Jul 26, 2022
@Apocryphon-X Apocryphon-X changed the title comptime error: cannot convert 'struct array' to 'int' (C error) Compile-time error: cannot convert 'struct array' to 'int' (C error) Jul 26, 2022
@Apocryphon-X Apocryphon-X changed the title Compile-time error: cannot convert 'struct array' to 'int' (C error) (Compile-time) C error: cannot convert 'struct array' to 'int' Jul 26, 2022
@Apocryphon-X Apocryphon-X changed the title (Compile-time) C error: cannot convert 'struct array' to 'int' (Compile-Time) C error: cannot convert 'struct array' to 'int' Jul 26, 2022
@JalonSolov
Copy link
Contributor

V should give a better error message here, but the problem is in your code.

You're passing a slice (which is basically an array) to a method that only takes a single int parameter.

@Apocryphon-X
Copy link
Author

Apocryphon-X commented Jul 26, 2022

I thought it should compile because (for some reason) this haves the same workaround as #15230 🤔

Current workaround
Extracting the string returned by $tmpl(...) into a separate variable and then passing it as a parameter works.

This compiles without problem:

import os

struct Dummy {}

fn (d Dummy) sample(x int) int {
    return x + 1
}

fn main() {
    $for method in Dummy.methods {
        tmp := os.args[1..]
        println(Dummy{}.$method(tmp))
    }
}

And it doesn't even fail nor panics at runtime with more or less than one element in os.args. Apparently it only takes the necessary amount of arguments to make the call of sample, if there are less than necessary it just sets everything to zero or an equivalent (even without using a slice)

@Apocryphon-X
Copy link
Author

Apocryphon-X commented Jul 26, 2022

I don't know if this [passing arrays to methods at compile-time this way] is a feature or a bug but it's really cool. It doesn't work at all with float types tho 😢 (#15220)

@ghost ghost mentioned this issue Oct 23, 2022
20 tasks
@felipensp felipensp self-assigned this Jul 3, 2024
@felipensp felipensp added the Comptime Features processed during compile time, like $if, $for, $env etc label Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Comptime Features processed during compile time, like $if, $for, $env etc
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants