Usage of free_function instead of unref_function leads to incorrect code generation
When free_function
is mistakingly used instead of unref_function
, vala generates incorrect code.
For example, if the following file will be used as the vapi file:
[Compact]
[CCode (cname = "struct example", ref_function = "example_ref", free_function = "example_free")]
// Mistakingly use free_function instead of unref_function ^~~~~~~~~~~~~
public class Example {
[CCode (cname = "example_new")]
public Example();
}
And the following file will be used as the bug.vala
:
void bug() {
new Example();
}
Then build will fail with something misleading, like this:
FAILED: bug.p/meson-generated_bug.c.o
cc -Ibug.p -I. -I.. -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/sysprof-4 -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -w -O0 -g -pthread -MD -MQ bug.p/meson-generated_bug.c.o -MF bug.p/meson-generated_bug.c.o.d -o bug.p/meson-generated_bug.c.o -c bug.p/bug.c
../bug.vala: In function ‘bug’:
../bug.vala:2:13: error: ‘NULL’ undeclared (first use in this function)
2 | new Example();
| ^~~~
../bug.vala:2:1: note: ‘NULL’ is defined in header ‘<stddef.h>’; did you forget to ‘#include <stddef.h>’?
1 | void bug() {
+++ |+#include <stddef.h>
2 | new Example();
../bug.vala:2:13: note: each undeclared identifier is reported only once for each function it appears in
2 | new Example();
| ^~~~
ninja: build stopped: subcommand failed.
And as you can see, the following code was generated for the bug.c
:
void
bug (void)
{
struct example* _tmp0_;
struct example* _tmp1_;
#line 2 "../bug.vala"
_tmp0_ = example_new ();
#line 2 "../bug.vala"
_tmp1_ = _tmp0_;
#line 2 "../bug.vala"
(_tmp1_ == NULL) ? NULL : (_tmp1_ = (NULL (_tmp1_), NULL));
#line 28 "bug.c"
}
where NULL is used as a value of unref_function
.
It would be nice to fail on code generation step instead, to not confuse users doing such mistakes.
vala 0.56.13, gcc 13.2.1
Edited by Dmitry Gerasimov