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

C error on &(arr.last() as Type) #23528

Open
islonely opened this issue Jan 19, 2025 · 3 comments
Open

C error on &(arr.last() as Type) #23528

islonely opened this issue Jan 19, 2025 · 3 comments
Labels
Bug This tag is applied to issues which reports bugs. 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

@islonely
Copy link
Contributor

islonely commented Jan 19, 2025

V version: V 0.4.9 11c59cf, press to see full `v doctor` output
V full version V 0.4.9 4225a34.11c59cf
OS macos, macOS, 15.2, 24C101
Processor 8 cpus, 64bit, little endian, Apple M3
Memory 0.47GB/16GB
V executable /Users/adamoates/v/v
V last modified time 2025-01-19 19:21:00
V home dir OK, value: /Users/adamoates/v
VMODULES OK, value: /Users/adamoates/.vmodules
VTMP OK, value: /tmp/v_501
Current working dir OK, value: /Users/adamoates/repos/cyberian_tiger
Git version git version 2.39.5 (Apple Git-154)
V git status weekly.2025.1-88-g11c59cf8 (1 commit(s) behind V master)
.git/config present true
cc version Apple clang version 16.0.0 (clang-1600.0.26.6)
gcc version Apple clang version 16.0.0 (clang-1600.0.26.6)
clang version Apple clang version 16.0.0 (clang-1600.0.26.6)
tcc version tcc version 0.9.28rc 2024-02-05 HEAD@105d70f7 (AArch64 Darwin)
tcc git status thirdparty-macos-arm64 713692d4
emcc version N/A
glibc version N/A

What did you do?
./v -g -o vdbg cmd/v && ./vdbg /Users/adamoates/Documents/test.v && /Users/adamoates/Documents/test

@[heap]
struct Foo {
mut:
	val int
}

@[heap]
struct Bar {
mut:
	val int
}

interface FooBar {
mut:
	val int
}

fn main() {
	mut fbs := []&FooBar{}
	fbs << &Foo{1}
	_ := &(fbs[0] as Foo)
	_ := &(fbs.last() as Foo)
}

What did you see?

================== C compilation error (from cc): ==============
cc: /tmp/v_501/test.01JJ035YNBY47QPGNK9EGP6P59.tmp.c:5766:18: error: cannot take the address of an rvalue of type 'main__Foo' (aka 'struct main__Foo')
cc:  5766 |         {main__Foo* _ = &(({ main__FooBar* _t2 = (*(main__FooBar**)array_last(fbs)); *(main__Foo*)__as_cast(_t2->_main__Foo,v_typeof_interface_idx_main__FooBar(_t2->_typ), 103); }));}
cc:       |                         ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc: 1 error generated.
================================================================
(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?

Expected code to compile and for &(fbs.last() as Foo) to be equal to &(fbs[0] as Foo)

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.

Copy link

Connected to Huly®: V_0.6-21956

@yuyi98
Copy link
Member

yuyi98 commented Jan 21, 2025

It can work now.

@[heap]
struct Foo {
mut:
	val int
}

@[heap]
struct Bar {
mut:
	val int
}

interface FooBar {
mut:
	val int
}

fn main() {
	mut fbs := []&FooBar{}
	fbs << &Foo{1}
	a := &(fbs[0] as Foo)
	println(a)
	b := &(fbs.last() as Foo)
	println(b)
}

PS D:\Test\v\tt1> vdbg run .
&Foo{
    val: 1
}
&Foo{
    val: 1
}

@felipensp
Copy link
Member

It can work now.

@[heap]
struct Foo {
mut:
val int
}

@[heap]
struct Bar {
mut:
val int
}

interface FooBar {
mut:
val int
}

fn main() {
mut fbs := []&FooBar{}
fbs << &Foo{1}
a := &(fbs[0] as Foo)
println(a)
b := &(fbs.last() as Foo)
println(b)
}

PS D:\Test\v\tt1> vdbg run .
&Foo{
val: 1
}
&Foo{
val: 1
}

Hi @yuyi98 , it works on tcc, but not in gcc or clang. 👍🏻

@felipensp felipensp added Bug This tag is applied to issues which reports bugs. 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. labels Jan 21, 2025
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. 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

No branches or pull requests

3 participants