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 when embedding C.Structs into V struct #23328

Closed
islonely opened this issue Jan 1, 2025 · 2 comments
Closed

C error when embedding C.Structs into V struct #23328

islonely opened this issue Jan 1, 2025 · 2 comments
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@islonely
Copy link
Contributor

islonely commented Jan 1, 2025

V doctor:

V full version: V 0.4.9 4225a34.7b9b3dd
OS: macos, macOS, 15.1.1, 24B91
Processor: 8 cpus, 64bit, little endian, Apple M3

getwd: /Users/adamoates/Documents/v_tests
vexe: /Users/adamoates/v/v
vexe mtime: 2025-01-01 01:32:40

vroot: OK, value: /Users/adamoates/v
VMODULES: OK, value: /Users/adamoates/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.39.5 (Apple Git-154)
Git vroot status: weekly.2024.52-38-g7b9b3dd9
.git/config present: true

CC version: Apple clang version 16.0.0 (clang-1600.0.26.4)
emcc version: N/A
thirdparty/tcc status: thirdparty-macos-arm64 713692d4

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

test.h

struct Position {
	int x, y;
};

struct Size {
	int width, height;
};

main.v

#include "test.h"
#flag -I ./

type Pos = C.Pos
type Size = C.Size

struct C.Pos {
__global:
	x int
	y int
}

struct C.Size {
__global:
	width int
	height int
}

struct Rectangle {
	C.Pos
	C.Size
}

fn main() {
    println(Rectangle{
		x: 400
		y: 100
		width: 800
		height: 600
	})
}

What did you expect to see?

Expected code to compile and print Rectangle{}.

What did you see instead?

================== C compilation error (from cc): ==============
cc: /tmp/v_501/test2.01JGG7XGNFZXDC952EJBWZNSH3.tmp.c:1047:13: error: field has incomplete type 'struct Pos'
cc:  1047 |         struct Pos Pos;
cc:       |                    ^
cc: /tmp/v_501/test2.01JGG7XGNFZXDC952EJBWZNSH3.tmp.c:834:16: note: forward declaration of 'struct Pos'
cc:   834 | typedef struct Pos main__Pos;
cc:       |                ^
cc: /tmp/v_501/test2.01JGG7XGNFZXDC952EJBWZNSH3.tmp.c:1697:95: error: incomplete definition of type 'struct Pos'
cc:  1697 |                 {_SLIT0, 0xfe10, {.d_s=indents}}, {_SLIT("    x: "), 0, {.d_c=0}}, {_SLIT(""), 7, {.d_i32=it->x}}, {_SLIT(""), 0, {.d_c=0}},
cc:       |                                                                                                           ~~^
cc: /tmp/v_501/test2.01JGG7XGNFZXDC952EJBWZNSH3.tmp.c:834:16: note: forward declaration of 'struct Pos'
cc:   834 | typedef struct Pos main__Pos;
cc:       |                ^
... (the original output was 25 lines long, and was truncated to 12 lines)
================================================================
(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 .

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.

Huly®: V_0.6-21759

@islonely
Copy link
Contributor Author

islonely commented Jan 1, 2025

This also causes a V panic when using aliases instead of C.Struct.

struct Rectangle {
	Pos
	Size
}
V panic: as cast: cannot cast `v.ast.Alias` to `v.ast.Struct`
v hash: 7b9b3dd
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:19135: at _v_panic: Backtrace
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:19603: by __as_cast
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:112106: by v__gen__c__Gen_struct_init
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:92130: by v__gen__c__Gen_expr
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:91214: by v__gen__c__Gen_expr_with_cast
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:111510: by v__gen__c__Gen_gen_expr_to_string
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:102946: by v__gen__c__Gen_fn_call
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:101363: by v__gen__c__Gen_call_expr
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:91788: by v__gen__c__Gen_expr
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:90613: by v__gen__c__Gen_stmt
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:90384: by v__gen__c__Gen_stmts_with_tmp_var
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:90259: by v__gen__c__Gen_stmts
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:100736: by v__gen__c__Gen_gen_fn_decl
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:100268: by v__gen__c__Gen_fn_decl
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:90572: by v__gen__c__Gen_stmt
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:90384: by v__gen__c__Gen_stmts_with_tmp_var
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:90259: by v__gen__c__Gen_stmts
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:89136: by v__gen__c__Gen_gen_file
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:89073: by v__gen__c__cgen_process_one_file_cb
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:32587: by sync__pool__process_in_thread
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:11474: by sync__pool__process_in_thread_thread_wrapper
0x19c3a32e4 : by ???
0x19c39e0fc : by ???
0x102552ab0 : at ???: RUNTIME ERROR: invalid memory access
0x102552554 : by ???
0x102552ebc : by ???
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:19135: by _v_panic
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:19603: by __as_cast
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:112106: by v__gen__c__Gen_struct_init
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:92130: by v__gen__c__Gen_expr
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:91214: by v__gen__c__Gen_expr_with_cast
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:111510: by v__gen__c__Gen_gen_expr_to_string
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:102946: by v__gen__c__Gen_fn_call
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:101363: by v__gen__c__Gen_call_expr
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:91788: by v__gen__c__Gen_expr
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:90613: by v__gen__c__Gen_stmt
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:90384: by v__gen__c__Gen_stmts_with_tmp_var
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:90259: by v__gen__c__Gen_stmts
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:100736: by v__gen__c__Gen_gen_fn_decl
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:100268: by v__gen__c__Gen_fn_decl
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:90572: by v__gen__c__Gen_stmt
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:90384: by v__gen__c__Gen_stmts_with_tmp_var
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:90259: by v__gen__c__Gen_stmts
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:89136: by v__gen__c__Gen_gen_file
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:89073: by v__gen__c__cgen_process_one_file_cb
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:32587: by sync__pool__process_in_thread
v2.01JGFQWM4SVD8YP682NHHPSDMM.tmp.c:11474: by sync__pool__process_in_thread_thread_wrapper
0x19c3a32e4 : by ???

@felipensp felipensp added Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Jan 1, 2025
@islonely
Copy link
Contributor Author

islonely commented Jan 2, 2025

I'm just being dumb again. The issue is C.Pos in the V code instead of C.Position.

@islonely islonely closed this as completed Jan 2, 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. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

No branches or pull requests

2 participants