[vala/0.40] vala: Improve detection of recursive struct declarations



commit f51c5cecd0b7b89bdd4e0df37796b1505dee7225
Author: Princeton Ferro <princetonferro gmail com>
Date:   Tue Jan 28 23:14:42 2020 -0500

    vala: Improve detection of recursive struct declarations
    
    Introduced with da2d58c95f39fd142dc845f5df9cdcd55be32476
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/902

 tests/Makefile.am                              |  1 +
 tests/semantic/struct-recursive-in-struct.test | 13 +++++++++++++
 vala/valastruct.vala                           | 13 ++++++++-----
 3 files changed, 22 insertions(+), 5 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 962da6729..39bdd8e5a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -750,6 +750,7 @@ TESTS = \
        semantic/struct-field-initializer.test \
        semantic/struct-invalid-base.test \
        semantic/struct-recursive.test \
+       semantic/struct-recursive-in-struct.test \
        semantic/switch-duplicate-label.test \
        semantic/switch-label-not-compatible.test \
        semantic/switch-label-not-constant.test \
diff --git a/tests/semantic/struct-recursive-in-struct.test b/tests/semantic/struct-recursive-in-struct.test
new file mode 100644
index 000000000..bcf5b8a46
--- /dev/null
+++ b/tests/semantic/struct-recursive-in-struct.test
@@ -0,0 +1,13 @@
+Invalid Code
+
+struct Bar {
+       Foo foo;
+}
+
+struct Foo {
+       Foo next;
+       Foo prev;
+}
+
+void main () {
+}
diff --git a/vala/valastruct.vala b/vala/valastruct.vala
index bf02c0260..cf5cc6c4d 100644
--- a/vala/valastruct.vala
+++ b/vala/valastruct.vala
@@ -464,15 +464,18 @@ public class Vala.Struct : TypeSymbol {
                return false;
        }
 
-       bool is_recursive_value_type (DataType type) {
-               var struct_type = type as StructValueType;
+       bool is_recursive_value_type (CodeContext context, DataType type) {
+               unowned StructValueType? struct_type = type as StructValueType;
                if (struct_type != null && !struct_type.nullable) {
-                       var st = (Struct) struct_type.type_symbol;
+                       unowned Struct st = (Struct) struct_type.type_symbol;
                        if (st == this) {
                                return true;
                        }
+                       if (!st.check (context)) {
+                               return false;
+                       }
                        foreach (Field f in st.fields) {
-                               if (f.binding == MemberBinding.INSTANCE && is_recursive_value_type 
(f.variable_type)) {
+                               if (f.binding == MemberBinding.INSTANCE && is_recursive_value_type (context, 
f.variable_type)) {
                                        return true;
                                }
                        }
@@ -512,7 +515,7 @@ public class Vala.Struct : TypeSymbol {
                foreach (Field f in fields) {
                        f.check (context);
 
-                       if (f.binding == MemberBinding.INSTANCE && is_recursive_value_type (f.variable_type)) 
{
+                       if (f.binding == MemberBinding.INSTANCE && is_recursive_value_type (context, 
f.variable_type)) {
                                error = true;
                                Report.error (f.source_reference, "Recursive value types are not allowed");
                                return false;


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