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 with gcc, but not with tcc, for a program, printing an incomplete typedef C struct #21053

Closed
spytheman opened this issue Mar 18, 2024 · 2 comments · Fixed by #21054
Labels
Auto str Method Generation Bugs/feature requests, that are related to the automatic string method generation. Bug This tag is applied to issues which reports bugs. Compiler: GCC Bugs/feature requests, that are related to compiler GCC. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@spytheman
Copy link
Member

spytheman commented Mar 18, 2024

V doctor:

V full version: V 0.4.4 a36c693
OS: linux, Ubuntu 20.04.6 LTS
Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i3-3225 CPU @ 3.30GHz

getwd: /space/v/oo
vexe: /space/v/oo/v
vexe mtime: 2024-03-18 09:30:45

vroot: OK, value: /space/v/oo
VMODULES: OK, value: /home/delian/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.42.0
Git vroot status: weekly.2024.12
.git/config present: true

CC version: cc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
thirdparty/tcc status: thirdparty-linux-amd64 40e5cbb5

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

#include <X11/Xlib.h>

@[typedef]
pub struct C.Display {}
// fn (d &C.Display) str() string { return 'C.Display{}' }

fn main() {
    x := unsafe { &C.Display( malloc(1024) ) }
    println(x)
}

What did you expect to see?

A compiled program, but with gcc. It does compile with tcc.

What did you see instead?
With tcc, which is OK:

&C.Display{}

With gcc, there is an error:

#0 11:35:51 ᛋ master /v/oo❱v -cc gcc run y.v
==================
/tmp/v_1000/y.01HS8ENRJ7YKPB6BXD22QC67E8.tmp.c:2129:35: error: parameter 1 (‘it’) has incomplete type
 2129 | static string Display_str(Display it) { return indent_Display_str(it, 0);}
      |                           ~~~~~~~~^~
/tmp/v_1000/y.01HS8ENRJ7YKPB6BXD22QC67E8.tmp.c: In function ‘Display_str’:
/tmp/v_1000/y.01HS8ENRJ7YKPB6BXD22QC67E8.tmp.c:2129:67: error: type of formal parameter 1 is incomplete
 2129 | static string Display_str(Display it) { return indent_Display_str(it, 0);}
      |                                                                   ^~
/tmp/v_1000/y.01HS8ENRJ7YKPB6BXD22QC67E8.tmp.c: At top level:
/tmp/v_1000/y.01HS8ENRJ7YKPB6BXD22QC67E8.tmp.c:2135:42: error: parameter 1 (‘it’) has incomplete type
 2135 | static string indent_Display_str(Display it, int indent_count) {
      |                                  ~~~~~~~~^~
/tmp/v_1000/y.01HS8ENRJ7YKPB6BXD22QC67E8.tmp.c: In function ‘main__main’:
...
==================
(Use `v -cg` to print the entire error message)

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.

@spytheman spytheman added the Bug This tag is applied to issues which reports bugs. label Mar 18, 2024
@spytheman
Copy link
Member Author

A possible solution is to change the autogenerated C struct str() method from:
static string Display_str(Display it) { return indent_Display_str(it, 0);}
to:
static string Display_str(Display *it) { return indent_Display_str(it, 0);}

i.e. pass Display to it as a pointer, which is valid, even for incomplete C struct types.

@spytheman spytheman added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Auto str Method Generation Bugs/feature requests, that are related to the automatic string method generation. Compiler: GCC Bugs/feature requests, that are related to compiler GCC. labels Mar 18, 2024
@spytheman
Copy link
Member Author

spytheman commented Mar 18, 2024

Note that vlib/clipboard/x11/clipboard.c.v line 23, has a workaround for that bug (in c29b64e), and without it, stuff like this, does not work with clang or gcc, since V structs in wrappers, often contain C struct fields, and the generation for auto string methods is recursive:

import clipboard
c := clipboard.new()
println(c)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto str Method Generation Bugs/feature requests, that are related to the automatic string method generation. Bug This tag is applied to issues which reports bugs. Compiler: GCC Bugs/feature requests, that are related to compiler GCC. 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.

1 participant