]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
generics-constraints: add test and improve error message
authorLorenz Wildberg <lorenz@wild-fisch.de>
Wed, 17 May 2023 19:27:56 +0000 (21:27 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 21 Apr 2024 11:43:42 +0000 (13:43 +0200)
tests/generics/multiple-classes-constraints.test [new file with mode: 0644]
vala/valagenericsymbol.vala
vala/valaobjecttypesymbol.vala
vala/valatypeparameter.vala

diff --git a/tests/generics/multiple-classes-constraints.test b/tests/generics/multiple-classes-constraints.test
new file mode 100644 (file)
index 0000000..4ca319f
--- /dev/null
@@ -0,0 +1,9 @@
+Invalid Code
+
+class Foo {}
+class Fuu {}
+
+class Bar<G> where G : Foo, Fuu {
+}
+
+void main () {}
\ No newline at end of file
index 54de021453e41275d0b249caa91b0e1c0d1d01ff..8037afaa553d773e7cf2592e3307def8da6c7b36 100644 (file)
@@ -53,4 +53,14 @@ public interface Vala.GenericSymbol : Symbol {
         * @return index of a type parameter, or -1
         */
        public abstract int get_type_parameter_index (string name);
+
+       public bool check_constraints () {
+               bool error = false;
+               foreach (var parameter in this.get_type_parameters ()) {
+                       if (!parameter.check_constraint ()) {
+                               error = true;
+                       }
+               }
+               return !error;
+       }
 }
index 0ba8c6d70155aabedaa03530551d0ce6d1f94344..94dd53cc332293f83b46e2065884b583079239ec 100644 (file)
@@ -381,6 +381,10 @@ public abstract class Vala.ObjectTypeSymbol : TypeSymbol, GenericSymbol {
                        return !error;
                }
 
+               if (!this.check_constraints ()) {
+                       error = true;
+               }
+
                if (!external_package && has_attribute ("DBus") && !context.has_package ("gio-2.0")) {
                        error = true;
                        Report.error (source_reference, "gio-2.0 package required for DBus support");
index e8c6bb61daa72c2beefbb19fad4253686784417a..bcf383b10a1716de63a6d8fc71915625153becbe 100644 (file)
@@ -141,6 +141,20 @@ public class Vala.TypeParameter : TypeSymbol {
                return name == param2.name && parent_symbol == param2.parent_symbol;
        }
 
+       public bool check_constraint () {
+               bool class_constraint = false;
+               foreach (var type in get_type_constraints ()) {
+                       if (type.symbol is Class) {
+                               if (class_constraint) {
+                                       Report.error (source_reference, "a type parameter can only be constrained with one class type");
+                                       return false;
+                               }
+                               class_constraint = true;
+                       }
+               }
+               return true;
+       }
+
        public override bool check (CodeContext context) {
                if (checked) {
                        return !error;