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



commit 887546c0d07bff9d641c7e855a2f9d0325b7deff
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 63ab63688..5cf2966bf 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -821,6 +821,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 e82f52329..422f8baf7 100644
--- a/vala/valastruct.vala
+++ b/vala/valastruct.vala
@@ -462,15 +462,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;
                                }
                        }
@@ -510,7 +513,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]