[vala/0.54] parser: Improve handling of nullable VarType in with-statement



commit c715dadf52d9489404d5f5dfb27df94b64e350f0
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Jan 25 18:10:49 2022 +0100

    parser: Improve handling of nullable VarType in with-statement

 tests/nullability/var-type.c-expected     |  7 +++++++
 tests/nullability/var-type.vala           |  5 +++++
 tests/parser/var-type-nullable.c-expected | 15 +++++++++++++++
 tests/parser/var-type-nullable.vala       |  8 ++++++++
 vala/valaparser.vala                      |  9 +++++++--
 5 files changed, 42 insertions(+), 2 deletions(-)
---
diff --git a/tests/nullability/var-type.c-expected b/tests/nullability/var-type.c-expected
index 9d0f55e35..10be13b51 100644
--- a/tests/nullability/var-type.c-expected
+++ b/tests/nullability/var-type.c-expected
@@ -56,6 +56,13 @@ _vala_main (void)
                        foo_collection = (_vala_array_free (foo_collection, foo_collection_length1, 
(GDestroyNotify) g_free), NULL);
                }
        }
+       {
+               {
+                       const gchar* foo = NULL;
+                       foo = "foo";
+                       _vala_assert (foo != NULL, "foo != null");
+               }
+       }
 }
 
 int
diff --git a/tests/nullability/var-type.vala b/tests/nullability/var-type.vala
index 3497d8606..42442b18e 100644
--- a/tests/nullability/var-type.vala
+++ b/tests/nullability/var-type.vala
@@ -8,4 +8,9 @@ void main () {
                        assert (foo != null);
                }
        }
+       {
+               with (unowned var? foo = "foo") {
+                       assert (foo != null);
+               }
+       }
 }
diff --git a/tests/parser/var-type-nullable.c-expected b/tests/parser/var-type-nullable.c-expected
index 5f86e3499..84096141e 100644
--- a/tests/parser/var-type-nullable.c-expected
+++ b/tests/parser/var-type-nullable.c-expected
@@ -82,6 +82,21 @@ _vala_main (void)
                        foo_collection = (_vala_array_free (foo_collection, foo_collection_length1, 
(GDestroyNotify) g_free), NULL);
                }
        }
+       {
+               {
+                       gchar* foo = NULL;
+                       gchar* _tmp8_;
+                       _tmp8_ = g_strdup ("foo");
+                       foo = _tmp8_;
+                       _g_free0 (foo);
+               }
+       }
+       {
+               {
+                       const gchar* foo = NULL;
+                       foo = "foo";
+               }
+       }
 }
 
 int
diff --git a/tests/parser/var-type-nullable.vala b/tests/parser/var-type-nullable.vala
index fb3256970..0600a0952 100644
--- a/tests/parser/var-type-nullable.vala
+++ b/tests/parser/var-type-nullable.vala
@@ -13,4 +13,12 @@ void main () {
                foreach (unowned var? foo in new string[] { "foo", "bar" }) {
                }
        }
+       {
+               with (var? foo = "foo") {
+               }
+       }
+       {
+               with (unowned var? foo = "foo") {
+               }
+       }
 }
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 5318bb265..4d860f859 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -2421,8 +2421,13 @@ public class Vala.Parser : CodeVisitor {
                LocalVariable? local = null;
 
                // Try "with (expr)"
-               Expression expr = parse_expression ();
-               if (!accept (TokenType.CLOSE_PARENS)) {
+               Expression expr;
+               try {
+                       expr = parse_expression ();
+               } catch {
+                       expr = null;
+               }
+               if (expr == null || !accept (TokenType.CLOSE_PARENS)) {
                        // Try "with (var identifier = expr)"
                        rollback (expr_or_decl);
                        DataType variable_type;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]