[vala/0.46] vala: Perform stricter compatibility check for delegates



commit dfe0d7ea50951bf3b4bcbfdaa0bc7f7eec306416
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun Sep 29 21:25:43 2019 +0200

    vala: Perform stricter compatibility check for delegates
    
    This currently requires to consider compatible delegates as equal.

 tests/Makefile.am                        |  2 ++
 tests/delegates/incompatible-target.test | 17 +++++++++++++++++
 tests/delegates/incompatible.test        | 16 ++++++++++++++++
 vala/valadelegatetype.vala               | 11 +++++++++--
 4 files changed, 44 insertions(+), 2 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f6e4b0881..3efdffbba 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -270,6 +270,8 @@ TESTS = \
        delegates/error-pos.vala \
        delegates/fields.vala \
        delegates/fields-no-target.vala \
+       delegates/incompatible.test \
+       delegates/incompatible-target.test \
        delegates/instance-method-to-no-target.test \
        delegates/lambda-mixed-instance-static.vala \
        delegates/lambda-shared-closure.vala \
diff --git a/tests/delegates/incompatible-target.test b/tests/delegates/incompatible-target.test
new file mode 100644
index 000000000..5db2d1d66
--- /dev/null
+++ b/tests/delegates/incompatible-target.test
@@ -0,0 +1,17 @@
+Invalid Code
+
+delegate void Func ();
+[CCode (has_target = false)]
+delegate void IncompatibleFunc ();
+
+interface Foo : Object {
+       public abstract void foo (Func? func);
+}
+
+class Bar : Object, Foo {
+       public void foo (IncompatibleFunc? func) {
+       }
+}
+
+void main () {
+}
diff --git a/tests/delegates/incompatible.test b/tests/delegates/incompatible.test
new file mode 100644
index 000000000..3280db414
--- /dev/null
+++ b/tests/delegates/incompatible.test
@@ -0,0 +1,16 @@
+Invalid Code
+
+delegate void Func ();
+delegate void IncompatibleFunc (string s);
+
+interface Foo : Object {
+       public abstract void foo (Func? func);
+}
+
+class Bar : Object, Foo {
+       public void foo (IncompatibleFunc? func) {
+       }
+}
+
+void main () {
+}
diff --git a/vala/valadelegatetype.vala b/vala/valadelegatetype.vala
index 73155359d..0220b0168 100644
--- a/vala/valadelegatetype.vala
+++ b/vala/valadelegatetype.vala
@@ -108,6 +108,10 @@ public class Vala.DelegateType : CallableType {
                return result;
        }
 
+       public override bool equals (DataType type2) {
+               return compatible (type2);
+       }
+
        public override bool is_accessible (Symbol sym) {
                return delegate_symbol.is_accessible (sym);
        }
@@ -151,6 +155,10 @@ public class Vala.DelegateType : CallableType {
                        return true;
                }
 
+               if (delegate_symbol.has_target != dt_target.delegate_symbol.has_target) {
+                       return false;
+               }
+
                // target-delegate is allowed to ensure stricter return type (stronger postcondition)
                if (!get_return_type ().stricter (dt_target.get_return_type ().get_actual_type (dt_target, 
null, this))) {
                        return false;
@@ -171,9 +179,8 @@ public class Vala.DelegateType : CallableType {
                }
 
                foreach (Parameter param in dt_target.get_parameters ()) {
-                       /* target-delegate is allowed to accept less arguments */
                        if (!params_it.next ()) {
-                               break;
+                               return false;
                        }
 
                        // target-delegate is allowed to accept arguments of looser types (weaker 
precondition)


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