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 codegen for struct with function member has gcc generating warnings #21823

Closed
thomas-mangin opened this issue Jul 8, 2024 · 4 comments · Fixed by #21825
Closed

C codegen for struct with function member has gcc generating warnings #21823

thomas-mangin opened this issue Jul 8, 2024 · 4 comments · Fixed by #21825
Assignees
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. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@thomas-mangin
Copy link
Contributor

thomas-mangin commented Jul 8, 2024

Describe the bug

V can generate working C code (as the compiler can handle the miss-generated C), which is normally not visible when compiling.

Reproduction Steps

with the following 'test.v' file:

module main

type Function = fn (int)

struct MyStruct {
        func Function @[required]
}

fn implementation(size int) {
        println('size is ${size}')
}

fn main() {
        m := MyStruct{
                func: implementation
        }
        println('${m}')
}
v -cc gcc -dump-c-flags compilation_flags.rsp -keepc test.v
gcc @compilation_flags.rsp
/private/tmp/v_501/test.tmp.c:2174:41: warning: too many arguments in call to 'main__Function_str'
        string _t1 = main__Function_str(it.func);
                     ~~~~~~~~~~~~~~~~~~        ^
/private/tmp/v_501/test.tmp.c:2174:33: warning: passing arguments to 'main__Function_str' without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
        string _t1 = main__Function_str(it.func);
                                       ^
2 warnings generated.
ld: warning: REFERENCED_DYNAMICALLY flag on symbol '_catch_exception_raise' is deprecated
ld: warning: REFERENCED_DYNAMICALLY flag on symbol '_catch_exception_raise_state' is deprecated
ld: warning: REFERENCED_DYNAMICALLY flag on symbol '_catch_exception_raise_state_identity' is deprecated
grep main__Function_str /private/tmp/v_501/test.tmp.c
static string main__Function_str(); // auto
static string main__Function_str() { return _SLIT("fn (int)");}
        string _t1 = main__Function_str(it.func);
// V auto functions:
static string indent_main__MyStruct_str(main__MyStruct it, int indent_count) {
	string indents = string_repeat(_SLIT("    "), indent_count);
	string _t1 = main__Function_str(it.func);
	string res = str_intp( 7, _MOV((StrIntpData[]){
		{_SLIT("MyStruct{\n"), 0, {.d_c=0}},
		{_SLIT0, 0xfe10, {.d_s=indents}}, {_SLIT("    func: "), 0, {.d_c=0}}, {_SLIT(""), 16, {.d_s=_t1}}, {_SLIT(""), 0, {.d_c=0}},
		{_SLIT("\n"), 0xfe10, {.d_s=indents}}, {_SLIT("}"), 0, {.d_c=0}},
	}));
	string_free(&_t1);
	string_free(&indents);
	return res;
}

Expected Behavior

the definition has the parameter or the call does not.

Current Behavior

nothing wrong as far as users a concerned

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.6 4475759

Environment details (OS name and version, etc.)

V full version: V 0.4.6 cc14272.4475759
OS: macos, macOS, 14.5, 23F79
Processor: 10 cpus, 64bit, little endian, Apple M1 Max

getwd: /Users/thomas/Code/github.com/ze-community/ze/main
vexe: /Users/thomas/Unix/local/v/master/v
vexe mtime: 2024-07-06 14:05:43

vroot: OK, value: /Users/thomas/Unix/local/v/master
VMODULES: OK, value: /Users/thomas/Unix/data/v/modules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.45.2
Git vroot status: weekly.2024.27-25-g4475759c (5 commit(s) behind V master)
.git/config present: true

CC version: Apple clang version 15.0.0 (clang-1500.3.9.4)
thirdparty/tcc status: thirdparty-macos-arm64 5c1d002f

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.

@thomas-mangin thomas-mangin added the Bug This tag is applied to issues which reports bugs. label Jul 8, 2024
@thomas-mangin thomas-mangin changed the title C codegen for struct with function member has gcc generate warnings C codegen for struct with function member has gcc generating warnings Jul 8, 2024
@spytheman
Copy link
Member

The warnings depend on the used C compiler.
For me, gcc-11 does not produce any for the code in the example, while clang-18 does.

@spytheman
Copy link
Member

As a temporary workaround, until we fix the codegen so that the generated code can be more strict, you can pass -w to the C compiler, to suppress the warnings, i.e. use:
gcc @compilation_flags.rsp -w

@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. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. labels Jul 8, 2024
@thomas-mangin
Copy link
Contributor Author

Thank you @spytheman - The code is compiling. This is more for information and to tidy things than report a blocker.

@spytheman
Copy link
Member

It is a problem, and it should be fixed. C compilers tend to get stricter and stricter, and in time, it can become an error too (gcc-14 made some of its warnings errors by default).

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. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants