[vala/0.48] vala: Accept "unowned var" as type for foreach variable declaration
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/0.48] vala: Accept "unowned var" as type for foreach variable declaration
- Date: Tue, 22 Dec 2020 20:38:53 +0000 (UTC)
commit f40d6d7f6ad86f01bffa84fa0268b5a610883211
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Mon Nov 9 17:02:50 2020 +0100
vala: Accept "unowned var" as type for foreach variable declaration
In addition to 39331e235f1183223c8ea685e3501ab4ca932c8f
See https://gitlab.gnome.org/GNOME/vala/issues/152
tests/Makefile.am | 1 +
tests/parser/foreach.vala | 11 +++++++++++
vala/valaforeachstatement.vala | 16 ++++++++++++++--
vala/valaparser.vala | 20 ++++++++++++++------
4 files changed, 40 insertions(+), 8 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 2307d434f..bb7b1ee0c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -724,6 +724,7 @@ TESTS = \
parser/field-no-override.test \
parser/field-no-static-class.test \
parser/field-no-virtual.test \
+ parser/foreach.vala \
parser/foreach-no-type.test \
parser/function-syntax-error.test \
parser/inner-array-size.test \
diff --git a/tests/parser/foreach.vala b/tests/parser/foreach.vala
new file mode 100644
index 000000000..7879b67da
--- /dev/null
+++ b/tests/parser/foreach.vala
@@ -0,0 +1,11 @@
+void main () {
+ string[] array = { "foo", "bar", "manam" };
+ foreach (string s in array) {
+ }
+ foreach (unowned string s in array) {
+ }
+ foreach (var s in array) {
+ }
+ foreach (unowned var s in array) {
+ }
+}
diff --git a/vala/valaforeachstatement.vala b/vala/valaforeachstatement.vala
index b2d6062b9..a04076b58 100644
--- a/vala/valaforeachstatement.vala
+++ b/vala/valaforeachstatement.vala
@@ -155,6 +155,10 @@ public class Vala.ForeachStatement : Block {
checked = true;
+ if (type_reference == null) {
+ type_reference = new VarType ();
+ }
+
// analyze collection expression first, used for type inference
if (!collection.check (context)) {
// ignore inner error
@@ -326,9 +330,13 @@ public class Vala.ForeachStatement : Block {
bool analyze_element_type (DataType element_type) {
// analyze element type
- if (type_reference == null) {
+ if (type_reference is VarType) {
// var type
type_reference = element_type.copy ();
+ // FIXME Only follows "unowned var" otherwise inherit ownership of element-type
+ if (!type_reference.value_owned) {
+ type_reference.value_owned = false;
+ }
} else if (!element_type.compatible (type_reference)) {
error = true;
Report.error (source_reference, "Foreach: Cannot convert from `%s' to `%s'".printf
(element_type.to_string (), type_reference.to_string ()));
@@ -344,9 +352,13 @@ public class Vala.ForeachStatement : Block {
bool check_without_iterator (CodeContext context, DataType collection_type, DataType element_type) {
// analyze element type
- if (type_reference == null) {
+ if (type_reference is VarType) {
// var type
type_reference = element_type.copy ();
+ // FIXME Only follows "unowned var" otherwise inherit ownership of element-type
+ if (!type_reference.value_owned) {
+ type_reference.value_owned = false;
+ }
} else if (!element_type.compatible (type_reference)) {
error = true;
Report.error (source_reference, "Foreach: Cannot convert from `%s' to `%s'".printf
(element_type.to_string (), type_reference.to_string ()));
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index cc957c175..e64591c73 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -2126,12 +2126,20 @@ public class Vala.Parser : CodeVisitor {
var begin = get_location ();
expect (TokenType.FOREACH);
expect (TokenType.OPEN_PARENS);
- DataType type = null;
- if (!accept (TokenType.VAR)) {
- type = parse_type (true, true);
- if (accept (TokenType.IN)) {
- Report.error (type.source_reference, "syntax error, expected var or type");
- throw new ParseError.SYNTAX ("expected var or type");
+ var var_or_type = get_location ();
+ DataType type;
+ if (accept (TokenType.UNOWNED) && accept (TokenType.VAR)) {
+ type = new VarType (false);
+ } else {
+ rollback (var_or_type);
+ if (accept (TokenType.VAR)) {
+ type = new VarType ();
+ } else {
+ type = parse_type (true, true);
+ if (accept (TokenType.IN)) {
+ Report.error (type.source_reference, "syntax error, expected `unowned
var', `var' or type");
+ throw new ParseError.SYNTAX ("expected `unowned var', `var' or type");
+ }
}
}
string id = parse_identifier ();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]