GNOME Bugzilla – Bug 578968
Default constructor of primitive (built-in) types
Last modified: 2010-03-20 16:06:17 UTC
Please describe the problem: Default constructor of primitive (built-in) types, like int(), float(), in C++ is allowed, but results in vala assertion failures and a GCC error. I'm using valac 0.6.1. Steps to reproduce: Use the following program: class Main { static void main (string[] args) { int a = int(); } } Actual results: $valac bug.vala bug.vala:5.10-5.18: warning: local variable `a' declared but never used int a = int(); ^^^^^^^^^ ** (valac:8622): CRITICAL **: vala_ccode_unary_expression_construct: assertion `expr != NULL' failed ** (valac:8622): CRITICAL **: vala_ccode_function_call_add_argument: assertion `expr != NULL' failed bug.c: In function ‘main_main’: bug.c:21: error: too few arguments to function ‘memset’ error: cc exited with status 256 Expected results: a initialized to zero. Does this happen every time? yes Other information: This idiom is often used in combination with generics/templates (to construct a default instance of a class/value type), but I haven't tried this yet.
Created attachment 132850 [details] [review] Patch I'm not sure whether this is the correct place for this, but here goes: primitive types already have defaults assigned in the glib vapi, and these are already propagated into the C code. This patch disables the "use of unassigned local" error when declaring a struct with a default value. I'm not sure whether the "int blah = int();" syntax is supposed to be supported or not.
Created attachment 132851 [details] [review] Patch Oops. First patch had (at least) a regression.
OK. thanks for already looking at this; note that the warning is OK, because the variable "a" is never used in the code (so I would not recommend applying the patch above), However *the main issues* are 1) the assertion failures in the valac source code 2) and the GCC error "too few arguments to function ‘memset’". so I think there are two possible solutions: - adding support for the syntax "int()" - not allowing the syntax "int()", however for other (non-primitive) value types it is allowed (such as struct foo -> foo a = foo()), so this can cause problems in the future (e.g. with generics). For illustrating my point why this syntax is useful: see for example the following program (which does however not compile yet in 0.6.1): class MyClass<T> { public static T create_default<T>() { return T(); } } class Main { static void main(string[] args) { stdout.printf("The default is: %d\n", MyClass<int>.create_default()); } } This would be a generic technique for creating default instances of a given value type.
Exactly the same symptoms with code: Pid p; p = Pid(); Still exists in vala 0.7.0 (from Debian package valac 0.7.0-1). It gives the CRITICAL assertion failures and generates the invalid call to memset. It also considers the variable uninitialized after declaration even though default value is defined and the initialization is actually generated into the C code.
Aha, the bug also applies to value types with the [SimpleType] attribute set (such as Pid in glib-2.0.vapi mentioned in comment #4).
I don't think we should support int() style construction for primitive types, literals should be sufficient. So we should report an error when this construction syntax is used with primitive types. The use case with generics is interesting, however, there are some issues with that approach. Some types require parameters for instantiation, and I don't know whether creating objects and initializing primitive types make sense with the same generic statement.
commit 0d8ce26878a96e7f18d55fa402977afd14e49bad Author: Jürg Billeter <j@bitron.ch> Date: Sat Mar 20 17:02:36 2010 +0100 Do not support constructors for boolean, integer, and floating types Fixes bug 578968.