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

Compiler build error: cannot convert 'struct main__Object' to 'struct main__Object * #21835

Open
pd-giz-dave opened this issue Jul 9, 2024 · 3 comments
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics.

Comments

@pd-giz-dave
Copy link

pd-giz-dave commented Jul 9, 2024

Describe the bug

Attempt to compile this:

module main
import json
@[heap]
pub struct Logger {
}
struct SaveParams[T] {
	object &T
}
pub fn (mut l Logger) save[T](p SaveParams[T]) {
	json.encode(p.object)
}
struct Object {
	s1 string = 'a string'
}
fn main() {
	mut logger := &Logger{}
	logger.save(object: &Object{}) //to_save)
	println('\\o//')
}

It results in:

/home/dave/v/v /home/dave/precious/fellsafe/v/compiler_bugs -color -o /home/dave/precious/fellsafe/v/bin/main
==================
/tmp/v_1000/main.01J2C3CGZWAPXQBC8PQ9T3F0B1.tmp.c:2241: error: cannot convert 'struct main__Object' to 'struct main__Object *'
...
==================
(Use `v -cg` to print the entire error message)
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 .

Reproduction Steps

Attempt to compile the above code

Expected Behavior

It should compile

Current Behavior

Get build error: cannot convert 'struct main__Object' to 'struct main__Object *'

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.6 66ea826

Environment details (OS name and version, etc.)

dave@dave-mini-pc~/.../fellsafe/v >>> v doctor
V full version: V 0.4.6 294f7e4.66ea826
OS: linux, "EndeavourOS Linux"
Processor: 16 cpus, 64bit, little endian, AMD Ryzen 7 5800H with Radeon Graphics

getwd: /home/dave/precious/fellsafe/v
vexe: /home/dave/v/v
vexe mtime: 2024-07-08 15:23:28

vroot: OK, value: /home/dave/v
VMODULES: OK, value: /home/dave/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.45.2
Git vroot status: weekly.2024.27-32-g66ea8260
.git/config present: true

CC version: cc (GCC) 14.1.1 20240522
thirdparty/tcc status: thirdparty-linux-amd64 40e5cbb5

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@pd-giz-dave pd-giz-dave added the Bug This tag is applied to issues which reports bugs. label Jul 9, 2024
@JalonSolov
Copy link
Contributor

V should definitely give a better message here. To make the code compile and run clean, I added an attribute, and removed the & from several places...

module main

import json

@[heap]
pub struct Logger {
}

struct SaveParams[T] {
        object T
}

pub fn (mut l Logger) save[T](p SaveParams[T]) {
        json.encode(p.object)
}

@[heap]
struct Object {
        s1 string = 'a string'
}

fn main() {
        mut logger := Logger{}
        logger.save(object: Object{}) // to_save)
        println('\\o//')
}

output is

\o//

(which is correct, as \\ becomes a single \ in a string)

@pd-giz-dave
Copy link
Author

But in my real case (that this micky mouse example was created from) Object has to be a reference, not a copy. Object is too big to copy without a big performance impact.
FYI: removing the SaveParams struct and passing &Object directly to save also works. But I can't do that either, as in the real case SaveParams has other similar fields.
The workaround is to not use a struct for the params, but that makes the code ugly.
It also works if the json.encode call is removed, but again my real cases needs that.
As a minimum, the compiler should give an error rather than the build exploding.
PS: In general I love V, these issues will not stop me using it.

@pd-giz-dave
Copy link
Author

I've discovered by looking at the C generated and using the debugger that this:

module main
import json
@[heap]
pub struct Logger {
}
@[params]
struct SaveParams[T] {
	object T
}
pub fn (mut l Logger) save[T](p SaveParams[T]) {
	json.encode(p.object)
}
struct Object {
	s1 string = 'a string'
}
fn main() {
	mut logger := &Logger{}
	mut object := &Object{}
	logger.save(object: object) //to_save)
	println('\\o/')
}

Works in that SaveParams.object is set as a reference. I.e. object is not de-referenced in the logger.save call.
Happy days!

@felipensp felipensp added the Generics[T] Bugs/feature requests, that are related to the V generics. label Jul 11, 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. Generics[T] Bugs/feature requests, that are related to the V generics.
Projects
None yet
Development

No branches or pull requests

3 participants