]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Detect usage of static type-parameter in runtime context
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 31 May 2022 08:19:46 +0000 (10:19 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 10 Aug 2022 14:07:44 +0000 (16:07 +0200)
Compact classes doesn't carry runtime information of its type-parameters.
In case this information is required we need report an error.

Fixes https://gitlab.gnome.org/GNOME/vala/issues/1326

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/generics/type-parameter-static-in-runtime.test [new file with mode: 0644]

index c2d0fce0d586d1d058c9493a867b64319b8e93be..4082f502caffa8f6d4aca4a5a57b148e4983e08c 100644 (file)
@@ -3044,6 +3044,11 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
        public CCodeExpression get_type_id_expression (DataType type, bool is_chainup = false) {
                if (type is GenericType) {
                        var type_parameter = ((GenericType) type).type_parameter;
+                       unowned Symbol? parent = type_parameter.owner.owner;
+                       if (parent is Class && ((Class) parent).is_compact) {
+                               Report.error (type.source_reference, "static type-parameter `%s' can not be used in runtime context", type_parameter.get_full_name ());
+                               return new CCodeInvalidExpression();
+                       }
                        string identifier = get_ccode_type_id (type_parameter);
                        return get_generic_type_expression (identifier, (GenericType) type, is_chainup);
                } else {
index 3cf1c91f47e84dd3c97feb7968738f53e196a8de..32d2c56419ee5e671119bdbbaa5fcecbbc0fa2d4 100644 (file)
@@ -764,6 +764,7 @@ TESTS = \
        generics/string-literal-comparison.vala \
        generics/type-parameter-properties.vala \
        generics/type-parameter-property-clash.vala \
+       generics/type-parameter-static-in-runtime.test \
        generics/value-pointer-type-access.vala \
        generics/bug640330.vala \
        generics/bug640330-2.test \
diff --git a/tests/generics/type-parameter-static-in-runtime.test b/tests/generics/type-parameter-static-in-runtime.test
new file mode 100644 (file)
index 0000000..f6b6317
--- /dev/null
@@ -0,0 +1,14 @@
+Invalid Code
+
+[Compact]
+class Foo<T> {
+       public Bar<T> bar () {
+               return new Bar<T> ();
+       }
+}
+
+class Bar<T> {
+}
+
+void main () {
+}