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

cgen error on init fixed size array of generic type #23545

Closed
larpon opened this issue Jan 21, 2025 · 3 comments · Fixed by #23547
Closed

cgen error on init fixed size array of generic type #23545

larpon opened this issue Jan 21, 2025 · 3 comments · Fixed by #23547
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@larpon
Copy link
Contributor

larpon commented Jan 21, 2025

V version: V 0.4.9 2a69b7c, press to see full `v doctor` output
V full version V 0.4.9 f048bb3.2a69b7c
OS linux, "EndeavourOS Linux"
Processor 16 cpus, 64bit, little endian, AMD Ryzen 7 8845HS w/ Radeon 780M Graphics
Memory 19.6GB/30.64GB
V executable /home/lmp/Projects/v/v
V last modified time 2025-01-21 08:39:50
V home dir OK, value: /home/lmp/Projects/v
VMODULES OK, value: /home/lmp/.vmodules
VTMP OK, value: /dev/shm
Current working dir OK, value: /home/lmp/Projects/v
Git version git version 2.48.1
V git status weekly.2025.03-19-g2a69b7c5 (2 commit(s) behind V master)
.git/config present true
cc version cc (GCC) 14.2.1 20240910
gcc version gcc (GCC) 14.2.1 20240910
clang version clang version 19.1.7
tcc version tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
tcc git status thirdparty-linux-amd64 0134e9b9
emcc version emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.72-git (437140d149d9c977ffc8b09dbaf9b0f5a02db190)
glibc version ldd (GNU libc) 2.40

What did you do?
./v -g -o vdbg cmd/v && ./vdbg t.v && t

module main

import math.vec

pub struct Bezier {
pub mut:
	points [4]vec.Vec2[f32]
}

pub fn (b Bezier) h3() vec.Vec2[f32] {
	return b.points[2]
}

pub fn (b Bezier) h4() vec.Vec2[f32] {
	return b.points[3]
}

pub fn (b Bezier) end() vec.Vec2[f32] {
	return b.h4()
}

pub struct Path {
pub mut:
	segments [1024]Bezier
	len      int // should be read-only
}

pub fn (mut p Path) add(b Bezier) {
	assert p.len + 1 < 1024
	p.segments[p.len] = b
	p.len++
}

fn main() {
	b1 := Bezier{
		points: [vec.Vec2[f32]{100, 100}, vec.Vec2[f32]{120, 80},
			vec.Vec2[f32]{200, 70}, vec.Vec2[f32]{400, 200}]!
	}
	b2 := Bezier{
		points: [b1.end(), b1.h3() + vec.Vec2[f32]{0, 2 * 80}, vec.Vec2[f32]{200, 70},
			b1.end() + vec.Vec2[f32]{400, 200}]!
	}

	mut path := Path{}

	path.add(b1)
	path.add(b2)

	println(sizeof(path))
}

What did you see?

================== C compilation error (from tcc): ==============
cc: /dev/shm/t.01JJ4PA9HFFVSK3Y4YDY0MWBMP.tmp.c:7745: error: cannot convert 'struct math__vec__Vec2_T_f32' to 'void *'
=================================================================
(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 .

What did you expect to see?

Something like "32772"

Workaround

module main

import math.vec

pub struct Bezier {
pub mut:
	points [4]vec.Vec2[f32]
}

pub fn (b Bezier) h3() vec.Vec2[f32] {
	return b.points[2]
}

pub fn (b Bezier) h4() vec.Vec2[f32] {
	return b.points[3]
}

pub fn (b Bezier) end() vec.Vec2[f32] {
	return b.h4()
}

pub struct Path {
pub mut:
	segments [1024]Bezier
	len      int // should be read-only
}

pub fn (mut p Path) add(b Bezier) {
	assert p.len + 1 < 1024
	p.segments[p.len] = b
	p.len++
}

fn main() {

	p_b1 := [vec.Vec2[f32]{100, 100}, vec.Vec2[f32]{120, 80}, vec.Vec2[f32]{200, 70}, vec.Vec2[f32]{400, 200}]!
	b1 := Bezier{
		points: p_b1
	}
	p_b2 := [b1.end(), b1.h3() + vec.Vec2[f32]{0, 2 * 80}, vec.Vec2[f32]{200, 70}, b1.end() + vec.Vec2[f32]{400, 200}]!
	b2 := Bezier{
		points: p_b2
	}

	mut path := Path{}

	path.add(b1)
	path.add(b2)

	println(sizeof(path))
}

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.

@larpon larpon added Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Jan 21, 2025
Copy link

Connected to Huly®: V_0.6-21973

@jorgeluismireles
Copy link

Another workaround, seems just b2 those extra operations are causing the problem:

	a := b1.end()
	b := b1.h3() + vec.Vec2[f32]{0, 2 * 80}
	c := vec.Vec2[f32]{200, 70}
	d := b1.end() + vec.Vec2[f32]{400, 200}
	b2 := Bezier{
		points: [a, b, c, d]!
	}

@jorgeluismireles
Copy link

jorgeluismireles commented Jan 21, 2025

But more likely because using b1 methods, this also works:

	a := b1.end()
	b := b1.h3() + vec.Vec2[f32]{0, 2 * 80}
	d := b1.end() + vec.Vec2[f32]{400, 200}
	b2 := Bezier{
		points: [a, b, vec.Vec2[f32]{200, 70}, d]!
	}

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. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants