]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Support creation of arrays with nullable elements
authorAaron Andersen <aaron@fosslib.net>
Sat, 10 Dec 2011 18:58:18 +0000 (19:58 +0100)
committerLuca Bruno <lucabru@src.gnome.org>
Sat, 10 Dec 2011 18:58:18 +0000 (19:58 +0100)
Partially fixes bug 571486.

tests/basic-types/arrays.vala
vala/valaarraytype.vala
vala/valaparser.vala

index 708f2469e04e0410886beb67f71263e75bec6eab..5d4d0c4f93cd0478578c8a1f8999a211e4e4259f 100644 (file)
@@ -41,6 +41,13 @@ void test_integer_array () {
        // in expressions
        assert (23 in a);
        assert (!(-1 in a));
+
+       // nullable elements
+       int?[] d = new int?[2];
+       d[0] = 10;
+       d[1] = null;
+       assert (d[0] == 10);
+       assert (d[1] == null);
 }
 
 void test_string_array () {
index 040df21c25b80e5ae6a08f10b26be0ae63a306ef..d69a6238ddbad510ae75babaa5e7564f7e75876c 100644 (file)
@@ -188,6 +188,10 @@ public class Vala.ArrayType : ReferenceType {
                        return false;
                }
 
+               if (element_type.nullable != target_array_type.element_type.nullable) {
+                       return false;
+               }
+
                if (element_type.compatible (target_array_type.element_type)
                    && target_array_type.element_type.compatible (element_type)) {
                        return true;
index 45db4043a36e44624c151c9241a7a8e2645456e9..76ea0b92dd3372b9e285b72759d0bb4f5b95ecb7 100644 (file)
@@ -844,7 +844,12 @@ public class Vala.Parser : CodeVisitor {
                        var expr = parse_object_creation_expression (begin, member);
                        return expr;
                } else {
+                       bool is_pointer_type = false;
                        while (accept (TokenType.STAR)) {
+                               is_pointer_type = true;
+                       }
+                       if (!is_pointer_type) {
+                               accept (TokenType.INTERR);
                        }
                        if (accept (TokenType.OPEN_BRACKET)) {
                                rollback (begin);
@@ -898,8 +903,15 @@ public class Vala.Parser : CodeVisitor {
                expect (TokenType.NEW);
                var member = parse_member_name ();
                DataType element_type = UnresolvedType.new_from_expression (member);
+               bool is_pointer_type = false;
                while (accept (TokenType.STAR)) {
                        element_type = new PointerType (element_type, get_src (begin));
+                       is_pointer_type = true;
+               }
+               if (!is_pointer_type) {
+                       if (accept (TokenType.INTERR)) {
+                               element_type.nullable = true;
+                       }
                }
                expect (TokenType.OPEN_BRACKET);