]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Allow to override virtual interface implementations
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 26 Sep 2019 09:08:31 +0000 (11:08 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 5 Oct 2019 11:45:55 +0000 (13:45 +0200)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/852

codegen/valaccodemethodmodule.vala
tests/Makefile.am
tests/objects/interface-virtual-override.vala [new file with mode: 0644]
vala/valamethod.vala

index e30e146471bafcf05cfd579521a62f8ecf6ba864..2546b4d8a003e7d82fe7036b2535b9fe09a7cd4b 100644 (file)
@@ -541,7 +541,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                                        if (m.overrides || (m.base_interface_method != null && !m.is_abstract && !m.is_virtual)) {
                                                Method base_method;
                                                ReferenceType base_expression_type;
-                                               if (m.overrides) {
+                                               if (m.overrides && m.base_method != null) {
                                                        base_method = m.base_method;
                                                        base_expression_type = new ObjectType ((Class) base_method.parent_symbol);
                                                } else {
index 4f88ae151d03e8597b623c2cf4c33e6817c98d64..b8fd4d20ef3c4c99f16938db5b86311b8394087a 100644 (file)
@@ -311,6 +311,7 @@ TESTS = \
        objects/interface_only.vala \
        objects/interfaces.vala \
        objects/interface-generics.vala \
+       objects/interface-virtual-override.vala \
        objects/methods.vala \
        objects/paramspec.vala \
        objects/plugin-module-init.vala \
diff --git a/tests/objects/interface-virtual-override.vala b/tests/objects/interface-virtual-override.vala
new file mode 100644 (file)
index 0000000..62fcb65
--- /dev/null
@@ -0,0 +1,16 @@
+interface IFoo : Object {
+       public virtual int foo () {
+               assert_not_reached ();
+       }
+}
+
+class Bar : Object, IFoo {
+       public override int foo () {
+               return 42;
+       }
+}
+
+void main () {
+       var bar = new Bar ();
+       assert (bar.foo () == 42);
+}
index 03653fee9e4b9f650ae0badf7a7e4f108f522672..c8d691c7ce63d37f9b5667f33391a9d6dea14cb2 100644 (file)
@@ -877,7 +877,7 @@ public class Vala.Method : Subroutine, Callable {
                                Report.error (source_reference, "A struct member `%s' cannot be marked as override, virtual, or abstract".printf (get_full_name ()));
                                return false;
                        }
-               } else if (overrides && base_method == null) {
+               } else if (overrides && base_method == null && base_interface_method == null) {
                        Report.error (source_reference, "`%s': no suitable method found to override".printf (get_full_name ()));
                } else if ((is_abstract || is_virtual || overrides) && access == SymbolAccessibility.PRIVATE) {
                        error = true;