From: Rico Tzschichholz Date: Tue, 25 Jan 2022 17:10:49 +0000 (+0100) Subject: parser: Improve handling of nullable VarType in with-statement X-Git-Tag: 0.55.2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6a8e2795d313b98bc60ff3956b911ff7297451fe;p=thirdparty%2Fvala.git parser: Improve handling of nullable VarType in with-statement --- 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 ebb914da1..1e84c8bfa 100644 --- a/vala/valaparser.vala +++ b/vala/valaparser.vala @@ -2541,8 +2541,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;