[vala/wip/issue/902] vala: Improve detection of recursive struct declarations



commit 72825e6ef132b5c8729a58e5af756d82c0f60894
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                           |  9 ++++++---
 3 files changed, 20 insertions(+), 3 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 29c9e7a8d..ba34f51d2 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -846,6 +846,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 8eff5c723..53a952753 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) {
+       bool is_recursive_value_type (CodeContext context, DataType type) {
                unowned StructValueType? struct_type = type as StructValueType;
                if (struct_type != null && !struct_type.nullable) {
                        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]