From: Rico Tzschichholz Date: Thu, 3 Oct 2019 18:42:58 +0000 (+0200) Subject: vala: Support "unowned var" to declare local variables X-Git-Tag: 0.47.1~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=39331e235f1183223c8ea685e3501ab4ca932c8f;p=thirdparty%2Fvala.git vala: Support "unowned var" to declare local variables Based on patch by Aaron Andersen Fixes https://gitlab.gnome.org/GNOME/vala/issues/152 --- diff --git a/tests/Makefile.am b/tests/Makefile.am index daf29e9dc..757cc0851 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -586,6 +586,7 @@ TESTS = \ parser/function-syntax-error.test \ parser/inner-array-size.test \ parser/invalid-brace.test \ + parser/local-variable.vala \ parser/lock-statement.vala \ parser/main-block.vala \ parser/method-no-abstract-override.test \ diff --git a/tests/parser/local-variable.vala b/tests/parser/local-variable.vala new file mode 100644 index 000000000..fc22135f0 --- /dev/null +++ b/tests/parser/local-variable.vala @@ -0,0 +1,10 @@ +void main () { + { + var foo = "foo"; + var bar = 42; + } + { + unowned var foo = "foo"; + unowned var bar = 42; + } +} diff --git a/vala/Makefile.am b/vala/Makefile.am index c6aed1c54..78e4c9390 100644 --- a/vala/Makefile.am +++ b/vala/Makefile.am @@ -177,6 +177,7 @@ libvala_la_VALASOURCES = \ valausingdirective.vala \ valavaluetype.vala \ valavariable.vala \ + valavartype.vala \ valaversion.vala \ valaversionattribute.vala \ valavoidtype.vala \ diff --git a/vala/valalocalvariable.vala b/vala/valalocalvariable.vala index f6a39ba8e..e334301c5 100644 --- a/vala/valalocalvariable.vala +++ b/vala/valalocalvariable.vala @@ -79,6 +79,10 @@ public class Vala.LocalVariable : Variable { checked = true; + if (variable_type == null) { + variable_type = new VarType (); + } + if (!context.experimental_non_null) { // local reference variables are considered nullable // except when using experimental non-null enhancements @@ -92,7 +96,7 @@ public class Vala.LocalVariable : Variable { } } - if (variable_type != null) { + if (!(variable_type is VarType)) { if (variable_type is VoidType) { error = true; Report.error (source_reference, "'void' not supported as variable type"); @@ -122,7 +126,7 @@ public class Vala.LocalVariable : Variable { } } - if (variable_type == null) { + if (variable_type is VarType) { /* var type */ if (initializer == null) { @@ -141,8 +145,9 @@ public class Vala.LocalVariable : Variable { return false; } + bool value_owned = variable_type.value_owned; variable_type = initializer.value_type.copy (); - variable_type.value_owned = true; + variable_type.value_owned = value_owned; variable_type.floating_reference = false; initializer.target_type = variable_type; diff --git a/vala/valaparser.vala b/vala/valaparser.vala index fe4e16915..587f6b159 100644 --- a/vala/valaparser.vala +++ b/vala/valaparser.vala @@ -1784,14 +1784,19 @@ public class Vala.Parser : CodeVisitor { void parse_local_variable_declarations (Block block) throws ParseError { var begin = get_location (); DataType variable_type; - if (accept (TokenType.VAR)) { - variable_type = null; + if (accept (TokenType.UNOWNED) && accept (TokenType.VAR)) { + variable_type = new VarType (false); } else { - variable_type = parse_type (true, true); + rollback (begin); + if (accept (TokenType.VAR)) { + variable_type = new VarType (); + } else { + variable_type = parse_type (true, true); + } } bool is_first = true; do { - if (variable_type == null && accept (TokenType.OPEN_PARENS)) { + if (variable_type is VarType && variable_type.value_owned && accept (TokenType.OPEN_PARENS)) { // tuple begin = get_location (); diff --git a/vala/valavartype.vala b/vala/valavartype.vala new file mode 100644 index 000000000..7187fc2c9 --- /dev/null +++ b/vala/valavartype.vala @@ -0,0 +1,38 @@ +/* valavartype.vala + * + * Copyright (C) 2019 Rico Tzschichholz + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Rico Tzschichholz + */ + +/** + * A to be inferred data type. + */ +public class Vala.VarType : DataType { + public VarType (bool value_owned = true) { + this.value_owned = value_owned; + } + + public override string to_qualified_string (Scope? scope) { + return (value_owned ? "var" : "unowned var"); + } + + public override DataType copy () { + return new VarType (value_owned); + } +}