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: C nested structs do not work #23421

Closed
dy-tea opened this issue Jan 9, 2025 · 0 comments · Fixed by #23422
Closed

cgen: C nested structs do not work #23421

dy-tea opened this issue Jan 9, 2025 · 0 comments · Fixed by #23422
Assignees
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

@dy-tea
Copy link

dy-tea commented Jan 9, 2025

Describe the bug

C structs that contain other structs directly within them with a variable name are not usable in V.

Reproduction Steps

Create a new v project with the following files:

// test.h
#ifndef TEST_H
#define TEST_H

typedef struct {
      struct {
               int x;
       } inner;
} outer;

#endif
// main.v
#flag -I @VMODROOT
#include "test.h"

struct C.outer {
	inner struct {
		x int
	}
}

struct Outer {
	inner struct {
		val int
	}
}

fn main() {
	_ = Outer{}
	_ = C.outer{}
}

Expected Behavior

Should compile.
You are not able to set x at all, even during instantiation of the struct, it should be either set to 0 or produce an error that it is unset (once supported).

Current Behavior

The code will compile with the _ = C.outer{} line removed but will not when it is present, in that case it will output given v -cg -show-c-output .:

======== Output of the C Compiler (/home/dylan/Repos/v/thirdparty/tcc/tcc.exe) ========
/tmp/v_1000/vbug.01JH6NX3SPVX8Z0C171VAPM0PE.tmp.c:2027: error: initialization of incomplete type
=======================================================================================
======== Output of the C Compiler (cc) ========
/tmp/v_1000/vbug.01JH6NX3SPVX8Z0C171VAPM0PE.tmp.c: In function ‘main__main’:
/tmp/v_1000/vbug.01JH6NX3SPVX8Z0C171VAPM0PE.tmp.c:2027:17: error: variable ‘_’ has initializer but incomplete type
 2027 |         {struct outer _ = ((struct outer){.inner = (main___VAnonStruct1){
      |                 ^~~~~
/tmp/v_1000/vbug.01JH6NX3SPVX8Z0C171VAPM0PE.tmp.c:2027:44: error: ‘struct outer’ has no member named ‘inner’
 2027 |         {struct outer _ = ((struct outer){.inner = (main___VAnonStruct1){
      |                                            ^~~~~
/tmp/v_1000/vbug.01JH6NX3SPVX8Z0C171VAPM0PE.tmp.c:2027:52: warning: excess elements in struct initializer
 2027 |         {struct outer _ = ((struct outer){.inner = (main___VAnonStruct1){
      |                                                    ^
/tmp/v_1000/vbug.01JH6NX3SPVX8Z0C171VAPM0PE.tmp.c:2027:52: note: (near initialization for ‘(anonymous)’)
/tmp/v_1000/vbug.01JH6NX3SPVX8Z0C171VAPM0PE.tmp.c:2027:42: error: invalid use of undefined type ‘struct outer’
 2027 |         {struct outer _ = ((struct outer){.inner = (main___VAnonStruct1){
      |                                          ^
/tmp/v_1000/vbug.01JH6NX3SPVX8Z0C171VAPM0PE.tmp.c:2027:23: error: storage size of ‘_’ isn’t known
 2027 |         {struct outer _ = ((struct outer){.inner = (main___VAnonStruct1){
      |                       ^
===============================================

Possible Solution

No response

Additional Information/Context

Working on my project https://github.com/dy-tea/vompositor, I noticed that nested structs were generating invalid eq functions during cgen. I have not been able to reproduce this issue outside that codebase so far but noticed this issue while attempting to reproduce it.

V version

V 0.4.9 ba9d358

Environment details (OS name and version, etc.)

|V full version      |V 0.4.9 de3b184.ba9d358
|:-------------------|:-------------------
|OS                  |linux, "CachyOS"
|Processor           |12 cpus, 64bit, little endian, 13th Gen Intel(R) Core(TM) i5-1335U
|Memory              |3.79GB/15.31GB
|                    |
|V executable        |/home/dylan/Repos/v/v
|V last modified time|2025-01-09 22:52:24
|                    |
|V home dir          |OK, value: /home/dylan/Repos/v
|VMODULES            |OK, value: /home/dylan/.vmodules
|VTMP                |OK, value: /tmp/v_1000
|Current working dir |OK, value: /home/dylan/Repos/vbug
|                    |
|Git version         |git version 2.47.1
|V git status        |weekly.2025.1-25-gba9d358d
|.git/config present |true
|                    |
|cc version          |cc (GCC) 14.2.1 20240910
|gcc version         |gcc (GCC) 14.2.1 20240910
|clang version       |clang version 18.1.8
|tcc version         |tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
|tcc git status      |thirdparty-linux-amd64 0134e9b9
|emcc version        |N/A
|glibc version       |ldd (GNU libc) 2.40

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-21851

@dy-tea dy-tea added the Bug This tag is applied to issues which reports bugs. label Jan 9, 2025
@felipensp felipensp self-assigned this Jan 10, 2025
@felipensp felipensp added 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 10, 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

Successfully merging a pull request may close this issue.

2 participants