After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 578968 - Default constructor of primitive (built-in) types
Default constructor of primitive (built-in) types
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Basic Types
0.6.x
Other All
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2009-04-14 20:23 UTC by Bart Goossens
Modified: 2010-03-20 16:06 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Patch (677 bytes, patch)
2009-04-17 21:02 UTC, Levi Bard
none Details | Review
Patch (827 bytes, patch)
2009-04-17 21:09 UTC, Levi Bard
reviewed Details | Review

Description Bart Goossens 2009-04-14 20:23:31 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.
Comment 1 Levi Bard 2009-04-17 21:02:21 UTC
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.
Comment 2 Levi Bard 2009-04-17 21:09:15 UTC
Created attachment 132851 [details] [review]
Patch

Oops. First patch had (at least) a regression.
Comment 3 Bart Goossens 2009-04-17 21:44:13 UTC
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. 

Comment 4 Jan Hudec 2009-04-18 07:54:56 UTC
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.
Comment 5 Bart Goossens 2009-04-18 12:42:46 UTC
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). 
Comment 6 Jürg Billeter 2009-05-07 13:38:31 UTC
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.
Comment 7 Jürg Billeter 2010-03-20 16:06:17 UTC
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.